2016-08-04 2 views
0
// Create movie DB 
var movies = [ 
    { 
     title: "Avengers", 
     hasWatched: true, 
     rating: 5 
    }, 
    { 
     title: "SpiderMan", 
     hasWatched: false, 
     rating: 4 

     }, 
    { 
     title: "LightsOut", 
     hasWatched: true, 
     rating: 6 
    } 
] 

// Print it out 
movies 

// Print out all of them 
movies.forEach(function(movie)){ 
    var result = "You have "; 
    if(movie.hasWatched){ 
     result += "watched "; 
}else{ 
     result += "not seen "; 
} 

    result += "\" + movie.title + "\"-"; 
    result += movie.rating + " stars"; 
    console.log(result); 
} 

私の最初のオブジェクトが真の理由が、コンソールのプリントアウトここGoogleのコンソールでJavascriptを

+0

みんなありがとうその私のせい! – Simon

答えて

0

あなたはこのラインで構文エラーがあった:

result += "\" + movie.title + "\"-";

をそれがで固定する必要があります。

JavaScripで

result += '"' + movie.title + '"- ';

二重引用符と一重引用符の両方を使用することができます。二重引用符(")を印刷する場合は、一重引用符(例:'"')で「囲む」ことができます。

二重引用符を使用するときの興味深い議論は、hereです。

var movies = [{ 
 
    title: "Avengers", 
 
    hasWatched: true, 
 
    rating: 5 
 
}, { 
 
    title: "SpiderMan", 
 
    hasWatched: false, 
 
    rating: 4 
 

 
}, { 
 
    title: "LightsOut", 
 
    hasWatched: true, 
 
    rating: 6 
 
}] 
 

 
// Print it out 
 
console.log(movies); 
 

 
// Print out all of them 
 
movies.forEach(function(movie) { 
 
    var result = "You have "; 
 
    if (movie.hasWatched) { 
 
    result += "watched "; 
 
    } else { 
 
    result += "not seen "; 
 
    } 
 
    result += '"' + movie.title + '"- '; 
 
    result += movie.rating + " stars"; 
 
    console.log(result); 
 
});

0

が更新されたコードです。いくつかの構文エラーを修正しました!

var movies = [{ 
 
    title: "Avengers", 
 
    hasWatched: true, 
 
    rating: 5 
 
}, { 
 
    title: "SpiderMan", 
 
    hasWatched: false, 
 
    rating: 4 
 

 
}, { 
 
    title: "LightsOut", 
 
    hasWatched: true, 
 
    rating: 6 
 
}] 
 

 
// Print it out 
 
console.log(movies); 
 

 
// Print out all of them 
 
movies.forEach(function(movie) { 
 
    var result = "You have "; 
 
    if (movie.hasWatched) { 
 
    result += "watched "; 
 
    } else { 
 
    result += "not seen "; 
 
    } 
 

 
    result += '"' + movie.title + '" - '; 
 
    result += movie.rating + " stars"; 
 
    console.log(result); 
 
});

関連する問題