最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

寶慕林4294392 2024-01-18 16:38:31
請參閱下面粗體部分。如果有人能幫助我,我將不勝感激。 如何確保 addRating() 方法的輸入介于 1 和 5 之間?也許有某種方法可以阻止代碼繼續(xù)/處理平均評分,如果其中一個(gè)評分低于 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貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以拋出這樣的錯(cuò)誤。


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


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

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

您可以拋出如下錯(cuò)誤:


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

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

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

你可以用一個(gè)提示去老派,然后把它放在while循環(huán)中。在此答案中,如果傳遞的值正確,則忽略 while 循環(huán)。我注釋了你對我的測試答案的推動(dòng)。


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


查看完整回答
反對 回復(fù) 2024-01-18
  • 3 回答
  • 0 關(guān)注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號