Archan Ghatpande
Archan Ghatpande

Reputation: 41

Why is my multiple if else not working in JavaScript?

function myFunction2() {
  var booloo = document.getElementById('texa').value;
  if (!document.getElementById('texa').value) {
    //var res = booloo.lastcharAt(booloo.value);
    console.log("nothing");
  } else if (booloo.substring(booloo.length - 1) == "+" || "-" || "*" || "/") {
    console.log("String Contain Operator at last");
  } else if (booloo.substring(booloo.length - 1) == "0" || "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9") {
    console.log("String contain Operand at last");
  }
}
<html>

<body>
  <input types="text" id="texa">
  <button onclick="myFunction2()">Try it</button>
</body>

</html>

I'm trying to use multiple if else blocks in my code but at third block ("String contain Operand at last") this is not working only remaining two blocks are working perfect.

My objectives are if user type value like e.g 10+ value then it goes in second block that operator contain at last. and if suppose user type 10+2 then it must goes into third block like ("contain Operand at last") but the problem is it does not go into third block.

Upvotes: 1

Views: 422

Answers (7)

weroro
weroro

Reputation: 1692

The new solution

As of 2015, browsers support Array.prototype.includes (caiuse, MDN) and with this you don't need to use indexOf.

if (['item1', 'item2'].includes(variable)) {
  // logic
}

Upvotes: 0

gavgrif
gavgrif

Reputation: 15509

I would use an array of operands to check the last character against and a ternary operator to differentiate the two outcomes if there is a value in the textbox.

function myFunction2() {
  var booloo = document.getElementById('texa').value;
  
  if (!document.getElementById('texa').value) {
    //var res = booloo.lastcharAt(booloo.value);
    console.log("nothing");
  } else {
  var operands=["+", "-", "*","/"];
  var lastChar= operands.indexOf(booloo.substring(booloo.length - 1)) ==-1 ? "Operator" : "Operand";
  
  console.log("String contains " + lastChar + " at last")
  }
}
<html>

<body>
  <input types="text" id="texa">
  <button onclick="myFunction2()">Try it</button>
</body>

</html>

Upvotes: 2

rtbm
rtbm

Reputation: 536

Your conditional statement is wrong defined. Rather than for checking for booloo.substring(booloo.length - 1) == "+" or "-" || "*" || "/" is logical true you should check for char occurrence in a string as following:

function myFunction2() {
  var booloo = document.getElementById('texa').value;
  
  if (!document.getElementById('texa').value) {
    console.log("nothing");
    return; // terminate function execution
  }

  var char = booloo.substring(booloo.length - 1);

  if (["+", "-", "*", "/"].indexOf(char) !== -1) {
      console.log("String Contain Operator at last");
  }

  else if (["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].indexOf(char) !== -1) {
      console.log("String contain Operand at last");
  }
}
<html>

<body>
  <input types="text" id="texa">
  <button onclick="myFunction2()">Try it</button>
</body>

</html>

Upvotes: 0

Pugazh
Pugazh

Reputation: 9561

Here is a better way of doing it with Regular Expression.

function myFunction2() {
  var booloo = document.getElementById('texa').value;
  var arr = [];
  if (!booloo) {
    console.log("nothing");
  } else {
    arr = booloo.split(/[+|-|*|\/]/);
    console.log(arr);

    if (arr[arr.length - 1] == "") {
      console.log("String Contain Operator at last");
    } else {
      console.log("String contain Operand at last");
    }
  }
}
<html>

<body>
  <input types="text" id="texa">
  <button onclick="myFunction2()">Try it</button>
</body>

</html>

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

You could take a single character and check it agains a string for the index.

function myFunction2() {
    var booloo = document.getElementById('texa').value;
    if (!document.getElementById('texa').value) {
        //var res = booloo.lastcharAt(booloo.value);
        console.log("nothing");
    } else if ('+-*/'.indexOf(booloo[booloo.length - 1]) !== -1) {
        console.log("String Contain Operator at last");
    } else if ('0123456789'.indexOf(booloo[booloo.length - 1]) !== -1) {
        console.log("String contain Operand at last");
    }
}
<input types="text" id="texa"> <button onclick="myFunction2()">Try it</button>

Upvotes: 0

baao
baao

Reputation: 73251

You can't compare to multiple values as you are trying to do.

You either have to write it out

booloo.substring(booloo.length - 1) == "+" || 
booloo.substring(booloo.length - 1) == "-" ||
// ...

Or create an array and check for indexOf()

var check1 = ['+','-','*','/'];
if (check1.indexOf(booloo.substring(booloo.length - 1) > -1)

Upvotes: 0

Igor
Igor

Reputation: 62238

That is not how a comparison works.

if(booloo.substring(booloo.length - 1) == "+" || "-" || "*" || "/")

should be written as

if(booloo.substring(booloo.length - 1) == "+" 
   || booloo.substring(booloo.length - 1) == "-" 
   || booloo.substring(booloo.length - 1) == "*" 
   || booloo.substring(booloo.length - 1) == "/")

Your existing code would be evaluated like this:

  • check if booloo.substring(booloo.length - 1) == "+"
  • Check if "-" (always evals to true, rest of statement would not be tested but I wrote it out anyway)
  • check if "*" (also always evals to true)
  • check if "/" (also always evals to true)

Upvotes: 7

Related Questions