Reputation: 45
I am trying to figure out how to push the result from the array to the empty arrays after using a function to calculate the result, using a for loop and another with a while loop but I don't understand what I am doing wrong. This is part of The Complete JavaScript Course 2021: From Zero to Expert! on udemy. I would really appreciate if someone could help me understand this.
function percentageOfWorld1(population) {
const worldPopulation = 7900;
return (population / worldPopulation) * 100;
}
let populations = [1441, 4, 6, 8];
const percentages2 = [];
const percentages3 = [];
while (populations < populations.length) {
populations++;
percentages2.push(percentageOfWorld1(populations));
}
for (let i = 0; i < populations.length; i++) {
percentages3.push(percentageOfWorld1(i));
}
console.log(percentages2);
console.log(percentages3);
Expected results is the calculated percentage of the world population for each of the 4 numbers in the populations array pushed in to its respective array.
result I got for the first one percentages3: script.js:179 (4) [0, 0.012658227848101267, 0.025316455696202535, 0.0379746835443038]
the result I got for percentages2 was an empty array.
Upvotes: 0
Views: 181
Reputation: 1038
If you want to get the same result in both loops, then your mistake was, that you were looking if populations(array) is smaller than its length. You need to declare a new int variable and then add +1 to the int and not to the array.
Additionally, the for loop adds +1 after the code inside the loop is done, so in the while loop you need to add +1 at the end. look in the snippet I added
function percentageOfWorld1(population) {
const worldPopulation = 7900;
return (population / worldPopulation) * 100;
}
let populations = [1441, 4, 6, 8];
const percentages2 = [];
const percentages3 = [];
let n = 0;
while (n < populations.length) {
percentages2.push(percentageOfWorld1(populations[n]));
n++;
}
for (let i = 0; i < populations.length; i++) {
percentages3.push(percentageOfWorld1(populations[i]));
}
console.log(percentages2);
console.log(percentages3);
EDIT: You want to pass the 4 values of the 'populations'array right? Then you need to pass the integer n or i in the array --> populations[i]
Another alternative, you can use this for the for loop:
for (let i of populations) {
percentages3.push(percentageOfWorld1(i));
}
this is the same as the for loop in the snippet, just try it out :)
Upvotes: 1
Reputation: 161
From your code:
let populations = [1441, 4, 6, 8];
populations++;
++
is an operator used for numbers, not arrays. To make an operation on every array element, you need to iterate / "loop" on them. Also:
percentages2.push(percentageOfWorld1(populations));
populations
here is an array, while your percentageOfWorld1
function expects a number. Shouldn't you provide populations[i]
element instead? Learn more about Arrays. Using a while loop.
let i = 0;
while(i < populations.length) {
percentages2.push(percentageOfWorld1(populations[i]));
i++;
}
The more common beginner way is using a for loop, as you have tried with percentages3
. You need to provide populations[i] instead of i. Practice and check the differences.
Note: the functional equivalent would be :
percentages2 = populations.map(percentageOfWorld1);
Upvotes: 1