3

I am developing this simple application which suppose to execute one function at a time. This code is written in javascript with the help of nodejs.

My Goal is to make function one to display the output first then only function two will display the output but both of this function must be run with one big function.

Here are my codes.

function combine(Human,Alien)
{
  function one (Human)
  {
    TotalOne = Human + 5;
    console.log(`The Number Of Human : ${TotalOne}`);
    return TotalOne;
  }
  function two (Alien)
  {
    TotalTwo = Alien + 10;
    console.log(`The Number Of Alien : ${TotalTwo}`);
    return TotalTwo;
  }
}
combine(1,3);

The output does not seems to be reading the function inside the big function. Any ideas on how to solve this ?

Thanks :)

4 Answers 4

4

You forgot to call the inner functions. Try this:

function combine(Human,Alien)
{
  function one (Human)
  {
    TotalOne = Human + 5;
    console.log(`The Number Of Human : ${TotalOne}`);
    return TotalOne;
  }
  function two (Alien)
  {
    TotalTwo = Alien + 10;
    console.log(`The Number Of Alien : ${TotalTwo}`);
    return TotalTwo;
  }
    one(Human);
    two(Alien);
}
combine(1,3);
Sign up to request clarification or add additional context in comments.

3 Comments

Just To Add : How do i make it delay ? like the output of function one will be display first and maybe after few minutes the output of the function two will be display. is there a way to make the output in the big function delay while running the big function ?
Wrap the call to the two(Alien) in a SetTimeout, like this setTimeout(() => two(Alien), 1000). See more here: w3schools.com/jsref/met_win_settimeout.asp
Or actually, even simpler: setTimeout(two, 1000, Alien);
2

You are not invoking the function one and two inside the combine function

    function combine(Human,Alien)
    {
      function one (Human)
      {
        TotalOne = Human + 5;
        console.log(`The Number Of Human : ${TotalOne}`);
        return TotalOne;
      }
      function two (Alien)
      {
        TotalTwo = Alien + 10;
        console.log(`The Number Of Alien : ${TotalTwo}`);
        return TotalTwo;
      }
      one(Human); //you missed the function invocation of one function
      two(Alien); //you missed the function invocation  of two function
    }
    combine(1,3)

Comments

2

You have one big function and two function inside. Those are function declarations. Last line is function call. This call executes big function, which in it's body declares the two inner functions. But you forgot to call those function inside big functions body.

Comments

2

If you do not want to make your 'combine' function heavy then please take the implementation of the function 'one' and 'two' outside of its body. This will make the program more modularised and easy to understand.

function one (Human){
    TotalOne = Human + 5;
    console.log(`The Number Of Human : ${TotalOne}`);
    //return TotalOne; you do not need the return statement as your are    printing the output on the console already.
}

function two (Alien){
    TotalTwo = Alien + 10;
    console.log(`The Number Of Alien : ${TotalTwo}`);
    //return TotalTwo;you do not need the return statement as your are    printing the output on the console already.
}

function combine(Human,Alien) {
    one(Human);
    two(Alien);
}
combine(1,3);

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.