Mutations- Trying to solve it using arrays (solved)

Mutations- Trying to solve it using arrays (solved)
0

#1

Tell us what’s happening:
Hi there!

I already solved this problem while manipulating in strings. I was wondering/trying to see if I could get it to work in arrays.

Steps:

  1. Create a newstr and lowercase all of them
  2. Take the entire string and put it into a newarray using split
    3)Run a for loop based on original entry arr[0] and the length of our newarray
  3. Use indexOf on newarray to find if any -1 returns (I think this line is wrong in actual code, also cause it searches the entire array and I can’t search a portion of it)
  4. If -1 returns, then return False
  5. If all pass, return True

Any help would be great!
Your code so far

function mutation(arr) {

 var newstr=arr.join('').toLowerCase();
  var newarr = newstr.split('');

for (i = arr[0].length;i<newarr.length;i++){
  if (newarr.indexOf(newarr[i])==-1){
    return false;
}
      return true;
}
}

  


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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0.

Link to the challenge:


#2

The way you are doing this with the following line is part of your problem. Using your example call to the function, after this line executes, newstr contains “hellohey”. Maybe create lowercase version of each array element first?

var newstr=arr.join('').toLowerCase();

After the following line executes, newarr is [ ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘h’, ‘e’, ‘y’ ]

var newarr = newstr.split('');

OK, so the following line does appear to start at the correct location of newarr (where the letter “h” starts in the word “hey” and the for loop condition will end at the letter “y” of the word “hey”. So far so good.

  for (i = arr[0].length; i<newarr.length; i++){

You are correct about this step having the wrong code. Instead of using newarr.indexOf(new[i]), you should just be checking the first 6 elements of newarr. This is why I said step 1 was your first mistake. If you want to use your existing code and just get past this challenge, then you could use either research to slice function which lets you return a specific part existing array. You could use it to create an array containing just the letters of “hello” and then use this new array (let’s call it checkArr) like:

checkArr.indexOf(arr[i])

While you have written the correct steps above, your code does reflect what you have written. Your current code will only ever make it through the first iteration, because inside the for loop, you return a true or false value, even if you have not checked the other letters yet. Remember, that once a return statement is executed, the value is returned and the function is immediately exited and does not return to the for loop. You just need to move one of the return statements. I will let you figure out which one.


#3

Hi Randell!

Thanks for going through each of my steps line by line, really shows and helps how my logic/code was flawed.

I understand your concern about #1 because my original solution (same as one shown in tips), just wanted to see and ask about doing it in one array. But I guess given the tools that we have (and maybe logic wise), it is prone to errors and better off as model solution.

I finally understand why I think the for loops are incorrect as you have mentioned. I do realize the True was written incorrectly, but did not know return exits the loop, I thought it just kept going. Which is why when I wrote it on paper, it made sense to me but not as output.

Thank you again!