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

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

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);
添加回答
舉報