0

I spent a long time trying to complete this function only to see that the syntax i was trying was not being accepted.

The countSheep function in code academy tells you to complete the function and gives you a newNumber variable that seems to not be defined in the local scope. So I tried to give it the "var" keyword. For some reason I can't understand the var keyword was not necessary and in order to complete the function and get it to pass the test I had to use the following:

as opposed to defining the variable I just used newNumber = number -1; // can also be written as newNumber -= 1; passed newNumber to the function

OR

not defined the newNumber variable and just invoke the function using n-1 as the parameter.

Here is the code that code academy gave us to solve.

function countSheep(number) {
  if (number === 0) {
    console.log("Zzzzzz");// Put your base case here
  } else {
	console.log("Another sheep jumps over the fence.");
	// Define the variable newNumber as 
	// 1 less than the input variable number
	newNumber = number - 1;
	// Recursively call the function
	// with newNumber as the parameter
	countSheep(newNumber);
  }
}

Can someone please tell me why the var keyword is not necessary inside of the function to define the newNumber variable. I appreciate it.

6
  • can also be written as newNumber -= 1 - no it can't Commented Jul 30, 2016 at 3:43
  • 4
    you can just do countSheep(number -1); and forget newNumber altogether Commented Jul 30, 2016 at 3:44
  • to answer your question, without the var it's equivalent to creating a global variable, i.e. like window.newNumber = number - 1 Commented Jul 30, 2016 at 3:45
  • Also note that omitting the var token is forbidden in strict-mode javascript. Commented Jul 30, 2016 at 4:47
  • Thanks for clarifying this. Why would we need to create a global variable at this point? We don't need any other function to access the code we just need countSheep to access the variable when its running. Commented Jul 30, 2016 at 18:00

2 Answers 2

2

if you declare newNumber using var it is only accessable on the scope of else block

.But if you don`t use var it will not be local,means can be accessed on the outer scope**(countSheep).**

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

2 Comments

in most browsers, that's almost correct - I believe declaring with var makes the variable function scoped, not block scoped ... using let instead of var would make it block scoped as you say ... not using either will make it global scoped, not just "outer" .. equivalent to window.newNumber = ...
Are you citing anything? If no, then don't use blockquotes, if yes then please attribute the source.
1

newNumber is a global variable that means, it is assigned to the global object. In browsers this is the window object:

function f(x) {
  y = x;
}

console.log(window.y);
f(123);
console.log(window.y);

To create a local variable that is only accessible within the function, use var:

function f(x) {
  var y = x;
  console.log("within f:", y);
}

f(123);
console.log("outside f:", window.y);

To create a local variable that is only accessible within its surrounding block, use let or const:

function f(x) {
  {
    let y = x;
    console.log("inside block", y);
  }

  try {y} catch (e) {console.log("outside block:", e.message)}
}

f(123);

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.