vinesh kumhar
vinesh kumhar

Reputation: 11

Why do several function calls only yield the last result?

This is my bark function using return. When I call it, I am calling it four times but I only get the value of the last function which is bark("lady", 17); and if I use this function with just console.log instead of return, then it will show me all four result.

Why is this happening and why can’t I see the result of all function calls?

function bark(name, weight) {
  var barkVoice;

  if (weight > 20) {
    barkVoice = name + " says WOOF WOOF";
  } else {
    barkVoice = name + " says woof woof";
  }

  return barkVoice;
}

bark("rover", 23);
bark("spot", 13);
bark("spike", 53);
bark("lady", 17);

Upvotes: 0

Views: 97

Answers (1)

axelduch
axelduch

Reputation: 10849

This happens because your environment is probably a console. It usually takes the last expression and prints it.

If you run

console.log(bark("rover", 23));
console.log(bark("spot", 13));
console.log(bark("spike", 53));
console.log(bark("lady", 17));

It should behave as you want it to.

Upvotes: 1

Related Questions