1

I solved Challenge 16 with no problem. I also found an answer for Challenge 17 but I don't understand why I can only use square brackets for adding property into an object like this: loopNumbers[nestedArr[i][0]] = nestedArr[i][1]

So, my questions are:

  1. Why can't I use dot notation like this?: loopNumbers.nestedArr[i][0] = nestedArr[i][1]
    If I use dot notation, then it says Type Error on line 120: Cannot read property '0' of undefined
  2. How come I don't need to put semicolon after the line of code loopNumbers[nestedArr[i][0]] = nestedArr[i][1]?

Below are the challenges.


Challenge 16

You are provided with an empty array called nestedArr. Using a for loop, add 5 sub-arrays to nestedArr, with each nested array containing the string 'loop' concatenated with the corresponding index in nestedArr as it's first element, and just the index as it's second element. Example of a subarray - ['loop3', 3]

My answer:

let nestedArr = [];
for(let i=0; i<5; i++){
  nestedArr.push(['loop'+i, i]);
}
console.log(nestedArr);

Challenge 17

Create a variable called loopNumbers and initialize it to an empty object literal. Using a for loop, iterate through nestedArr from the previous challenge. For each iteration of your loop, assign a new property to loopNumbers where the property name is the first element in each nested array in nestedArr and the value is the second element.

My answer:

let loopNumbers = {};
for (let i = 0; i < nestedArr.length; i++) {
       loopNumbers[nestedArr[i][0]] = nestedArr[i][1]
   }
console.log(loopNumbers);
sgt.ahn
  • 99
  • 1
  • 10
  • 1
    semi colon line breaks are optional in javascript due to automatic semi colon insertion with caveat that there are some code sequences that will break if they aren't used. Best practice is always use them – charlietfl May 08 '18 at 22:11
  • `loopNumbers.nestedArr` means "access property with name `nestedArr` on `loopNumbner`". That's not what you want so you should not do it. Consider a similar problem with math operators. `1 + 2 * 3` is different than `(1 + 2) * 3`. – Felix Kling May 08 '18 at 22:16

2 Answers2

0

Semicolons are optional in javascript but it's recommended to always add them in order to avoid ambiguities on several cases (see this post).

For adding a property, you can use dot if the argument is static so something like :

loopnumbers.myproperty = 3

But as nestedArr[i][0] is not static, you need to put the brackets.

fazega
  • 327
  • 2
  • 13
  • Thank you fazega! How do I know the argument is static of not in JavaScript?? I know in Java, I used like `static int num = 3;` – sgt.ahn May 08 '18 at 22:17
  • By 'static' I mean something that is not interpreted, that is to say a word. You can't do loopnumbers.variable = something. – fazega May 08 '18 at 22:20
0

EDIT: several times I use "get" when it's more of "access" or "consider". So yes, I'm saying "get this thing" and I'm not necessarily meaning read it's value, but instead "we're doing something here". Just roll with it ok?

In js, arrays are accessed as "get entry at index of array":

array[index]

and objects are accessed as "get property named property from object":

object.property
object["property"]

what you're trying to do is "get property from object named by entry index of array". That's something you CAN do. But what you actually did was ask for something a little different.

loopNumbers.nestedArr[i][0] means "get property nestedArr from object loopNumbers and from that get entry at index i and from that get entry at index 0.

to get the property of loopNumbers that is named by the 0th entry of the ith entry of nestedArr, you have to start by getting the ith entry of nestedArr

nestedArr[i]

And then you want entry 0

nestedArr[i][0]

Now this is not a property you can just use directly because as said before loopNumbers.nestedArr tries to get a property called nestedArr from the object loopNumbers, which does not exist. The only way to get the property named by the value stored in nestedArr[i][0] from the object called loopNumbers is with square brackets as

loopNumbers[nestedArr[i][0]]
ayemossum
  • 81
  • 5