3 Replies - 598 Views - Last Post: 10 September 2013 - 09:55 AM

#1 dmoeller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-April 13

Difficulty in exercise from Codecademy

Posted 09 September 2013 - 05:11 AM

var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for (var lan in languages){
    if(typeof lan === "string"){
        console.log(lan);
    }
}


everything above "// print hello in the 3 different languages" is automatically entered by the site as the lesson. I can not figure out how to print the value of the variables. my result from this code is :

english
french
notALanguage
spanish

please help.
Note: this exercise is not for homework or office work. just free lesson online.

Is This A Good Question/Topic? 0
  • +

Replies To: Difficulty in exercise from Codecademy

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Difficulty in exercise from Codecademy

Posted 09 September 2013 - 05:31 AM

To access a value in a dictionary, you write dictionary[key].

As you noticed for (var lan in languages) sets lan to be the key (i.e. the name of the language), so write languages[lan] to get the corresponding value.
Was This Post Helpful? 1
  • +
  • -

#3 JoeSimmons  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 10-August 13

Re: Difficulty in exercise from Codecademy

Posted 09 September 2013 - 05:32 PM

console.log( languages[lan] );

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: Difficulty in exercise from Codecademy

Posted 10 September 2013 - 09:55 AM

if your browser supports it (currently only FF) you can also use Harmony’s for … of
for (let hello of languages) {
    console.log(hello);
}

This post has been edited by Dormilich: 10 September 2013 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1