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
becauseindexOf
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 anint
, not abool
– 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
3 Answers
Reset to default 8The 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!