如何确保 addRating() 方法的输入介于 1 和 5 之间?

请参阅下面粗体部分。如果有人能帮助我,我将不胜感激。 如何确保 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());


宝慕林4294392
浏览 114回答 3
3回答

一只甜甜圈

您可以抛出这样的错误。throw new Error("messages");此外,我建议您用 AND 而不是 OR 来描述有效输入的范围 value < 5 && value > 1function addRating(value) {&nbsp; if (value < 5 && value > 1) {&nbsp; &nbsp; console.log("Everything is okay");&nbsp; } else {&nbsp; &nbsp; throw new Error("Invalid Input. Please enter an integer between 1-5.");&nbsp; }}addRating(12);

慕容森

您可以抛出如下错误: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);

蝴蝶刀刀

你可以用一个提示去老派,然后把它放在while循环中。在此答案中,如果传递的值正确,则忽略 while 循环。我注释了你对我的测试答案的推动。function addRating(newRating) {&nbsp; &nbsp;while(newRating < 1 || newRating > 5){&nbsp; &nbsp; &nbsp;newRating = Number(prompt("Invalid Input. Please enter an integer between 1-5."));&nbsp; }&nbsp;&nbsp;&nbsp; //this.ratings.push(newRating);}addRating(41);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript