Group Chat Week 3 Solutions

Well done if you got these right!

We’re using the assignment operator (=), rather than the equality/identity operator (==/===) here, so this code tries to insert the value of "Adam" into the name variable.

Since we declared name as a constant, this throws an error. If we defined it instead with let, then this would actually work! Can you figure out why?

Hint: try console.log()ing name = ‘Adam’ - what does it evaluate to?

  • What output does this code give when we run the function runCode()?

This gives us the answer of A1:

  • (1 + 2) evaluates to 3. Since we’re using it in a boolean context (inside the if condition), this evaluates to true (any number except one of the “falsey” values converts to true).
  • (1 + 2) && true => true && true. This evaluates to true, since true AND true are both true.
  • The STRING of ‘0’ evaluates to true, so we console.log “Answer: A1”.

If num was instead the NUMBER 0, then it would evaluate to false, and we’d get A2 instead.

For loops always follow the same pattern:

  • Initialization: create a counter variable, giving it an initial value - let i = 25
  • Comparison: if this is true, then run the block of code - i >= 0
  • Update: reassign the value of the counter after each run - i = i - 5

First time, we have i = 25. i is greater than 0, so we print 25. Second time, i updates to 20. i is greater than 0, so we print 20… After 5 runs, the value is 0. 0 is greater than or equal to 0, so we get one last print out of 0. After that, i takes the value of -5, which is less than 0, so the for loop stops.

  • What will each of the throwPokeball() functions output in this code?

    • PIKAAA PIKAAA! - Pikachu is the first item in the array, at index 0.
    • Roooar! - We reassigned Charmander to null on line 8, and null falls under the ‘default’ case in our switch statement.
    • Roooar! - We reassigned Squirtle to Pidgey on line 8, so this also falls under the ‘default’ case in our switch statement.
    • Roooar! - Bulbasaur was always at the last position in the array, and this falls under default too!
  • What would you write at the end of this code to get Pikachu to attack with Thunderbolt?

    • You can get Pikachu to speak with pikachu.speak()
    • You can get Pikachu to attack with Thunderbolt with pikachu.attack(pickachu.moves[1])

These are both object methods, which are similar to functions.