【问题标题】:How do I ensure the input is between 1 and 5 for the addRating() method?如何确保 addRating() 方法的输入在 1 到 5 之间?
【发布时间】:2021-04-01 15:02:08
【问题描述】:

请参阅下面的粗体部分。如果有人可以帮助我,我将不胜感激。 如何确保 addRating() 方法的输入在 1 到 5 之间?如果其中一个评分低于 1 且高于 5,也许有某种方法可以阻止代码继续/处理平均评分。

class Media {

  constructor(title) {
    this._title = title;
    this._isCheckedOut = false;
    this._ratings = [];
  }
  
  get title() {
    return this._title;
  }

  get isCheckedOut() {
    return this._isCheckedOut;
  }

  get ratings() {
    return this._ratings;
  }

  set isCheckedOut(status) {
    this._isCheckedOut = status;
  }

  toggleCheckOutStatus() {
    this.isCheckedOut = !this.isCheckedOut;
  }

  getAverageRating() {
    let ratingsSum = this.ratings.reduce((accumulator, currentRating) =>
    accumulator + currentRating);
    return ratingsSum/this.ratings.length;
  }

  // Here is the mentioned function
  // look inside addRating()
  addRating(newRating) {
    if (newRating < 1 || newRating > 5) {
      console.log("Invalid Input. Please enter an integer between 1- 
  5.");
      } else {
      this.ratings.push(newRating);
    }
  }
}

class Book extends Media {
  constructor(author, title, pages) {
    super(title);
    this._author = author;
    this._pages = pages;
  }
  get author() {
    return this._author;
  }
   get pages() {
    return this._pages;
  }
}

class Movie extends Media {
  constructor(director, title, runTime) {
    super(title);
    this._director = director;
    this._runTime = runTime;
  }
  get director() {
    return this._director;
  }
  get runTime() {
    return this._runTime;
  }
}

const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);

historyOfEverything.toggleCheckOutStatus();

console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);

console.log(historyOfEverything.getAverageRating());

const speed = new Movie('Jan de Bont', 'Speed', 116);

speed.toggleCheckOutStatus();

console.log(speed.isCheckedOut);

speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

console.log(speed.getAverageRating());

【问题讨论】:

  • 你可以抛出无效输入之类的错误
  • 我该怎么做?

标签: javascript class methods


【解决方案1】:

你可以像这样抛出错误:

function addRating(newRating) {
  if (newRating < 1 || newRating > 5) {
    throw "Invalid Input. Please enter an integer between 1-5.";
  } else {
    this.ratings.push(newRating);
  }
}
addRating(7);

有关 throw 的更多信息,请参阅 Mozilla 文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

【讨论】:

    【解决方案2】:

    你可以像这样抛出一个错误。

    throw new Error("messages");
    

    另外,我建议您使用 AND 而不是 OR 来描述有效输入的范围 value &lt; 5 &amp;&amp; value &gt; 1

    function addRating(value) {
      if (value < 5 && value > 1) {
        console.log("Everything is okay");
      } else {
        throw new Error("Invalid Input. Please enter an integer between 1-5.");
      }
    }
    
    addRating(12);

    【讨论】:

      【解决方案3】:

      您可以在提示符下进入老学校并将其放入 while 循环中。在此答案中,如果传递的值正确,则忽略 while 循环。我评论了您对我的测试答案的推动。

      function addRating(newRating) {
         while(newRating < 1 || newRating > 5){
           newRating = Number(prompt("Invalid Input. Please enter an integer between 1-5."));
        }
        
        //this.ratings.push(newRating);
      }
      
      
      addRating(41);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-09
        • 2015-02-08
        • 1970-01-01
        相关资源
        最近更新 更多