Basic Algorithm Scripting: Mutations

function mutation(arr) {
  let text = ""; 
  let secondString = arr; 

  for (let i = 0; i < text.length; i += 1) {
    if (str.indexOf(text) == str.indexOf(secondString)) {
      return true
  return false;

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

When I run the test I get all the ‘return false’ correct, but the ones that are supposed to return true are incorrect. Trying my best not to look at answers, any advice?


function mutation(arr) {
  const firstString = arr[0].toLowerCase; 
  const secondString = arr[1].toLowerCase; 

  for (let i = 0; i < secondString.length; i += 1) {
    if (firstString.indexOf(secondString[i]) = 0) return false; 
      return true; 

Everything for my new solution is coming out as true. I don’t know how to get it to understand that if both strings don’t contain the same letters…then it’s false

I’ve edited your code for readability.
When you want to post code on the forum, please use the Preformatted Text tool (</> or CTRL+e) and paste your code between the two sets of triple backticks.

If you want to increment a variable, i += 1 works but usual practice is i++.

Your toLowerCase() method will not work unless you include the parentheses.

This is not a comparison operator:
if (firstString.indexOf(secondString[i]) = 0)

Also, if you want to use indexOf() to check whether a string contains a particular value using 0 will only check if the value is at index 0 in the string. What value does indexOf() return if it doesn’t find the character you’re searching for?

Thanks! It works now.

if (firstString.indexOf(secondString[i]) < 0) return false; 

Can you explain what this line of code is saying. Putting it less than 0 is confusing me. I understand everything else.

My understanding is that the indexOf() function is searching “firstString” for the presence of “secondString[i]”. If it is present it will return the index at which it is found and this will be a number that is 0 or greater and the condition will not be true. If it is not present then the function will return -1 which means the condition will be true as -1 < 0 is true.

I am still learning as well though so I could be mistaken…

1 Like

This is just how the indexOf() method works. It will return the first index at which it finds the item searched for. If it can’t find it, it will return the value -1.
Returning a value of -1 makes it a useful method for checking whether or not a particular item exists.

1 Like