0

我试图让这个功能在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."));

所以使用我得到帮助的答案就是我想出来的,因为我需要使用一个全局变量。所以我的故事将通过改变全局变量而改变。

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.");

我想再次感谢你们。我还有一个布尔函数,一个使用 for 循环函数的数组,还有一个过程。所以使用这个我应该能够理解如何创建我的作业的下一部分。

4

2 回答 2

1

函数的参数work对应于形式参数total,它从不打印或以其他方式使用。如果要将其打印到控制台,那么,您必须将其打印到控制台。

目前尚不清楚您要做什么,但这是一种可能性:

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.");

如果您确实想将字符串作为参数传递给该函数work的控制台并将其写入控制台,请使用. 请记住保留我在示例中删除的参数。console.log(total)total

于 2012-07-12T01:20:43.607 回答
0

还有一个基于 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.");

这将允许被调用者使用“total”参数定义应该提取的总加仑水。

于 2012-07-12T01:40:17.390 回答