0

This is from my current udacity course. I've tried several answers all seem to fail. I've used dot notation and bracket notation. Even tried to mix them up. Tried to console.log() them. Tried a while loop. I've looked up several pages on here and on MDV that talk about dot/bracket notation. Either there's a registry error or I'm still doing something wrong.

I'm not looking for a flat out answer as much as I am looking for someone to "explain like I'm five" what I am doing wrong and how I can do it correctly.


Reading Nested Objects Write an expression that outputs how to say hello in Portuguese:

const greetings = {
  hello: [{
    english: 'hi',
    french: 'bonjour',
    portuguese: 'oi'
  }],
  goodbye: [{
    english: 'bye',
    french: 'au revoir',
    portuguese: 'tchau'
  }]
};

greetings.hello.portuguese;

greetings['hello']['portuguese'];

greetings['hello'][2];

These are kind of what I figure would be correct, though they aren't accepted.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
  • 1
    Do you know what `[]` and `{}` in such a nested structure signify? `greetings` is an object (due to the `{`…`}`) with the properties `hello` and `goodbye`, so `greetings.hello` will suffice. `greetings.hello` is an array (due to the `[`…`]`) with a _single_ element, so neither `greetings.hello[2]` nor `greetings.hello.portuguese` will exist, only `greetings.hello[0]` will. That’s an object (due to the `{`…`}`) with the properties `english`, `french` and `portuguese`, so you’ll need `greetings.hello[0].portuguese`. – Sebastian Simon Jan 21 '18 at 00:09

3 Answers3

0

You are forgetting that the object that holds the 'portuguese' property is is placed within an array. So you will have to access the array first (in this case, the first and only item of the array) before you can access the portuguese property on the object:

const greetings = {
  hello: [{
    english: 'hi',
    french: 'bonjour',
    portuguese: 'oi'
  }],
  goodbye: [{
    english: 'bye',
    french: 'au revoir',
    portuguese: 'tchau'
  }]
};

console.log(greetings.hello[0].portuguese); // hello

console.log(greetings.goodbye[0]['portuguese']); // goodbye

Now you can use dot/bracket notation to access the property

S.Baah
  • 11
  • 3
0

I had this one:

const greetings = {
  hello: {
    english: 'hi',
    french: 'bonjour',
    portuguese: 'oi'
  },
  goodbye: {
    english: 'bye',
    french: 'au revoir',
    portuguese: 'tchau'
  }
};

If you output as an alert, console.log, document.write, no matter how you want to do the output:

alert(greetings.hello.portuguese);
console.log(greetings.hello.portuguese);

worked for me.

I think this is an error on the part of udacity.

Acorn
  • 24,970
  • 5
  • 40
  • 69
pat B
  • 1
  • Thanks for your answer @patB. Please see the edit I did to see how you can improve the formatting of your future answers! – Acorn Sep 20 '19 at 19:47
-1

The answer is

greetings.hello.portuguese; greetings['hello']['portuguese'];

User 3637
  • 41
  • 2