Mutations: funtion to compare elements from 2 arrays

Tell us what’s happening:
I am trying to : 1) using regex, split into arrays the words to be compared, so each regex would allow to set an array. 2) Compare an element of one array with another one from the other array. 3) if it works, I would implement regex working with any input and proceed to iterate to solve the challenge.

I am stuck in the second step: I couldn’t get the if else statement working out…

Your code so far

const myArray = ['hello', 'hey'];

let strFromMyArray = myArray.join(", ").toLocaleLowerCase();


let myRegex = /hello/;
let arrFromFirstWord = strFromMyArray.match(myRegex);

let myRegex2 = /hi/;
let arrFromSecondWord = strFromMyArray.match(myRegex2);

function comparing (arr1, arr2) {
  if (arr1[0] == arr2[0]) {    //I'm intending to compare without a loop,
  return true;                 //just to see if it works
 
} else {
  return false;
}
}



console.log(comparing(arrFromFirstWord, arrFromSecondWord));







Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.94.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/

I’m trying a solution that only works with an specific array (so, I don’t waste time solving regex). And, I just wanna see if I can compare only one element of array1 with one element of array2 (so, I don’t waste time figuring out how to iterate those arrays until I see if the core solution works).

compare one element of each array

yea, exactly. Like this: firstElementTested[0].includes(thirdTester[0])-->true

Let’s forget about it.
I’m trying this:

function mutation(arr) {
  let firstElement = arr.slice(0,1).join().toLowerCase().split("");
  let secondElement = arr.slice(1,2).join().toLowerCase().split("");
  let iteratorTester;
  let counter1 = 0, counter2 = 0;
    for (let i in firstElement){
      iteratorTester = firstElement[counter1].includes(secondElement[counter2]);
      if (iteratorTester === true){
        iteratorTester == true;
         counter2++;
        
      } else {
        iteratorTester == false;
        
        
      }
    } 
    
    return iteratorTester;
}

Problem: I don’t know how to move counter1 forward and start over counter2. And I don’t know how to return the hold result (it should be: :slight_smile: ìf any of the tested elements false, function return false’, right?)

Ok. So, this returns some false but it’s not working with some cases:

function mutation(arr) {
  let firstElement = arr.slice(0,1).join().toLowerCase().split("");
  let secondElement = arr.slice(1,2).join().toLowerCase().split("");
  let iteratorTester;
    for (let i = 0; i < firstElement.length; i++){
      for (let j = 0; j < secondElement.length; j++){
        iteratorTester = firstElement[i].includes(secondElement[j]);
        if (iteratorTester == true){
           iteratorTester == true;
           } else {
           iteratorTester == false;               
      }
      }       
    }     
    return iteratorTester;
}

mutation(["Alien", "line"]);

How do I get a if test that really test each element of firstElementwith each secondElementbefore returning a result?
In fact, the problem I have is that the program is checking correctly each possible comparison (at this context) but iteratortester is just storing the last test (in this example is returninf false when we want true)

Maybe if you explain the suggestion with an example I could understand it

Thanks R.
I almost did it:

function mutation(arr) {
  let arr1 = arr.slice(0, 1).join().toLocaleLowerCase();
  let arr2  = arr.slice(1, 2).join().toLocaleLowerCase();  

  for (let i = 0; i < arr2.length; i++){  
    return (arr1.indexOf(arr2[i])) == -1 ? false : true;
           
  }

}

console.log(mutation(["hello", "hey"]));

I only doesn’t work with `[“hello”, “hey”], So, I did an exercise

let arr = ['hello', 'hEy'];

let arr1 = arr.slice(0, 1).join().toLocaleLowerCase();
let arr2  = arr.slice(1, 2).join().toLocaleLowerCase();

console.log(typeof(arr2));

console.log(arr1.indexOf(arr2)); //--> -1


console.log("hello".indexOf("hey"));

and it works. So, I don’t get it.:expressionless:

Randell. Thanks for the feedback. Finally, I got a solution.

I want to say that I felt afraid of asking something else when you used that expression; too harsh for me. The rest of the feedback was excellent.

function mutation(arr) {
  let arr1 = arr.slice(0, 1).join().toLocaleLowerCase();
  let arr2  = arr.slice(1, 2).join().toLocaleLowerCase();  
  let test;
  let result = [];
  for (let i = 0; i < arr2.length; i++){  
    test = arr1.indexOf(arr2[i]);   
    result.push(test);        
  }
  return result.includes(-1) ? false : true;
}

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

Some problems I faced up to here:
I realized that I needed 2 vars: one (test) to take the result of each iteration and other one (an array) to store all the results, so once the loop has ended up I could use the includes method to see if any of tested characters in arr2 (the second word of the array given by the challenge) was absent in the tested array arr1 .

you don’t need the two variables. if test is -1, just return false from inside the loop

for future reference, this is perfectly good syntax and doesn’t need a second variable: result.push(arr1.indxOf(arr2[i]))

I appreciate it ieahleen!