0

Im studying JavaScript,

I can make variables, and they work. However when I place them in side a function they dont work..? why? I recieve the answer ( 10 0 undefined)

var totalPeople = 3;
var totalApples = 30;

function tree(totalApples, totalPeople) {
  const shareApples = totalApples / totalPeople;
  const moduloApples = totalApples % totalPeople;

  const output = shareApples;

  console.log(output + " " + moduloApples);
}

console.log(tree(totalApples, totalPeople));
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Define "not work". What do you expect to happen and what actually happens? – deceze Apr 17 '20 at 12:44
  • 1
    The arguments are shadowing the outer variables, so `totalApples` etc values will be determined by how the function is called, not by the value in the outer scope – CertainPerformance Apr 17 '20 at 12:44
  • Google JavaScript scope. Basically variables declared inside a function are only known inside the function. Variables declared outside a function can be thought of as global. Make sure you understand passing by value and pass by reference – DCR Apr 17 '20 at 12:45

0 Answers0