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
change the example[i/100]
to example[i] /100
and you can console.log
each response from inside the for loop.
var example = [5, 10, 15, 20];
for (var i = 0; i < example.length; i++) {
console.log(example[i] / 100);
}
divide each value by 100,
Yes, you want to divide each value by 100, but also store the result back into the array. In JavaScript, if you want to do some calculation and store the results, you need to say
left-hand-side = calculation
In other words, you need to use the assignment operator (=
) to assign the results of the calculation to some variable, array element, or property.
In your case, the "calculation" is the value of the array element divided by 100, so it would be
example[i] / 100
You may wonder why example[i/100]
does not work. Think about it. The thing that comes inside the []
is an index into the array. You don't want to divide the index i
by 100; you want to divide the value at index i
by 100.
The result of this calculation--dividing the element at index i
by 100--you want to put back into the array at the same location. So the "left-hand-side" is also element[i]
. The entire statement thus is
example[i] = example[i] / 100;
Your attempt, which was
( example [ i/ 100] )
is therefore missing the mark on a couple of levels. First of all, you are dividing the index by 100, instead of dividing the value at the index by 100. Second, you are not assigning the result to anything--so the result will just be thrown away.
Merely changing the line you knew was shaky, to the above example[i] = example[i] / 100;
will make your code work perfectly. You don't need forEach
, or map
, or arrow functions (although I hope you will learn what they are and start using them sometime soon).
In this particular case, we are assigning the result of dividing a particular array element by 100 back to that same array element. In such cases, JavaScript provides special assignment operators, which allow you to avoid duplicating the reference to the array element (or variable, or object property). In this case, you're interested in the "division assignment operator", which you can use as
example[i] /= 100;
^^
But there's no particular need to use that, other than brevity.
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!
The non-ES6 code might read like this. map returns a new array where each element has been changed by the function. It's up to you whether you want a new array, or whether you use a for
loop to change the elements of the existing array.
var example = [5, 10, 15, 20];
Example 1
var out = example.map(function (el) {
return el / 100;
});
console.log(out); // [ 0.05, 0.1, 0.15, 0.2 ]
Example 2
for (var i = 0, l = example.length; i < l; i++) {
example[i] = example[i] / 100;
}
console.log(example);
UPDATED to save the modified values in-place
There's not much reason to log() each value separately. You could streamline the code with something like this, with pretty ES6 arrow functions and ES5 array methods:
var example = [5, 10, 15, 20]
console.log(example = example.map(x => x/100))
Note: arrow functions such as x => x/100
currently work in a very small subset of Web browsers. If you want to publish a web app or page right now, you should use a standard anonymous function: function (x) { return x/100 }
.