Bonfire Mutations [stuck]

Bonfire Mutations [stuck]
0

#1

Hi

I’m hit a wall on this one: I assumed all my expressions would satsify all the prompts on the side. The only giving me trouble is the ["hello,“hey”] scenario… I’ve uploaded a link with my code. Please advise me on what I should be focusing on. No need to give the answer.

Thank you :slight_smile:


#2

You are making this problem more difficult than it has to be. You are declaring many unneeded arrays and variables in general. You only uploaded a screenshot from line 1 to 39, so I do not know what other code you have. It is better to copy and paste the code directly into the forum and then use three backticks (left of #1 on the keyboard) before and after to have it show up as you original typed it. Here are a few suggestions to clean up your code while leaving a few of your variable names:
1st, instead of declaring an empty arrays fst (line 4), snd (line 7), delete those lines replace your line 14 and 15 with the following:

var fst = arr[0].toLowerCase();
var snd = arr[1].toLowerCase();

All you want is the first element in the array (index 0) for fst and the second element in the array (index 1) for snd

2nd, you should remove all variable declarations which are not used in your code such as lines 8, 9, 10, 11, 12, 13, 16, and 17. It just clutters up all of your code.

Not sure why you are pushing items on to the array called emptyf . In general, it is hard to comment much more, because I can not see past line 39, but your logic in your if statements and return statements is not correct.

Please post entire cleaned up code and I will try to help you some more.


#3

Thanks sir! Duly noted and appreciate the input on cleaning up the work.


#4

Thanks so much for the advice on cleaning the code. I realized after removing the clutter, I was missing something important with my if statements. This method seemed to work! Take a look and let me know if there’s anything I can do to lean out the process even more.

function mutation(arr) {

var fst = arr[0].slice().toLowerCase();

var snd = arr[1].slice().toLowerCase();

var crct = [];
var wrng = [];

for (var i =0; i < snd.length; i++) {

if (fst.indexOf(snd[i]) !== -1){

 crct.push(snd[i]);

}// end of if statement

else if (fst.indexOf(snd[i]) !== 0){

 wrng.push(snd[i]);

}//end of else if statement

}//end of for loop

if (fst.indexOf(wrng) !== 0){

return false;

}// end of if statement

else if (fst.indexOf(wrng) !== -1){

return true;

}//end of else if

}//end of function

mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]);


#5

So below is your current code (I used three backtick marks before and after the code to make it look better on the forum here):

function mutation(arr) {
  var fst = arr[0].slice().toLowerCase();
  var snd = arr[1].slice().toLowerCase();
  var crct = [];
  var wrng = [];

  for (var i =0; i < snd.length; i++) {
    if (fst.indexOf(snd[i]) !== -1){
      crct.push(snd[i]);
    }// end of if statement
    else if (fst.indexOf(snd[i]) !== 0){
      wrng.push(snd[i]);
    }//end of else if statement

  }//end of for loop
  console.log(crct,wrng);
  if (fst.indexOf(wrng) !== 0){
    return false;
  }// end of if statement
  else if (fst.indexOf(wrng) !== -1){
    return true;
  }//end of else if
}//end of function

One thing to ask yourself is “Do I really need to put items into an array called wrng?” If you think about it, as soon as you put anything into wrng, you could stop and return false. There is no point in checking anything after that.

Also, ask yourself "Do I really need the array called crct? If you approach the problem as I have suggested above, then if you make it past checking all the elements of snd in the for loop to see if they create a false condtion and none do, then you know you could simply return true after the for loop.

You can solve this problem without creating a single array using the logic I have proposed. See if you can get rid of the arrays and stick to just checking the false condition(s) in the for loop. If you get stuck, reply back with what you have and make sure to use three back ticks before and after your pasted code.


#6

I used arrays for an unusual solution. This function will return true or false followed by an array representing the second parameter. It checks if that array contains a zero. Any zero represents an unmatched letter.

[details=My solution (spoiler)]

function mutation(arr) {
  /* Iterate through 2nd string one character at a time to see how many times each 
character is present in the 1st string */
  var z = [];
  for ( var i = 0; i < arr[1].length; i++ ) {
    z.push(0); //create an array of zeros of length of 2nd string 
    for ( var j = 0; j < arr[0].length; j++ ) {
      var x = arr[0][j];
      var y = new RegExp(x, 'i');  //create RegExp to ignore Case
        if (arr[1][i].match(y)) {
          z[i]++;
      }
    }
  }
  var checkForZero = function(isZero) {
    return isZero === 0;
  };
if (z.find(checkForZero) === 0) { 
  return false + ". array z is [" + z + "]";
}
else return true + ". array z is [" + z + "]";

}
mutation(["hello", "hey"]);
mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
```[/details]

#7

@Soupedenuit Your solution is unusual and overly complicated. Try to keep your solutions simple and easy to follow. While it is great that you were able to incorporate regular expressions into your solution, your lines 8-10 (below):

      var x = arr[0][j];
      var y = new RegExp(x, 'i');  //create RegExp to ignore Case
        if (arr[1][i].match(y)) {

could have been accomplished with the following:

      var x = arr[0][j].toLowerCase();
      var y = arr[1][i].toLowerCase();
        if (x===y) {

Also, you could get rid of checkForZero (lines 15-17 of your solution), if you simply change line 18 from:

if (z.find(checkForZero) === 0) { 

to the following:

if (z.indexOf(0) > 0) { 

#8

thanks for the tips @rmdawson71.
I was pretty happy with this one but obviously still a newbie with much to learn.


#9

I’m with you on that! Glad you figured it out :slight_smile:


#10

I think that’ll be something I can try to do after solving - which is to see if I cut anything out unnecessary.

Thanks again!