mardi 5 septembre 2017

If and Else statement are both running in javascript

running a simple search and replace script, and i wrote an if statement that if the string is empty, to alert "no string" but instead the else runs which is the function AND THEN the alert runs

let str = ''; // Testing if statement
let findWord = prompt('what word do you want to find');
let replaceWith = prompt('replace with that new word');

const searchAndReplace = (string, oldWord, newWord) => {
      if (string == '') { // should it be '', or null, or undefined?
            alert('no msg')
      } else {
            return string.split(oldWord.toLowerCase()).join(newWord.toLowerCase());
      }
}

let newString = searchAndReplace(str, findWord, replaceWith);
console.log(newString);

sorry in advance if this is a noob question, tried looking through here already

Aucun commentaire:

Enregistrer un commentaire