Submissions will no longer be placed on the leaderboard. You may still attempt this problem for practice.

JavaScript ES6 Let

Here's a useful discussion related to the let function and a comparison with the usual var function.

Let

The function let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
This is unlike the var keyword, which defines a variable globally or locally to an entire function regardless of block scope.

Scoping Rules

Variables declared by let have the block, in which they are defined, as their scope, as well as any contained subblocks.

SAMPLE CODE

function varScope() {
  var num1 = 31;
  if (num1 % 2 == 1) {
    var num1 = 32;  // same variable, num.
    console.log(num1);  // 32
  }
  console.log(num1);  // 32
}

function letScope() {
  let num2 = 31;
  if (num2 % 2 == 1) {
    let num2 = 32;  // different variable, num.
    console.log(num2);  // 32
  }
  console.log(num2);  // 31
}

console.log("Output using varScope() :");
varScope();
console.log("");
console.log("Output using letScope() :");
letScope();

OUTPUT

Output using varScope() :
32
32

Output using letScope() :
32
31

Errors with let

Redeclaring the same variable within the same function or block scope raises an Error.

SAMPLE CODE

let a = 1;
let a = 2;

OUTPUT

Identifier 'a' has already been declared

Task

We have declared a global variable index. Your task is to edit the given code so that the value of the global variable doesn't change.

Line: 1 Col: 1
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score