Basic Algorithm Scripting: Mutations


I am always getting false. How may I fix it?
Plus**I do nor understand wha is the reason why I get false twice when I shoud get true 4 times.

Plus***What is the reason why I cannot sort this with sort()? is there a bug in this excercise?

function mutation(arr) {
 let indexArr= [];
  
          for (let i=0;i<arr[1].length;i++){
             
               let index=arr[0].indexOf(arr[1][i])
              
               indexArr.push(index)
               
               
      } 
       console.log(indexArr)
              
              if(indexArr.indexOf(-1)>=0){
                         return false;
              }else{
                
                return true;
              }
}

mutation(["Noel", "Ole"]);

Plus**


function mutation(arr) {
  
   for (let i=0;i<arr[1].length;i++){
           
           console.log(arr[0].includes(arr[1][i]))


   }

}

Plus***

let arrFst=arr[0].split("").sort()
  let arrScd=arr[1].split("").sort();


  1. In your function you are not considering the fact that lowercase and capitalized letters returns false when comparing them.
  2. Similarly capitalized letters are being sorted before the lowercase letters, so even Z will end up before a.

General note - do you really need to put result of every check to separate array and basically at the end check once more for letter not present in second array?

1 Like

But it is still not working :frowning:

function mutation(arr) {
let fstEle=arr[0].toLowerCase();

let scdEle=arr[1].toLowerCase();

if(scdEle.indexOf(fstEle)===-1){

        return false;

}else{
           
        return true;   
}
       

                 
              
    }

mutation(["voodoo", "no"]);

you are checking if the whole word is included in the others, instead you need to check if all the letters are

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.

if you had “Alien” and “line” as inputs, you are checking if “line” is included in “alien”, which is not. but you should check if all the letters are included, and they are.

It should return true, your algorithm returns false

1 Like

I am getting: mutation(["hello", "hey"]) should return false. That is the last one.

function mutation(arr) {
let fstEle=arr[0].toLowerCase();

let scdEle=arr[1].toLowerCase();


     for (let i=0;i<scdEle.length;i++){

if(fstEle.indexOf(scdEle[i])==-1){
                   
         return false;    
        

} else{

  return true;
     
     }
          
     }
                 
              
    }

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

It is done!

function mutation(arr) {
let fstEle=arr[0].toLowerCase();

let scdEle=arr[1].toLowerCase();


     for (let i=0;i<scdEle.length;i++){

if(fstEle.indexOf(scdEle[i])==-1){
                   
         return false;    
        
        } 
     
     }
   return true;       
     }
   

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

I am thankful for your time!

here you are just checking first letter

anyway, glad you made it!
happy coding!

1 Like