javascripting icon indicating copy to clipboard operation
javascripting copied to clipboard

scope problem should be updated to ES2015

Open rshpeley opened this issue 5 years ago • 4 comments

The scope exercise defines scope in javascript before the ES2015 standard. Being 5 years later the ES2015 is probably the most used javascript now.

  1. enter javascripting
  2. select SCOPE
  3. you can see that only global and local scopes are referenced

Include block scope. Rename local scope to function scope.

Usage is defined at these two links from w3schools for let and const,

rshpeley avatar Feb 05 '20 07:02 rshpeley

I agree.

However, including the block scope feels a bit difficult. I would like to reduce the description of function scope and instead compare global scope with block scope.

ledsun avatar Feb 05 '20 10:02 ledsun

Yeah, having taught this to others I think removing function scope is okay for what javascripting is trying to achieve. We don't need to cover every part of JS and const & let are the most commonly used today IMO. Maybe a reference to var in the lesson so when people on their learning journey see it it's not a completely unknown.

itzsaga avatar Feb 05 '20 13:02 itzsaga

@ledsun here's a bit of code based on the scope problem to show global and block scoping:

let [a, b, c] = [1, 2, 3]
console.log(`a: ${a}, b: ${b}, c: ${c}`)
       
{
  let [b, c] = [5, 6]
  console.log(`a: ${a}, b: ${b}, c: ${c}`)
  {
    let b = 8
    console.log(`a: ${a}, b: ${b}, c: ${c}`)
    {
      let [a, c] = [7, 9]
      console.log(`a: ${a}, b: ${b}, c: ${c}`)
      {
        let [a, c] = [1, 8]
        console.log(`a: ${a}, b: ${b}, c: ${c}`)
      }
      console.log(`a: ${a}, b: ${b}, c: ${c}`)
    }
    console.log(`a: ${a}, b: ${b}, c: ${c}`)
  }
  console.log(`a: ${a}, b: ${b}, c: ${c}`)
}
console.log(`a: ${a}, b: ${b}, c: ${c}`)

Running node scope.js gives:

a: 1, b: 2, c: 3
a: 1, b: 5, c: 6
a: 1, b: 8, c: 6
a: 7, b: 8, c: 9
a: 1, b: 8, c: 8
a: 7, b: 8, c: 9
a: 1, b: 8, c: 6
a: 1, b: 5, c: 6
a: 1, b: 2, c: 3

Putting this into problem form:

let [a, b, c] = [1, 2, 3]
       
{
  let [b, c] = [5, 6]
  {
    let b = 8
    {
      let [a, c] = [7, 9]
      {
        let [a, c] = [1, 8]
      }
    }
  }
}

Then solution form:

let [a, b, c] = [1, 2, 3]
       
{
  let [b, c] = [5, 6]
  {
    let b = 8
    console.log(`a: ${a}, b: ${b}, c: ${c}`)
    {
      let [a, c] = [7, 9]
      {
        let [a, c] = [1, 8]
      }
    }
  }
}

Gives the proper solution.

a: 1, b: 8, c: 6

Of course the preamble still needs to be reworked.

rshpeley avatar Feb 05 '20 21:02 rshpeley

I am looking at another issue(#125) and wondering if the problem statement @rshpeley presented is too difficult.

ledsun avatar Apr 13 '20 14:04 ledsun