0

I’m trying to figure out why my code is not giving the right output.

My input shouldn’t be contained within the array elements.

I found an easy way to solve it with regex, so I am not using regex for that one.

Please, break down my code, and tell me what is the problem with the code.

function checkInput(input, words) {
  var arr = input.toLowerCase().split(" ");
  var i, j;
  var matches = 0;
  for (i = 0; i < arr.length; i++) {
    for (j = 0; j < words.length; j++) {
      if (arr[i] != words[j]) {
        matches++;
      }
    }
  }
  if (matches > 0) {
    return true;
  } else {
    return false;
  }
};

console.log(checkInput("Move an array element from one array", ["from"])); // should be false
console.log(checkInput("Move an array element from one array", ["elem"])); // should be true

2
  • So you mean, the function should return true iff no element of your words array is a word in your input string, right? Commented Nov 3, 2017 at 4:57
  • @Xufox, thats right. Commented Nov 3, 2017 at 5:06

2 Answers 2

1

if (arr[i] != words[j]) will be true at some point or another most of the time.

You want to check the opposite and return the opposite logic, so:

if(arr[i] == words[j]) {
  matches++;
}

and

if (matches > 0) {
    return false;
} else {
    return true;
}

But a simpler way would be:

function checkInput(input, words){
  let lowerCaseInput = input.toLowerCase().split(" ");
  return words.find(word => lowerCaseInput.includes(word)) === undefined;
}

Array.prototype.find will return undefined iff no element is found that satisfies a specific rule provided by the callback function, word => lowerCaseInput.includes(word) in this case. So we check whether its return value is undefined which will tell us whether a word has been matched in input.

Note that your function unnecessarily checks for the entire words array even though it only matters whether one word matches.

Also, the words in words are case-sensitive! If you don’t want that, replace .includes(word) by .includes(word.toLowerCase()).

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

Comments

0

Because you are using matches > 0, you think it will return true only when no matches is found. But what happens is that when you have input ab aa cc and word aa

  • first iteration matches = 1
  • second iteration matches = 1
  • third iteration matches = 2

So matches will contain how many times word is different from items of input. So as result, it will always return true as long as input is more than two words long, for at least one word of input will be different from word. You can rather consider increasing the value of matches if word is found instead.

function checkInput(input, words) {
    var arr = input.toLowerCase().split(" ");
    var i, j;
    var matches = 0;
    for (i = 0; i < arr.length; i++) {
        for (j = 0; j < words.length; j++) {
            if (arr[i] === words[j]) {
                matches++;
            }
        }
    }
    if (matches === 0) {
        return true;
    } else {
        return false;
    }
};

console.log(checkInput("Move an array element from one array", ["from"])); 
console.log(checkInput("Move an array element from one array", ["elem"]));

3 Comments

@Xufox, I was editing the post when you downvoted me. Even if the code itself was not an explanation, I don't think that my answer was wrong to the point of being downvoted as long as my code solves the issue.
Okay, I understand @Xufox. But you were to quick to downvote the answer. For I was editing it.
I mean given the sentence Move an array element from one array. As long as, the input is more than two words longs, it will always return true.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.