0

I want to initiate a code if the user has entered a specific symbol or string. In this task I need to check if the user has entered any of the following : "a","b","c","d". However it seems to just ignore the if statements.

Here is the whole problem: Write a program that determines the color of a chessboard square based on its Label and Rank. Input: On the first line, you will receive L - the label On the second line, you will receive R - the rank

let L = prompt();
let R = Number(prompt());
if (L == ("a", "c", "e", "g")) {
  if (R % 2 == 0) {
    /*if we are on a/c/e/g lines and the number is even the 
                         square is white*/
    console.log("light");
  } else {
    console.log("dark");
  } //else it is going to be odd therefore dark
} else if (L == ("b", "d", "f", "h")) { //opposite logic:
  if (R % 2 == 0) {
    console.log("dark");
  } else {
    console.log("light");
  }
}

The problem is I don't know how to compare the two strings. I tried with some of the string methods but I guess I am just making a sintax error

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Possible duplicate of [Determine if string is in list in JavaScript](https://stackoverflow.com/questions/2430000/determine-if-string-is-in-list-in-javascript) – Heretic Monkey May 21 '19 at 16:10

2 Answers2

0

Comparison with == doesn't work like that. Either split your code in multiple or (||) statements or use an array:

Or:

var L = "e";
if (L == "a" || L == "c" || L == "e" || L == "g"){
    console.log("Or method");
}

Array - includes:

var L = "c";
if (["a", "c", "e", "g"].includes(L)){
    console.log("includes method")
}

Array - indexOf:

var L = "g";
if (["a", "c", "e", "g"].indexOf(L) !== -1){
    console.log("indexOf method");
} 

More on logical operators in JavaScript:

https://javascript.info/logical-operators

NullDev
  • 6,739
  • 4
  • 30
  • 54
-1

("a","c","e","g") is an expression using comma operator and it will evaluate to its last operand("g" in above case). So

if(L==("a","c","e","g")){..}

is same as

if(L == "g"){...}

You can create array and then use includes()

let L = prompt();
let R = Number(prompt());
if (["a", "c", "e", "g"].includes(L)) {
    if (R % 2 == 0) {
        console.log("light");
    } else {
        console.log("dark");
    } 
} else
if (["b", "d", "f", "h"].includes(L)) { 
    if (R % 2 == 0) {
        console.log("dark");
    } else {
        console.log("light");
    }
}

You can make your code much shorter. Create an array containing both the arrays of letters and add the indexOf array in which L is present to R

let L = prompt();
let R = Number(prompt());

const arr = [['a','c','e','g'],['b','d','f','h']];


if((R + (arr.findIndex(a => a.includes(L)))) % 2 === 0) console.log('white');
else console.log('black');
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Yea thanks that is gonna be a useful method. The idea of the task is to use only if statements for exercising so we should use only conditionals. Anyways will be useful for the other tasks ass well thank you! – danail iliev May 21 '19 at 16:02
  • @danaililiev Always glad to help. Make sure to see the second method which is much cleaner. – Maheer Ali May 21 '19 at 16:11