最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using indexOf() to compare characters in an Array - Stack Overflow

programmeradmin4浏览0评论
  function mutation(arr) {

  var tester = arr[1].split('');

  for (var i = 0; i < tester.length; i ++) {
    if (!arr[0].indexOf(tester[i])) return false;
  }
  return true;
  }

  mutation(["hello", "hey"]);

Here I should return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

I do not see any problems with this code but it passes like only 90% of the tests and I do not know why. And I can not see a pattern there — what exact conditions should I meet to fail the test.

  function mutation(arr) {

  var tester = arr[1].split('');

  for (var i = 0; i < tester.length; i ++) {
    if (!arr[0].indexOf(tester[i])) return false;
  }
  return true;
  }

  mutation(["hello", "hey"]);

Here I should return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

I do not see any problems with this code but it passes like only 90% of the tests and I do not know why. And I can not see a pattern there — what exact conditions should I meet to fail the test.

Share Improve this question edited Nov 18, 2015 at 20:19 Alex Bykov asked Nov 18, 2015 at 9:59 Alex BykovAlex Bykov 7186 silver badges13 bronze badges 4
  • 5 arr[0].indexOf(tester[i]) < 0 because indexOf returns index of char in a string. Zero based. And -1 if char is not in there. – Yury Tarabanko Commented Nov 18, 2015 at 10:01
  • 1 indexOf function will return an int, not a bool – user4090029 Commented Nov 18, 2015 at 10:03
  • 1 @Mr.Wolf javascript allows truthy/falsey don't forget. So this would be valid if the user didn't mind missing the first character. – Curtis Commented Nov 18, 2015 at 10:04
  • Possible duplicate of indexOf is not working in JavaScript – Anonymous Commented Nov 18, 2015 at 15:15
Add a ment  | 

3 Answers 3

Reset to default 8

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

String.prototype.indexOf() returns -1 if value was't found, that is why your statement doesn't work.

Change to:

if (arr[0].indexOf(tester[i]) < 0) return false;

This won't work because you are classing the first position (0 position) as not acceptable.

Your condition will only be true for values which aren't greater than 0, when 0 should also be valid.

Therefore change it so that it only returns false for values which are less than 0.

Change this line:

if (!arr[0].indexOf(tester[i])) return false;

To:

if (arr[0].indexOf(tester[i]) < 0) return false;

Things were really obvious — Upper/LowerCase() issue. This works now:

function mutation(arr) {

 arr[0] = arr[0].toLowerCase();
 arr[1] = arr[1].toLowerCase();
 var tester = arr[1].split('');
 for (var i = 0; i < tester.length; i ++) {
   if (arr[0].indexOf(tester[i]) == -1) return false;
 }
  return true;
}

mutation(["hello", "hey"]);

And of course I have not noticed an obvious 0 position issue:

if (arr[0].indexOf(tester[i]) == -1) return false;

^ this is correct.

Thanks everyone!

发布评论

评论列表(0)

  1. 暂无评论