亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何確保 addRating() 方法的輸入介于 1 和 5 之間?

如何確保 addRating() 方法的輸入介于 1 和 5 之間?

寶慕林4294392 2024-01-18 16:38:31
請參閱下面粗體部分。如果有人能幫助我,我將不勝感激。 如何確保 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());
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

您可以拋出這樣的錯誤。


throw new Error("messages");

此外,我建議您用 AND 而不是 OR 來描述有效輸入的范圍 value < 5 && value > 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);


查看完整回答
反對 回復 2024-01-18
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您可以拋出如下錯誤:


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

查看完整回答
反對 回復 2024-01-18
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

你可以用一個提示去老派,然后把它放在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);


查看完整回答
反對 回復 2024-01-18
  • 3 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號