javascript 函数 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11443742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
function while loop
提问by Ezcaflowne
I'm trying to get this function to work properly in javascript, well it is working properly what i can get to happen is the bottom console.log (work(" gallons of water has been extracted from the carpet.")); I can't get the " gallons of water has been extracted from the carpet." to show up in that same line of code.
我正在尝试让这个函数在 javascript 中正常工作,它工作正常,我能得到的是底部的 console.log(工作(“已经从地毯中提取了加仑的水。”));我无法理解“从地毯中抽取了加仑的水”。出现在同一行代码中。
// Global variable
var waterGallons = 40
var work = function(total) {
var water = 0;
while (water < waterGallons) {
console.log("The carpet company was called out to extract " + water + " gallons of water.")
water+=4;
};
return waterGallons;
}
console.log (work(" gallons of water has been extracted from the carpet."));
So using the answers that i got for help with is what i came out with because i need to use a global variable. so my story will change by changing the global variable.
因此,使用我寻求帮助的答案是我得出的结论,因为我需要使用全局变量。所以我的故事会通过改变全局变量而改变。
var total = 40
var work = function(total) {
var water = 0;
while (water < total) {
console.log("The carpet company was called out to extract " + water + " gallons of water.")
water+=4;
};
return water;
}
console.log (work(total) + " gallons of water has been extracted from the carpet.");
Id like to thank you guys again. I still have a boolean function, a array using a for loop function, and a procedure left. So using this i should be able to understand how to create the next parts of my assignment.
我想再次感谢你们。我还有一个布尔函数、一个使用 for 循环函数的数组和一个过程。所以使用这个我应该能够理解如何创建我的任务的下一部分。
回答by Wayne
The argument to the function work
corresponds to the formal parameter total
, which is never printed or otherwise used. If you want to print it to the console, then, well, you have to print it to the console.
函数的参数work
对应于形式参数total
,它从不打印或以其他方式使用。如果你想把它打印到控制台,那么,你必须把它打印到控制台。
It's not clear what you're trying to do, but this is one possibility:
目前尚不清楚您要做什么,但这是一种可能性:
var waterGallons = 40;
var work = function() {
var water = 0;
while (water < waterGallons) {
console.log("The carpet company was called out to extract " +
water + " gallons of water.")
water += 4;
};
return water;
}
console.log(work() + " gallons of water has been extracted from the carpet.");
If you really dowant to pass the string as an argument to work
and write it to the console insidethat function, then use console.log(total)
. Just remember to keep the total
parameter that I removed in my example.
如果你真的想字符串作为参数传递给work
,并将其写入到控制台里面那个功能,然后使用console.log(total)
。请记住保留total
我在示例中删除的参数。
回答by biril
And another version (guess) based on lwburk 's previous answer:
另一个版本(猜测)基于 lwburk 的先前答案:
var work = function(total) {
var water = 0;
while (water < total) {
console.log("The carpet company was called out to extract " +
water + " gallons of water.")
water += 4;
};
return water;
}
console.log (work(40) + " gallons of water has been extracted from the carpet.");
This will allow the callee to define the total gallons of water that should be extracted, using the 'total' argument.
这将允许被调用者使用 'total' 参数定义应该提取的总加仑水量。