Looping Through An Array and Dividing Each Value By 100

后端 未结 5 1233
Happy的楠姐
Happy的楠姐 2021-01-27 03:44

Hey this is my first question and I\'m just finding my way with JS. I have an array which is set, I want to loop through it and divide each value by 100, then print the output t

5条回答
  •  臣服心动
    2021-01-27 04:25

    I fixed your code (see below):

    var example = [5, 10, 15, 20];
    
    example.forEach(function(i) {
      console.log(example[i] / 100);
    });
    

    So a couple of comments on the above:

    1. I believe you meant to get as a result: 0.05, 0.1., 0.15, and 0.2 by dividing by 100.. so keep in mind your math.

    2. You are dividing by the index of the array example, change your syntax to represent the following: example[i] = example[i] / 10;, which translates to short hand form: example[i] /= 100;.

    3. Look into the forEach function, which works as a for loop over an array and it can be an excellent implementation on your example.

    4. Assuming you want to extend your knowledge, it would be great if you jumped into a beginner course. I can't think of anywhere better to start than here: CodeAcademy, Javascript courses... I hope it helps!

提交回复
热议问题