Mutations: funtion to compare elements from 2 arrays

Mutations: funtion to compare elements from 2 arrays
0

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 can not figure out why you are doing any of the above (especially the myRegex2 = /hi/).

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).

What are you wanting to accomplish with the if statement?

compare one element of each array

You are only comparing the first 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?)

The easiest way to handle the function returning false if any of the tested elements are false, would be to just check for a false condition and return false inside the for loop. Then outside of the for loop, just return true, because you know nothing resulted in a false condition. This way it is a simple if statement without an else statement.

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)

The above if statement can be removed because you are checking if iteratorTester is true and then assigning the same variable true if the comparison evaluates to true or false.

You did not implement the logic I suggested to you.

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

To solve this challenge, you don’t need to convert the strings (the first and second elements of the array passed to the function). Since the challenge asks you to return true if all of the letters of the first string are present in the second string. Another way of thinking about this problem is to realize you just need to validate each letter (case-insensitive) of the second string is found somewhere in the first string OR another way to think about it is that each letter of the second string is “included” in the first string.

You can use a single for loop to iterate through the second string and if you find a single letter that is NOT included in the first string, you return false. If you make it all the way through the second string, this means that the first string contains all of the letters of the second string. It is quite possible that the first string contains extra letters which are not in the second string, but you don’t care about that. If you make it all the way through the loop without returning false, then right after the for loop ends, you can simply return true. It is really that simple.

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:

You still are not following what I said before

You will never make it past the first letter of arr2, because you are returning true or false in the first iteration. After the return statement executes, the function is exited immediately (no passing Go and definitely does not keep iterating through the for loop). It is pure coincidence that most of the challenges are passing based on the values of the strings.

What works? I have no idea what you mean here.

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!