首页 > 解决方案 > 有人可以解释如何正确编写这个 Javascript 程序吗?

问题描述

我正在找人告诉我应该如何完成这项任务以及我哪里出错了。说明如下:

“在这个长评论下方的这个文件中写下你的练习代码。请确保只使用分配的 freeCodeCamp 课程中涵盖的语法和技术。

(这里引用的资料是 FreeCodeCamp 的 110 节 Javascript 基础课,探索 Var 和 Let 关键字以及 26 节面向对象编程课)

  1. 编写一个名为 createMovie 的函数,该函数期望接收三个参数:title、duration 和 quote。这个函数应该返回一个对象。它返回的对象应该具有也称为标题、持续时间和报价的属性。分配给这些属性的值应该是传递给函数的值。此外,createMovie 返回的对象应该有两个方法:

    isLongerThan - 一个函数,它接受一个电影对象作为参数,如果电影比作为参数传递给它的电影长,则返回 true,否则返回 false。

    logQuote - 将电影对象的 quote 属性的值记录到控制台的函数。

  2. 创建一个名为 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.
  1. 编写以下两个函数,它们都使用电影数组来确定返回什么。

    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;
};

我很欣赏这可能完全是垃圾代码,但我希望如果有人可以向我展示光明,它可能会激励我继续编码而不是放弃并继续在酒吧工作

标签: javascript

解决方案


很遗憾听到你的挫折。这是代码,如果您有任何问题,请告诉我:

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;
}

推荐阅读