4 回答

TA貢獻1875條經驗 獲得超5個贊
validanswerone
是一個數組,您應該使用以下命令檢查它Array#includes
:
if?(validanswerone.includes(userage))?{ ???... }

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!")
希望有一天這能解決某人的一些問題!

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

TA貢獻1824條經驗 獲得超6個贊
您可以使用 JavaScript 中的 include 函數,無論數組中是否存在用戶,它都可以為您提供輸出!像這樣“validanswerone.includes(userage)”,更好的方法是如果您僅為多種形式的 YES 或 NO 創建一個數組(您可以使用 .tolower 或 .toCapital 函數檢查它)
添加回答
舉報