Looks For-In to Me (What Am I Missing?)


#1


https://www.codecademy.com/courses/objects-ii/5/1?curriculum_id=506324b3a7dffd00020bf661#


string
ReferenceError: string is not defined


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

// print hello in the 3 different languages
for (var x in languages) {
    if (console.log(typeof x) === string) {
        console.log(x);
    } else {
        console.log( "Not a Language");
    }
}


#2

Just like it says string is not defined. This if (console.log(typeof x) === string) needs to be this if (console.log(typeof x) === 'string').

Your code would work if you put this at the top.

var string = 'string';

#3

I tried that

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

// print hello in the 3 different languages
for (var x in languages) {
if (console.log(typeof x) === 'string') {
console.log(x);
} else {
console.log( "Not a Language");
}
}

But now it prints

string
Not a Language
string
Not a Language
string
Not a Language
string
Not a Language


#4

Sorry i coded incorrectly here is what i get when I do exactly what you said
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};

// print hello in the 3 different languages
var string = 'string'
for (var x in languages) {
if (console.log(typeof x) === 'string') {
console.log(x);
} else {
console.log( "Not a Language");
}
}

Result:

string
Not a Language
string
Not a Language
string
Not a Language
string
Not a Language


#5

console.log(languages[x])

You are supposed to console.log the values in languages rather than just the variable x.


#6

Guys, i'am printing everything.... i used the same as pyplayer11675 and the corrections of oscartbeamish and still nothing. i'am using this code and still same result... can anyone assist ??

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

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

and also:

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

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

i imagine that a else statment is not necessary cuz the exercise only asks to print 3 languages


#7

I have used this code and i think it's correct...

var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
for (var x in languages) {
if (typeof languages[x] === "string") {
console.log(languages[x]);
} else {
console.log("Not a Language");
}
}


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.