0

it doesn't work. it always goes with the else. i need to use array, and see if a string contains any of the words of the array. please help, this is really annoying

function buttonClick() {
  var name = document.getElementById('myText').value;
  var yourstring = name;
  var substrings = ['fruit', 'apple'];
  var length = substrings.length;
  while (length--) {
    if (yourstring.indexOf(substrings[length]) != -1) {
      var outcome = 1;
    } else {
      var outcome = 2;
    }
  }
  switch (outcome) {
    case 1:
      document.getElementById('fruitvalue').innerHTML = name + 'is ...';
      break;
    case 2:
      document.getElementById('fruitvalue').innerHTML = name + 'is not ...';
      break;
  }
}
<body>
  <center>
    Last Name:<input type="text" id="myText" value="">
    <button 
      onClick="buttonClick()" 
      style="font-family:font; color:blue;"
    >Submit</button>
    <h2 id="fruitvalue"></h2>
  </center>
</body>
</head>

1
  • 1
    code is only going to be whatever the last step will be since you check outcome outside of the loop.... Commented Oct 27, 2018 at 1:45

4 Answers 4

1

Because you are updating your outcome value for each item in the array. So, if the last item in the array is not your input value, switch statement will check for that only. So, you can use code like below

 function buttonClick() {
    var name = document.getElementById("myText").value;
    var yourstring = name;
    var substrings =['fruit', 'apple'];
    //only change
    document.getElementById("fruitvalue").innerHTML = 
    substrings.includes(yourstring) ? // use of includes function to check item in the array
    (name + " is ...") : 
    (name + " is not ...");  
}
<body>
    <center>
    Last Name:<input type="text" id="myText" value="">
    <button onClick="buttonClick()" style="font-family:font; 
    color:blue;">Submit</button>
    <h2 id="fruitvalue"></h2>
    </center>
</body>
       


        

Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest you do away with the loops and the ifs, and significantly reduce the code by using array.some and String.prototype.includes. This code could be clearly written as a single line:

    function hasWord(str, words) {
      return words.some(word => str.includes(word));
    }

    const name = document.getElementById('myText').value;
    const substrings = ['fruit', 'apple'];
    const element = document.getElementById('fruitvalue');
    
    function buttonClick() {
      const matches = hasWord(name, substrings);    
      const suffix = matches ? 'is ...' : 'is not ...';
      element.innerHTML = `${name} ${suffix}`;
    }
    <body>
      <center>
        Last Name:
        <input type="text" id="myText" value="">
        <button 
          onClick="buttonClick()" 
          style="font-family:font; color:blue;"
        >Submit</button>
        <h2 id="fruitvalue"></h2>
      </center>
    </body>

Comments

0

You should use break inside first if block of while block, otherwise, outcome will be set with other values in next iterations.

function buttonClick() {
  var name = document.getElementById("myText").value;
  var yourstring = name;
  var substrings =['fruit', 'apple'],
      length = substrings.length;
      
  while(length--) {
    if (yourstring.indexOf(substrings[length])!==-1) {
      var outcome=1;
      break;
    }
    else {
      var outcome=2;
    }
  }
  
  switch (outcome) {
    case 1 :
      document.getElementById("fruitvalue").innerHTML = (name + "is ...");
      break;
    case 2 :
      document.getElementById("fruitvalue").innerHTML = (name + "is not ...");
      break;
  }
}
<body>
  <center>
    Last Name:<input type="text" id="myText" value="">
    <button onClick="buttonClick()" style="font-family:font;color:blue;">Submit</button>
    <h2 id="fruitvalue"></h2>
  </center>
</body>

Optimized version is here:

function buttonClick() {
  const yourstring = document.getElementById("myText").value;
  const substrings =['fruit', 'apple']
  
  document.getElementById("fruitvalue").innerHTML = substrings.filter(s => yourstring.indexOf(s) !== -1).length ?
     (yourstring + " is ...") : (yourstring + " is not ...")
}
<body>
  <center>
    Last Name:<input type="text" id="myText" value="">
    <button onClick="buttonClick()" style="font-family:font;color:blue;">Submit</button>
    <h2 id="fruitvalue"></h2>
  </center>
</body>

2 Comments

i think, indexOf() is not the correct logic to compare the data exactly. It will break for input like, apples
that's what @AnkushSharma want?
0

Try this.

function buttonClick() {
  var name = document.getElementById('myText').value;
  var yourstring = name;
  var substrings = ['fruit', 'apple'];

    if (substrings.includes(yourstring)) {
      var outcome = 1;
    } else {
      var outcome = 2;
    }

  switch (outcome) {
    case 1:
      document.getElementById('fruitvalue').innerHTML = name + ' is ...';
      break;
    case 2:
      document.getElementById('fruitvalue').innerHTML = name + ' is not ...';
      break;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.