Mutations Algorithms

Mutations Algorithms
0

#1

Tell us what’s happening:
Hello Everyone,
Another noob question.

I’m stuck. When returning the result variable I get the matching index values. What is the best way to convert these values back to a string for comparison? or am I going about this the wrong way?

Thank you in Advance for your feedback!

Your code so far

function mutation(arr) {
  var a = arr[0].toLowerCase();
  var b = arr[1].toLowerCase();
  var result = "";
  
  for(var i = 0; i<b.length; i++){
    result += a.indexOf(b[i]);
  }
  return result;
  
}
mutation(["Alien", "line"]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

You can use .charAt() which takes the index of a character of a specified string as an argument and returns the character at that index.


#3

Look at the “Checking Occurrences” section at MDN indexOf Description page.

You can check if a character or word is in a string by seeing if the indexOf is (!== -1)

let arr_a = 'cat';
let arr_b = 'rat';

arr_a.indexOf(arr_b[0]) !== -1; // false
arr_a.indexOf(arr_b[0]) === -1; // true

You could also check if the indexOf method returns a value 0 or greater instead of for -1.

All you need to do is return a true or false.