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 pany 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 pany 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.
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 pany 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 pany 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.
Share Improve this question edited Jul 13, 2012 at 0:04 Cheesebaron 24.5k15 gold badges69 silver badges121 bronze badges asked Jul 12, 2012 at 1:05 EzcaflowneEzcaflowne 271 gold badge1 silver badge8 bronze badges 4- What are you trying to acplish with this method? It is unclear what exactly you are trying to make it do, considering it returns a global variable that is defined outside of the function. – Austin Commented Jul 12, 2012 at 1:10
-
And your argument
total
was not even used. – Ruel Commented Jul 12, 2012 at 1:17 - its school work, I'm trying to tell a story, this part of my story. I need to follow this flow chart. In this particular part i need to have a number argument-> number function -> local variables -> while loop true -> math -> output -> back to while loop, or false off while loop -> return number. I'm just learning about functions and I'm pretty sure i don't know what I'm doing... :/ – Ezcaflowne Commented Jul 12, 2012 at 1:35
- Thank you lwburk and biril for your answers i think this helps out a lot for understanding how creating a function and argument, then calling the argument after "some code is ran" – Ezcaflowne Commented Jul 12, 2012 at 1:49
2 Answers
Reset to default 1The 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 pany 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.
And another version (guess) based on lwburk 's previous answer:
var work = function(total) {
var water = 0;
while (water < total) {
console.log("The carpet pany 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597231a4635188.html
评论列表(0条)