0

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.

4
  • What are you trying to accomplish 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. Commented Jul 12, 2012 at 1:10
  • And your argument total was not even used. 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... :/ 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" Commented Jul 12, 2012 at 1:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.