【问题标题】:Push calculated result from one array to another using loops使用循环将计算结果从一个数组推送到另一个数组
【发布时间】:2021-06-01 21:57:48
【问题描述】:

我试图弄清楚如何在使用函数计算结果后将结果从数组推送到空数组,使用 for 循环和另一个使用 while 循环,但我不明白我在做什么错误的。这是完整的 JavaScript 课程 2021 的一部分:从零到专家!在udemy上。如果有人能帮助我理解这一点,我将不胜感激。

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);

预期结果是人口数组中推入其各自数组的 4 个数字中的每一个计算出的世界人口百分比。

我得到的第一个百分比的结果:script.js:179 (4) [0, 0.012658227848101267, 0.025316455696202535, 0.0379746835443038]

我得到的 percents2 的结果是一个空数组。

【问题讨论】:

    标签: javascript arrays function for-loop while-loop


    【解决方案1】:

    如果您想在两个循环中获得相同的结果,那么您的错误是,您正在查看 population(array) 是否小于其长度。 您需要声明一个新的 int 变量,然后将 +1 添加到 int 而不是数组。

    另外,for循环是在循环内的代码完成后加上+1,所以在while循环中你需要在最后加上+1。查看我添加的sn-p

    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);

    编辑: 您想传递“人口”数组的 4 个值吗? 然后你需要在数组中传递整数 n 或 i --> population[i]

    另一种选择,您可以将其用于 for 循环:

    for (let i of populations) {
        percentages3.push(percentageOfWorld1(i));
    }
    

    这和sn-p中的for循环是一样的,试试看吧:)

    【讨论】:

    • 我明白了,谢谢,但是为什么把每个数组的第一个结果都计算为0,这也是我想不通的。
    • 这是因为,你从值 0 开始,因此如果你将 0 传递给你的函数,它的结果是 0/7900 * 100,结果总是 0 :)
    • 感谢您的回复,但是您如何避免这种情况并使其计算数组中索引 0 的实际数量为 1441?
    • 现在我明白你的意思了,我也有点困惑,那么你需要传递你声明的索引的元素,所以不是传递 n 或 i,而是传递 population[n或 i],这意味着您通过人口的 n 指数
    • 我还在答案末尾添加了另一种紧凑且更好的 for 循环方法。
    【解决方案2】:

    来自您的代码:

    let populations = [1441, 4, 6, 8];
    populations++;
    

    ++ 是用于数字而非数组的运算符。要对每个数组元素进行操作,您需要对它们进行迭代/“循环”。另外:

    percentages2.push(percentageOfWorld1(populations));
    

    populations 这里是一个数组,而您的 percentageOfWorld1 函数需要一个数字。你不应该提供populations[i] 元素吗?了解有关Arrays 的更多信息。使用 while 循环。

    let i = 0;
    while(i < populations.length) {
        percentages2.push(percentageOfWorld1(populations[i]));
        i++;
    }
    

    更常见的初学者方法是使用 for 循环,就像您尝试使用 percentages3 一样。您需要提供人口[i] 而不是 i。练习并检查差异。

    注意:等效的功能是:

    percentages2 = populations.map(percentageOfWorld1);
    

    【讨论】:

    • 感谢您的回复,我的脑子里有很多东西,我需要花时间来理解这一点。
    猜你喜欢
    • 2017-06-22
    • 2014-02-04
    • 2020-08-23
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2021-06-20
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多