【问题标题】:Function returns Nan returns when I send More argument then function parameters当我发送更多参数然后函数参数时,函数返回 Nan 返回
【发布时间】:2021-11-22 02:18:07
【问题描述】:

我不明白为什么当我传递的参数多于参数时它会发送 nan

function percetageofworld3(population1) {
    return (population1 / 7900) * 100;
}
const describePopulation = function(country, population) {
    const chinesePopulation = percetageofworld3(country, population);
    console.log(chinesePopulation)
    const countries = `${country} has ${population} million people,
        which is about ${chinesePopulation}% of the world`
    return countries;
}

【问题讨论】:

  • country 长什么样子?
  • 因为你正在做一个字符串/数字运算,它给出了 NaN。将人口传递到世界百分比3

标签: javascript function javascript-objects


【解决方案1】:

你传递给percetageofworld3 两个参数,但函数只有一个,所以你传递国家例如'italy',它将是return ('italy' / 7900) * 100;

如果你只通过数字工作

function percetageofworld3(population1) {
  return (population1 / 7900) * 100;
}
const describePopulation = function(country, population) {
  const chinesePopulation = percetageofworld3(population);
  console.log('Result of chinesePopulation: ' + chinesePopulation)
  const countries = `${country} has ${population} million people,
which is about ${chinesePopulation}% of the world`
  return countries;
}
console.log('Result of describePopulation: ' + describePopulation('italy', 1000))

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2021-08-06
    • 2021-05-10
    • 2012-09-08
    相关资源
    最近更新 更多