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

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

如何檢查 if 條件用戶輸入的單詞是否存在于分配給變量的數組中

如何檢查 if 條件用戶輸入的單詞是否存在于分配給變量的數組中

夢里花落0921 2023-11-11 16:06:56
請參閱評論后的第2行。這個問題在代碼注釋中有提到。//valid input to be taken by uservar validanswerone = ["yes" , "YES", "Yes"];//here i want to take multiple form of input the user might type but my efforts are going effort less.var validanswertwo = "no";//only this is working.//conditionsif (userage === validanswerone){  console.log("\n\nPlease put your credit card details in the next page" + ", you will be redirected soon!");}else if(userage === validanswertwo){  console.log("\n\nWe are sorry!");  console.log("\n\nPlease try back in" + "  *days remaining to be become 18+ will be added in the coming update still not understood the formula to implicate it*");}else{  console.log("\n\nInvalid input!")}
查看完整描述

4 回答

?
慕田峪4524236

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

validanswerone是一個數組,您應該使用以下命令檢查它Array#includes

if?(validanswerone.includes(userage))?{
???...
}


查看完整回答
反對 回復 2023-11-11
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

這是問題的解決方案。通過使用將所有輸入轉換為大寫或小寫


var age = userage.toUpperCase();

我能夠以任何格式輸入輸入,該輸入會自動轉換為我的條件,在本例中為大寫。


請有人幫助我正確地重新表述問題并發布答案,以便需要此答案的人能夠輕松找到它。


var readlineSync = require('readline-sync');

//question asked to the user

var userage = readlineSync.question("Are you above the age of 18 for the purchase of the tickets \n(*as of DEC-2020) " + " : ");

var age = userage.toUpperCase();



//valid input to be taken by user

var validanswerone = "YES";

var validanswertwo = "NO";


//conditions

if (age === validanswerone){

  console.log("\n\nPlease put your credit card details in the next page" + ", you will be redirected soon!");

}

else if(age === validanswertwo){

  console.log("\n\nWe are sorry!");

  console.log("\n\nPlease try back in" + "  *days remaining to be become 18+ will be added in the coming update still not understood the formula to implicate it*");

}

else{

  console.log("\n\nInvalid input!")

}



//final-output

console.log("\n\nThank you, you will be redirected soon!")

希望有一天這能解決某人的一些問題!


查看完整回答
反對 回復 2023-11-11
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

如果數組僅包含字符串值,并且數組包含類似 [ {id:1,name:"Aditya"} ] 的對象,則可以使用Array.includes() 。那么您應該使用Array.some()因為它在迭代數組中的事件元素時應用比較邏輯。



查看完整回答
反對 回復 2023-11-11
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

您可以使用 JavaScript 中的 include 函數,無論數組中是否存在用戶,它都可以為您提供輸出!像這樣“validanswerone.includes(userage)”,更好的方法是如果您僅為多種形式的 YES 或 NO 創建一個數組(您可以使用 .tolower 或 .toCapital 函數檢查它)



查看完整回答
反對 回復 2023-11-11
  • 4 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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