我需要帮助...我正在学习 JavaScript,这看起来很简单,但我可能只是忽略了...一切...我的问题是我需要返回 0 到 0 之间所有偶数的字符串包括 num; ex 7 为您提供 0246 等。我已经得到:
function stringOfEvens(num) {
for (let i = 0; i <= num.length; ++i) {
if (i % 2 === 0 ); {
return i;
}
}
}
我知道整个前景是运行一个 for 循环,从 0 到每次附加的有问题的数字,如果该数字 % 2 = 0,则返回该数字,但有些东西是错过的......我什至可能过度思考并错误地对待这整件事......这象征性地让我发疯......
最佳答案
function theJob(limit) {
var res = ''; //initialize as a string so that the other numbers will be appended instead of added
for (i = 0; i <= limit; i += 2) { // step increase by 2 to skip odd numbers
res += i;
}
return res; // returning the resulting string
}
console.log(theJob(10));
关于javascript - 如何从odd数组中获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177926/