[英]How do I return a variable from nested For Loops in JavaScript without using a global variable?
Here is my code sans input这是我的无输入代码
// Check if three addends equal sum and return the product if so
let result;
function addNumbers(first,second,third,sum) {
if (first + second + third === sum) {
result = first * second * third;
return (first * second * third);
}
};
// find three numbers in list that add up to specific number
function testResult(list,sum) {
let firstAddend;
let secondAddend;
let thirdAddend;
for (let i = 0; i < list.length; i++) {
firstAddend = list.shift();
for (let j = 0; j < list.length; j++) {
secondAddend = list[j];
for (let k = 1; k < list.length; k++) {
thirdAddend = list[k];
addNumbers(firstAddend,secondAddend,thirdAddend,sum);
}
}
}
};
What I want is testResult()
to return the result from addNumbers()
when it returns the product.我想要的是
testResult()
在返回产品时从addNumbers()
返回结果。 I want to get rid of let result;
我想摆脱
let result;
and result =...
in addNumbers()
.和
result =...
在addNumbers()
。 I've been confused about scope but I think I'm starting to understand.我一直对 scope 感到困惑,但我想我开始明白了。 Does each
for
loop contain the scope of the previous?每个
for
循环是否包含前一个的scope? If anyone is interested this is from Advent of Code Day 1. I am not certain if having the data is necessary here.如果有人感兴趣,这是来自代码出现的第 1 天。我不确定这里是否需要数据。 If it is let me know and I will edit accordingly.
如果是让我知道,我会相应地编辑。
Does each
for
loop contain the scope of the previous?每个
for
循环是否包含前一个的scope?
Yes, it does.是的,它确实。 Whenever you create a sub-scope, all the variables in the previous scope are available.
每当创建子作用域时,前面的 scope 中的所有变量都可用。 So, you don't actually have to declare
firstAddend
and secondAddend
and thirdAddend
ahead of time:因此,您实际上不必提前声明
firstAddend
和secondAddend
和thirdAddend
:
function testResult(list,sum) {
for (let i = 0; i < list.length; i++) {
let firstAddend = list.shift();
for (let j = 0; j < list.length; j++) {
let secondAddend = list[j];
for (let k = 1; k < list.length; k++) {
let thirdAddend = list[k];
addNumbers(firstAddend,secondAddend,thirdAddend,sum);
}
}
}
}
Next, the return
means that when you call the function, it takes on the value that you return.接下来,
return
意味着当您调用 function 时,它会采用您返回的值。 So, you don't need a global result
variable, as you can just utilize the return value.因此,您不需要全局
result
变量,因为您可以只使用返回值。 However, you should move the if
statement out of the addNumbers
function and into the testResult
function, as you need to know when to return, not just what to return.但是,您应该将
if
语句从addNumbers
function 移到testResult
function 中,因为您需要知道何时返回,而不仅仅是返回什么。 In fact, the addNumbers
function is simply enough to where it should just go directly into testResult
:事实上,
addNumbers
function 就足够了,它应该只是 go 直接进入testResult
:
function testResult(list,sum) {
for (let i = 0; i < list.length; i++) {
let firstAddend = list.shift();
for (let j = 0; j < list.length; j++) {
let secondAddend = list[j];
for (let k = 1; k < list.length; k++) {
let thirdAddend = list[k];
if (firstAddend + secondAddend + thirdAddend === sum) {
return firstAddend * secondAddend * thirdAddend;
}
}
}
}
}
For practice, if you want the function, you could do something like the following:作为练习,如果您想要 function,您可以执行以下操作:
function addNumbers(first,second,third,sum) {
if (first + second + third === sum) {
return (first * second * third);
} else {
return null; // return some value so the calling function knows that no sum was found
}
}
function testResult(list,sum) {
for (let i = 0; i < list.length; i++) {
let firstAddend = list.shift();
for (let j = 0; j < list.length; j++) {
let secondAddend = list[j];
for (let k = 1; k < list.length; k++) {
let thirdAddend = list[k];
let result = addNumbers(firstAddend, secondAddend, thirdAddend, sum);
if (result !== null) {
return result;
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.