javascript - 有人可以解释如何正确编写这个 Javascript 程序吗?
问题描述
我正在找人告诉我应该如何完成这项任务以及我哪里出错了。说明如下:
“在这个长评论下方的这个文件中写下你的练习代码。请确保只使用分配的 freeCodeCamp 课程中涵盖的语法和技术。
(这里引用的资料是 FreeCodeCamp 的 110 节 Javascript 基础课,探索 Var 和 Let 关键字以及 26 节面向对象编程课)
编写一个名为 createMovie 的函数,该函数期望接收三个参数:title、duration 和 quote。这个函数应该返回一个对象。它返回的对象应该具有也称为标题、持续时间和报价的属性。分配给这些属性的值应该是传递给函数的值。此外,createMovie 返回的对象应该有两个方法:
isLongerThan - 一个函数,它接受一个电影对象作为参数,如果电影比作为参数传递给它的电影长,则返回 true,否则返回 false。
logQuote - 将电影对象的 quote 属性的值记录到控制台的函数。
创建一个名为 movies 的变量并为其分配一个数组。该数组应包含通过调用 createMovie 函数创建的六个对象。您应该传递给 createMovie 函数以创建这些对象的值是:
title | duration | line
----------------------------------------------------------------------------
Star Wars | 121 | If there's a bright center to the universe,
| | you're on the planet that it's farthest from.
| |
Pulp Fiction | 154 | Do you know what they call a Quarter Pounder
| | with Cheese in France?
| |
Dirty Dancing | 100 | Nobody puts Baby in a corner.
| |
Forrest Gump | 142 | Life is like a box of chocolates.
| |
The Wizard of Oz | 101 | Lions and tigers and bears, oh my!
| |
Cabaret | 124 | Life is a cabaret, old chum, so come to the
| | cabaret.
编写以下两个函数,它们都使用电影数组来确定返回什么。
getMovieByTitle - 此函数需要一个字符串作为参数,并返回电影数组中的对象,其标题属性等于传递给它的字符串(如果有的话)。
getAverageDuration - 此函数返回数组中所有电影的平均持续时间。
您可以通过在 Chrome 中打开 index.html 并使用控制台来测试您的代码(有关使用控制台的说明,请参见http://jsforcats.com/)。更正打开控制台时看到的任何错误后,您可以运行以下命令并验证输出。
var starWars = getMovieByTitle('星球大战');
var pinchFiction = getMovieByTitle('Pulp Fiction');
纸浆小说.isLongerThan(starWars);
纸浆小说.logQuote();
getAverageDuration(); */"
所以我写的代码是由一些尽可能最好的伪代码组成的。我对此完全陌生,而且我肯定咬得比我能咀嚼的多。任何帮助,将不胜感激。据我所知,这是:
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;
};
我很欣赏这可能完全是垃圾代码,但我希望如果有人可以向我展示光明,它可能会激励我继续编码而不是放弃并继续在酒吧工作
解决方案
很遗憾听到你的挫折。这是代码,如果您有任何问题,请告诉我:
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;
}
推荐阅读
- php - 如何在将数组转换为 jsonobject php 时从 json 对象中删除数组索引?
- python-3.x - 如何在 Django 中提交 302 表单后发送 POST 请求?
- apache-poi - 如何使用 Apache POI 获取 Excel 工作表的当前单元格位置
- excel - 初始化和调用变量
- html - 如何使页脚粘在底部
- dll - 客户端和 DLL 在调用连接方面如何兼容
- kendo-ui - jquery的kendo-grid依赖模块有哪些?
- java - Jmeter>>>每次执行./jmeter命令时都发现错误(./jmeter:/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java/bin/java:未找到)
- reactjs - React Typescript Storybook - 字符串文字类型
- windows - 文件被永久删除后如何恢复