• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Easier way to if/or?

L0L2G00D

New Coder
I am making a game in javascript, and was wondering If there was an easier way to check for multiple answers
JavaScript:
answer = prompt(_Name + "?");
    if(answer === 'y' || answer === 'Y' || answer === 'Yes' || answer === 'yes' || answer === 'Ye' || answer === 'ye' || answer === 'Ya' || answer === 'ya'){
    console.log(something);
    } else {
    console.log(something);
    };
 
Solution
This will allow you to quickly determine if the answer is allowed or not, by forcing the answer to be lowercase, and also trimmed of spaces before or after.

JavaScript:
var answer = "yes";  // correct
// var answer = "incorrect"; // not in valid answers array
var validanswers = ["y", "yes", "ye", "ya"]

if(validanswers.indexOf(answer.toLowerCase().trim()) > -1){

 console.log("this is an accepted answer");

} else {

 console.log("that is not a correct answer");

}

Ghost

Active Coder
This will allow you to quickly determine if the answer is allowed or not, by forcing the answer to be lowercase, and also trimmed of spaces before or after.

JavaScript:
var answer = "yes";  // correct
// var answer = "incorrect"; // not in valid answers array
var validanswers = ["y", "yes", "ye", "ya"]

if(validanswers.indexOf(answer.toLowerCase().trim()) > -1){

 console.log("this is an accepted answer");

} else {

 console.log("that is not a correct answer");

}
 
Solution

L0L2G00D

New Coder
This will allow you to quickly determine if the answer is allowed or not, by forcing the answer to be lowercase, and also trimmed of spaces before or after.

JavaScript:
var answer = "yes";  // correct
// var answer = "incorrect"; // not in valid answers array
var validanswers = ["y", "yes", "ye", "ya"]

if(validanswers.indexOf
[CODE lang="javascript" title="Example"]let answer = prompt("yes?")
const QuestionOne = {
    yes: ["yes","ye","y"]
    no: ["no", "n"]
    maybe: ["ect"]
}
if(QuestionOne.yes.indexOf(answer.toLowerCase().trim()) > -1){))

 console.log("this is an accepted answer");

} else {

 console.log("that is not a correct answer");

}
Would I be able to use objects for Multiple answers
[/CODE]
Code:
let answer = prompt("Is the answer yes?")
const QuestionOne = {
    yes: ["yes","ye","ya", "y"],
    no: ["no", "n"],
    maybe: ["maybe", m]
}
if(QuestionOne.yes.indexOf(answer.toLowerCase().trim()) > -1){
console.log('ok')
} else {
if(QuestionOne.yes.indexOf(answer.toLowerCase().trim())>-1){
console.log('why not')
} else {
if(QuestionOne.yes.indexOf(answer.toLowerCase().trim())>-1){
console.log('answer yes or no')
} else {
console.log('what?')
}
}
}
 

Top