Reputation: 374
For this assignment the object 'languages' was created for you. the assignment is to use a 'for-in' loop to write three different ways to say 'hello.' I want to use an 'if' statement to check that the property is a 'string' and if so, print that version of 'hello' to the console. I am stuck and getting an error stating a "there was a problem with your syntax."
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the 3 different languages
for (var x in obj) {
var typeCheck = languages.x;
if {
(typeof typeCheck === "string") {
console.log(typeCheck;)
}
}
}
Upvotes: 0
Views: 280
Reputation: 374
Using the code suggested by Robin, I've modified it to be the following final solution.
for (var x in languages) {
var typeCheck = languages[x];
if (typeof typeCheck === "string") {
console.log(typeCheck);
}
}
Upvotes: 2
Reputation: 13679
You are looping with an undefined variable obj
. Use the defined one languages
instead.
for (var x in languages) {
var typeCheck = x;
if {
(typeof typeCheck === "string") {
console.log(typeCheck;)
}
}
}
Update
I noticed there was a lot errors in your code.
Your if statement
Which is not how to use it, currently it is :
if {
(typeof typeCheck === "string") {
console.log(typeCheck;)
}
}
Change that to :
if (typeof typeCheck === "string") {
console.log(typeCheck);
}
Then your ending semi colon on the console.log()
, it should be after the ending )
.
So from :
console.log(typeCheck;)
To :
console.log(typeCheck);
Putting them together :
for (var x in languages) {
var typeCheck = x;
if (typeof typeCheck === "string") {
console.log(typeCheck);
}
}
Upvotes: 5
Reputation: 15435
There's a few problems here:
for (var x in obj) {
- This loops on some object obj
which is undefined in this context. You need to loop over languages
. if {
- if statements have the form form of if (condition)
, not what you have there. Here is a correctly version that loops over the enumerable properties of languages
and performs the type check.
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
Object.keys(languages).forEach(function(key) {
if (typeof languages[key] === "string") {
console.log(languages[key]);
}
});
Upvotes: 1