为什么我的 JavaScript 函数的输出未定义?
Why is the output of my JavaSript function undefined?
任何人都可以提示我下面的 Javascript 函数有什么问题吗?
我正在寻找两个数字之间的最大倍数。
当我在我的测试 window 中编写不在函数内部的代码时,它可以工作并且它给了我我需要的东西,但是如果我想将它包含在我的函数中,输出是未定义的。我相信我错过了一些愚蠢的东西,但我已经解决了几个小时,但我看不到它。
function maxMultiple(d, b){
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i)}
if (d>0 && b>0){
return arr.pop(); // does not work - undefined
} else {
return d; }
}
console.log(maxMultiple(7,100)); // Output is undefined
// it works....
/*
let b = 7;
const d = 100;
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i);
}
console.log(arr.pop()); //Output is 98
*/
function maxMultiple(d, b){
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i)
}
console.log(arr.length === 0); // array is empty here
if (d>0 && b>0){
let res; // cache result
// pop() on empty array always gives undefined
console.log([].pop() === (res = arr.pop()));
return res; // does not work - undefined
} else {
return d;
}
}
console.log(maxMultiple(7,100)); // Output is undefined
看起来您已经互换了变量 b
和 d
。只需交换函数签名中的参数,您就会得到您期望的结果。
您分享的示例将有效,因为您将 b 用作 7,将 d 用作 100。
在实际代码中,您传递的参数如下:
b - 100,d - 7。
因此 for
循环甚至没有执行一次,因此记录了您提到的 undefined
。
希望对您有所帮助!
这一行有问题 for (let i = b; i <= d; i += b)
。在你的程序中 d 是 7 并且 b 是 100 所以没有元素被推到 array
function maxMultiple(d, b) {
const arr = [];
for (let i = b; i <= d; i += b) { // i = i + b
arr.push(i)
}
if (d > 0 && b > 0) {
return arr.pop(); // does not work - undefined
} else {
return d;
}
}
console.log(maxMultiple(7, 100));
因为100小于7,数组为空。什么都没有被推动。
我猜你的两个参数顺序错了。
任何人都可以提示我下面的 Javascript 函数有什么问题吗? 我正在寻找两个数字之间的最大倍数。 当我在我的测试 window 中编写不在函数内部的代码时,它可以工作并且它给了我我需要的东西,但是如果我想将它包含在我的函数中,输出是未定义的。我相信我错过了一些愚蠢的东西,但我已经解决了几个小时,但我看不到它。
function maxMultiple(d, b){
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i)}
if (d>0 && b>0){
return arr.pop(); // does not work - undefined
} else {
return d; }
}
console.log(maxMultiple(7,100)); // Output is undefined
// it works....
/*
let b = 7;
const d = 100;
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i);
}
console.log(arr.pop()); //Output is 98
*/
function maxMultiple(d, b){
const arr = [];
for (let i=b; i<=d; i+=b) { // i = i + b
arr.push(i)
}
console.log(arr.length === 0); // array is empty here
if (d>0 && b>0){
let res; // cache result
// pop() on empty array always gives undefined
console.log([].pop() === (res = arr.pop()));
return res; // does not work - undefined
} else {
return d;
}
}
console.log(maxMultiple(7,100)); // Output is undefined
看起来您已经互换了变量 b
和 d
。只需交换函数签名中的参数,您就会得到您期望的结果。
您分享的示例将有效,因为您将 b 用作 7,将 d 用作 100。
在实际代码中,您传递的参数如下:
b - 100,d - 7。
因此 for
循环甚至没有执行一次,因此记录了您提到的 undefined
。
希望对您有所帮助!
这一行有问题 for (let i = b; i <= d; i += b)
。在你的程序中 d 是 7 并且 b 是 100 所以没有元素被推到 array
function maxMultiple(d, b) {
const arr = [];
for (let i = b; i <= d; i += b) { // i = i + b
arr.push(i)
}
if (d > 0 && b > 0) {
return arr.pop(); // does not work - undefined
} else {
return d;
}
}
console.log(maxMultiple(7, 100));
因为100小于7,数组为空。什么都没有被推动。
我猜你的两个参数顺序错了。