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
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:
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.
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;
.
Look into the forEach
function, which works as a for loop over an array and it can be an excellent implementation on your example.
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!