0

I'm practicig in writing some code. In the solution to the exercise I found this line of code (see below), but I can't figure out what does c[num] represent and how it works. Is the solution maybe using brackets notation instead of dot notation?

This would mean that c[num] is the same as saying c.num. Or am I on a completely wrong track?

Line of code:

while(num != 1 && c[num] !== true)

Thank you!

PS. Full solution is here: https://www.w3resource.com/javascript-exercises/javascript-conditional-statements-and-loops-exercise-8.php

user3926863
  • 325
  • 4
  • 13
  • Square brackets [] represents Array in JavaScript. c[num] means value from c array at index num. c = ['a','b','c','d']; num=2; x = c[num]; then x will be 'c' – Nawed Khan Jan 29 '19 at 21:13

1 Answers1

2

c.num is equivalent to c["num"], not c[num]. c[num] provides a way to dynamically look up an attribute based on the value stored in a variable, so it would only be equivalent to c.num if num's value was "num". Given num is clearly supposed to be an integer, that's not the case.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271