在JavaScript中将humanYears转换为catYears和dogYears
问题
我们需要编写一个JavaScript函数,该函数将人类的年龄以年为单位,并返回各自的dogYears和catYears。
输入
const humanYears = 15;
输出
const output = [ 15, 76, 89 ];
示例
以下是代码-
const humanYears = 15; const humanYearsCatYearsDogYears = (humanYears) => { let catYears = 0; let dogYears = 0; for (let i = 1; i <= humanYears; i++) { if (i === 1) { catYears += 15; dogYears += 15; }else if (i === 2) { catYears += 9; dogYears += 9; }else { catYears += 4; dogYears += 5; } } return [humanYears, catYears, dogYears]; }; console.log(humanYearsCatYearsDogYears(humanYears));
输出结果
[ 15, 76, 89 ]