Reputation: 27
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.
// 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.
Upvotes: 0
Views: 13130
Reputation: 60414
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.
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 do want to pass the string as an argument to work
and write it to the console inside that function, then use console.log(total)
. Just remember to keep the total
parameter that I removed in my example.
Upvotes: 1
Reputation: 1995
And another version (guess) based on lwburk 's previous answer:
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.
Upvotes: 0