Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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

Coder
I am making a game in javascript, and was wondering If there was an easier way to check for multiple answers
[CODE lang="javascript" highlight="2"]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);
};[/CODE]
 
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");

}
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
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?')
}
}
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom