Nathan Nathan - 5 months ago 10
Javascript Question

indexOf() : is there a better way to implement this?

EDIT

Thank you guys, and i apologize for not being more specific in my question.
This code was written to check if a characters in the second string is in the first string. If so, it'll return true, otherwise a false.

So my code works, I know that much, but I am positive there's gotta be a better way to implement this.

Keep in mind this is a coding challenge from Freecodecamp's Javascript tree.

Here's my code:



function mutation(arr) {

var stringOne = arr[0].toLowerCase();
var stringTwo = arr[1].toLowerCase().split("");
var i = 0;
var truthyFalsy = true;

while (i < arr[1].length && truthyFalsy) {

truthyFalsy = stringOne.indexOf(stringTwo[i]) > -1;
i++

}
console.log(truthyFalsy);
}




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





THere's gotta be a better way to do this. I recently learned about the map function, but not sure how to use that to implement this, and also just recently learned of an Array.prototype.every() function, which I am going to read tonight.

Suggestions? Thoughts?

Answer

the question is very vague. however what i understood from the code is that you need to check for string match between two strings.

Since you know its two strings, i'd just pass them as two parameters. additionally i'd change the while into a for statement and add a break/continue to avoid using variable get and set.

Notice that in the worst case its almost the same, but in the best case its half computation time.

mutation bestCase 14.84499999999997
mutation worstCase 7.694999999999993
bestCase: 5.595000000000027
worstCase: 7.199999999999989

// your function (to check performance difference)
function mutation(arr) {

  var stringOne = arr[0].toLowerCase();
  var stringTwo = arr[1].toLowerCase().split("");
  var i = 0;
  var truthyFalsy = true;

  while (i < arr[1].length && truthyFalsy) {

    truthyFalsy = stringOne.indexOf(stringTwo[i]) > -1;
    i++

  }
  return truthyFalsy;
}



function hasMatch(base, check) {
  var strOne = base.toLowerCase();
  var strTwo = check.toLowerCase().split("");

  var truthyFalsy = false;

  // define both variables (i and l) before the loop condition in order to avoid getting the length property of the string multiple times.
  for (var i = 0, l = strTwo.length; i < l; i++) {
    var hasChar = strOne.indexOf(strTwo[i]) > 0;
    if (hasChar) {
      //if has Char, set true and break;
      truthyFalsy = true;
      break;
    }
  }
  return truthyFalsy;
}

var baseCase = "hello";
var bestCaseStr = "hey";
var worstCaseStr = "yap";

//bestCase find match in first iteration
var bestCase = hasMatch("hello", bestCaseStr);
console.log(bestCase);

//worstCase loop over all of them.
var worstCase = hasMatch("hello", worstCaseStr);
console.log(worstCase);

// on your function
console.log('mutation bestCase', checkPerf(mutation, [baseCase, bestCaseStr]));

console.log('mutation worstCase', checkPerf(mutation, [baseCase, worstCaseStr]));

// simple performance check
console.log('bestCase:', checkPerf(hasMatch, baseCase, bestCaseStr));

console.log('worstCase:', checkPerf(hasMatch, baseCase, worstCaseStr));

function checkPerf(fn) {
  var t1 = performance.now();
  for (var i = 0; i < 10000; i++) {
    fn(arguments[1], arguments[2]);
  }
  var t2 = performance.now();
  return t2 - t1;
}