0

Is it possible to nest functions within a main function in JavaScript? I've been playing around with some code, and I can't get my function to display anything to the screen.

function main() {
    var crew;
    var crewMemberWeight = 113;
    var winterWeight = 2.8;
    var winter = false;
    var capacity = 24000;
    var cargo = [1000, 5500, 2000, 3500, 4000, 7000];

    function cargoWeight(cargo) {
        total = 0;
        for(var i = 0; i < cargo.length; i++) {
            total += cargo[i];
        }
        document.write("Cargo weight: " + total);
        return total;
    }
}

main();
5
  • 9
    You're not calling cargoWeight anywhere. Commented Mar 28, 2016 at 21:48
  • Oh, alright. I was thinking that if I called my main function below, the functions within it may be called as well. Commented Mar 28, 2016 at 21:49
  • 1
    Even if that was the case, your function takes an argument of 'load' which isn't defined anywhere so it would fail. Commented Mar 28, 2016 at 21:50
  • Why can't you put cargoWeight(load) outside the main() and call it like you would any other function. IE cargoWeight(2000); Commented Mar 28, 2016 at 21:51
  • Where would you have gotten the idea that merely declaring a function would somehow magically execute it, in any context, inside another function or not? Commented Dec 8, 2016 at 11:34

6 Answers 6

2
function main() {
    var crew;
    var crewMemberWeight = 113;
    var winterWeight = 2.8;
    var winter = false;
    var capacity = 24000;
    var cargo = [1000, 5500, 2000, 3500, 4000, 7000];

    function cargoWeight(cargo) {
        total = 0;
        for(var i = 0; i < cargo.length; i++) {
            total += cargo[i];
        }
        document.write("Cargo weight: " + total);
        return total;
    }

    // you have to call the function and pass in cargo. Not just define it
    cargoWeight(cargo)
}

main();
Sign up to request clarification or add additional context in comments.

Comments

0

You just have to call this internal function inside main(). I would do: cargoWeight(cargo); before last bracket.

Comments

0

You need to call the cargoWeight function at the end. Looking at your code it looks like you want to pass the cargo variable in.

function main() {
var crew;
var crewMemberWeight = 113;
var winterWeight = 2.8;
var winter = false;
var capacity = 24000;
var cargo = [1000, 5500, 2000, 3500, 4000, 7000];

function cargoWeight(load) {
    total = 0;
    for(var i = 0; i < load.length; i++) {
        total += load[i];
    }
    document.write("Cargo weight: " + total);
    return total;
}
cargoWeight(cargo);
}

main();

Comments

0

It is possible to define functions within functions in javascript. However, when you do so, that nested function can only be called from inside the parent function. So, if you wrote another separate function, you wouldn't be able to call cargoWeight() from that other function.

function main() {
    var crew;
    var crewMemberWeight = 113;
    var winterWeight = 2.8;
    var winter = false;
    var capacity = 24000;
    var cargo = [1000, 5500, 2000, 3500, 4000, 7000];

    function cargoWeight(load) {
        total = 0;
        for(var i = 0; i < load.length; i++) {
            total += load[i];
        }
        document.write("Cargo weight: " + total);
        return total;
    }
}

main();

function other(){
    cargoWeight(400)  // can't call this method here
}

There are a lot of applications for this which would require a lengthy post to explain, but looking into javascript objects and closures would be a good place to start in order to understand why and how javascript works this way.

Comments

0

first off you need to call 'cargoWeight(load)' you also need to define 'load' that is going to be pulled into the function.

What i think your intent was to add up the 'cargo' array? see linked fiddle:

www.jsfiddle.net/natLuuy6

Comments

-1

This is late to the party, but may be interesting to a "JavaScript beginner".

function main()
{
    var crew;
    var crewMemberWeight = 113;
    var winterWeight = 2.8;
    var winter = false;
    var capacity = 24000;
    var cargo = [1000, 5500, 2000, 3500, 4000, 7000];

    function cargoWeight(cargoValues) {
        return cargoValues.reduce((a, b) => a + b, 0);
    }

    document.write("Cargo weight: ", cargoWeight(cargo));
}

main();

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.