有人能解释一下如何正确编写这个Javascript程序吗?
我正在寻找一个人来告诉我如何完成这项任务,我哪里做错了。说明如下: “在此长注释下方的文件中编写此练习的代码。 请确保只使用指定文档中包含的语法和技术 免费课程 (这里引用的材料是FreeCodeCamp基础Javascript的110课,探索Var和Let关键字,以及26节面向对象编程课)有人能解释一下如何正确编写这个Javascript程序吗?,javascript,Javascript,我正在寻找一个人来告诉我如何完成这项任务,我哪里做错了。说明如下: “在此长注释下方的文件中编写此练习的代码。 请确保只使用指定文档中包含的语法和技术 免费课程 (这里引用的材料是FreeCodeCamp基础Javascript的110课,探索Var和Let关键字,以及26节面向对象编程课) 编写一个名为createMovie的函数,该函数预期接收三个参数: 标题、持续时间和引号。此函数应返回一个对象。对象 它返回的属性也应命名为title、duration和duration 引号。分配给这些属
var Movies = [];
function CreateMovie (id, title, duration, quote) {
let Films = {
title: title,
duration: duration,
quote: quote,
isLongerThan: function (Movies) {
for (var x = 0; x < Movies.length; x++) {
for (var y = 0; y < Movies[x].length; y++) {
if (This.duration > Movies[x][y].duration) {
return true;
} else {
return false;
}
}
}
},
logQuote: function (title){
for (var x = 0; x < Movies.length; x++) {
for (var y = 0; y < Movies[x].length; y++){
if (Movies[x][y].hasOwnProperty(title)){
console.log(Movies[x][y].quote)
}
}
}
}
};
Movies.push(Films);
return Films;
};
function getMovieByTitle (title) {
for (var x = 0; x < Movies.length; x++) {
for (var y = 0; y < Movies[x].length; y++) {
if (title === Movies[x][y].title) {
return Movies[x];
} else {
return undefined;
}
};
function getAverageDuration () {
var totalMovies = [];
for (var i = 0; i < Movies.length; i++) {
totalMovies.push[i];
}
var durationTotal = 0;
for (var x = 0; x < Movies.length; x++) {
durationTotal += (Movies[x][2]) ;
}
var totalAvg = (durationTotal / totalMovies.length);
return totalAvg;
};
var电影=[];
函数CreateMovie(id、标题、持续时间、引用){
让电影={
标题:标题,,
持续时间:持续时间,
quote:quote,
isLongerThan:功能(电影){
对于(var x=0;xMovies[x][y].duration){
返回true;
}否则{
返回false;
}
}
}
},
日志:功能(标题){
对于(var x=0;x
我明白这可能完全是垃圾代码,但我希望如果有人能告诉我,这可能会激励我继续编码,而不是放弃,永远在酒吧里工作很抱歉听到你的挫折。这是代码,如果你有问题,请告诉我:
class Movie {
constructor(title, duration, quote) {
this.title = title;
this.duration = duration;
this.quote = quote;
}
isLongerThan(other) {
return this.duration > other.duration;
}
logQuote() {
console.log(this.quote);
}
}
function createMovie(title, duration, quote) {
return new Movie(title, duration, quote);
}
function getMovieByTitle(movies, title) {
for (let m of movies)
if (m.title === title)
return m;
}
function getAverageDuration(movies) {
let total = 0;
for (let m of movies)
total += m.duration;
return total / movies.length;
}
困在一段代码上太糟糕了。我们都去过那里。
这就是我执行你的任务的方式 就像@georg。如果你有任何问题,请告诉我们
class Movie {
constructor(title, duration, quote) {
this.title = title;
this.duration = duration;
this.quote = quote;
}
isLongerThan(movie) {
return this.movie.duration > movie.duration;
}
logQuote() {
console.log(this.quote);
}
}
const movies = [];
function createMovie(title, duration, quote) {
let movie = new Movie(title, duration, quote);
movies.push(movie);
}
function getMovieByTitle(title) {
return movies.find(movie => movie.title === title);
}
function getAverageDuration() {
return movies.reduce(accumulator, movie => {
return accumulator + movie.duration;
}, 0) / movies.length;
}
这是一个最简单的版本,没有使用任何类,也没有使用您尚未熟悉的函数。我已经编写了一个简单的循环解决方案。您可以使用
class
和map
函数编写相同的内容,如其他答案所示
let movies = [];
/**
* Creates a new movie object and adds the object to
* the movies array.
*
* Returns the newly created movies object.
*/
function createMovie(title, duration, quote) {
let movie = {
title: title,
duration: duration,
quote: quote,
isLongerThan: function (other_movie) {
return this.duration > other_movie.duration;
},
logQuote: function () {
console.log(this.quote);
}
}
movies.push(movie);
return movie;
}
/**
* Searches the movies array for matching title and returns
* the movie object if a match is found. Returns "undefined"
* if no atch is found.
*
* @param string title
*/
function getMovieByTitle(title) {
for (let i = 0; i < movies.length; i++) {
let movie = movies[i];
if (movie.title === title) {
return movie;
}
}
}
/**
* Gets the average duration of all the movies using a simple
* for loop.
*/
function getAverageDuration() {
let total_duration = 0;
let average_duration = 0;
if (movies.length > 0) {
// Iterate through the movies, if movies array
// is not empty. If we don't do this check, the average
// duration could result in an NaN result (division by 0).
for (let i = 0; i < movies.length; i++) {
let movie = movies[i];
total_duration += isNan(movie.duration) ? 0 : movie.duration;
}
// Rounds the average to two decimal places.
average_duration = (total_duration / movies.length).toFixed(2);
}
return average_duration;
}
let movies=[];
/**
*创建新的电影对象并将该对象添加到
*电影阵列。
*
*返回新创建的电影对象。
*/
函数createMovie(标题、持续时间、引用){
让电影={
标题:标题,,
持续时间:持续时间,
quote:quote,
isLongerThan:函数(其他电影){
返回this.duration>other_movie.duration;
},
logQuote:function(){
console.log(this.quote);
}
}
电影。推送(电影);
回归电影;
}
/**
*在电影数组中搜索匹配的标题并返回
*如果找到匹配项,则返回电影对象。返回“未定义”
*如果没有找到atch。
*
*@param字符串标题
*/
函数getMovieByTitle(标题){
for(设i=0;i0){
//迭代电影,如果电影数组
//不是空的。如果我们不做这个检查,平均
/
let movies = [];
/**
* Creates a new movie object and adds the object to
* the movies array.
*
* Returns the newly created movies object.
*/
function createMovie(title, duration, quote) {
let movie = {
title: title,
duration: duration,
quote: quote,
isLongerThan: function (other_movie) {
return this.duration > other_movie.duration;
},
logQuote: function () {
console.log(this.quote);
}
}
movies.push(movie);
return movie;
}
/**
* Searches the movies array for matching title and returns
* the movie object if a match is found. Returns "undefined"
* if no atch is found.
*
* @param string title
*/
function getMovieByTitle(title) {
for (let i = 0; i < movies.length; i++) {
let movie = movies[i];
if (movie.title === title) {
return movie;
}
}
}
/**
* Gets the average duration of all the movies using a simple
* for loop.
*/
function getAverageDuration() {
let total_duration = 0;
let average_duration = 0;
if (movies.length > 0) {
// Iterate through the movies, if movies array
// is not empty. If we don't do this check, the average
// duration could result in an NaN result (division by 0).
for (let i = 0; i < movies.length; i++) {
let movie = movies[i];
total_duration += isNan(movie.duration) ? 0 : movie.duration;
}
// Rounds the average to two decimal places.
average_duration = (total_duration / movies.length).toFixed(2);
}
return average_duration;
}