-1

I am trying the "if-else" condition with array values in Javascript. The result is the same for both conditions. How to fetch results for the same condition.

Here is my code for the Program:

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

  <title>Conditional Statement</title>
</head>

<body>

  <!-- Section for body starts here -->
  <div class="mb-3">
    <label for="exampleFormControlInput1" class="form-label">Test a Fruit</label>
    <input type="text" class="form-control" id="fruitsName" onchange="updateFruits()">
  </div>
  <!-- Section for body ends here -->


  <script type="text/javascript">
    function updateFruits() {
      const fruits = ["Apple", "Banana", "Mango"];
      var fruitName = document.getElementById('fruitsName').value;

      if (fruitName != fruits) {
        console.log("This is not a fruit");
      } else {
        console.log("This is a fruit");
      }
    }
  </script>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>


</body>

</html>
Nick Vu
  • 8,514
  • 1
  • 14
  • 25

0 Answers0