In your second example, the one with a curly brace, you’ve forgotten to include a closing curly brace for the if statement.
freeCodeCamp Challenge Guide: Mutations
This is my proposed solution.
function mutation(arr) { var el1 = arr[0].toLowerCase().split(""); var el2 = arr[1].toLowerCase().split(""); for(var i = 0 ; i < el2.length; i++) { if(!el1.includes(el2[i])) { return false; } } return true; }
Is the .indexOf method faster than .includes?
My solution. I don’t know why, but I could not use “indexOf ()” properly. I then looked in the documentation for “indexOf ()” and found “inculdes”. With this function, I quickly found a solution. Happy to have discovered “every ()”.
function mutation(arr) {
arr = arr.join(" “).toLowerCase().split(” ");for(var i = 0; i < arr[1].length; i++) {
for(var j = 0; j < arr[0].length; j++) {
if(arr[0].includes(arr[1][i]) == false) {
return false;
}
}
}return true;
}
mutation([“hEllo”, “hey”]);
Edit : Shit ! Why I use two “for” ?! Delete one “for”.
function mutation(arr) {
arr = arr.join(" “).toLowerCase().split(” ");for(var i = 0; i < arr[1].length; i++) {
if(arr[0].includes(arr[1][i]) == false) {
return false;
}
}return true;
}
I knew that my way was super inefficient but I didn’t want to peak the answer. So behold a working algorithm that is rather hideous:
function mutation(arr) {
var str1 = arr[0].toLowerCase();
var str2 = arr[1].toLowerCase();
var arrayOf2 = str2.split("");
var arrayOfTruth = [];
for (var i=0;i<str2.length;i++) {
if (str1.indexOf(arrayOf2[i])!== -1) {
arrayOfTruth.push("true");
} else {
arrayOfTruth.push("false");
}
}
if (arrayOfTruth.indexOf("false") !== -1 ) {
return false;
} else {
return true;
}
}
ironically, i could have used the logic of the last if function as the whole answer as shown in the basic solution in the OP.
I used a switch statement to get around [“hello”, “hey”]…
function mutation(arr) {
var word = [];
for (var i = 0; i < arr.length; i++) {
word.push(arr.shift());
word = word.join("").toLowerCase().split();
arr = arr.join("").toLowerCase().split();
for (var j = 0; j < arr[i].length; j++) {
switch (word[i].indexOf(arr[i][j])) {
case -1: return false;
}
}
return true;
}
}
mutation([“hello”, “hey”]);
Hi, I am trying to solve this challenge and I do not understand why my code isn’t working. Can somebody help? Thanks!
function mutation(arr) {
var string1 = arr[0].toLowerCase();
var string2 = arr[1].toLowerCase();
for (var i = 0; i < string2.length; i++) {
if (string1.indexOf(string2[i]) != -1) {
arr = true;
} else {
arr = false;
}
}
return arr;
}
I think I may win the most convoluted/inefficient award…
function mutation(arr) {
var arr1 = arr[0].toLowerCase();
var arr2 = arr[1].toLowerCase();
var newArr = [];
if (arr2.length >= arr1.length) {
for (i = 0; i < arr1.length; i++) {
newArr[i] = arr2.indexOf(arr1[i]);
}
for (i = 0; i < newArr.length; i++) {
if (newArr[i] === -1) {
return false;
}
}
return true;
}
if (arr1.length > arr2.length) {
for (i = 0; i < arr2.length; i++) {
newArr[i] = arr1.indexOf(arr2[i]);
}
for (i = 0; i < newArr.length; i++) {
if (newArr[i] === -1) {
return false;
}
}
return true;
}
}
function mutation(arr) {
var caractere = arr[1].toLowerCase().split(’’);
for(var i=0;i<caractere.length;i++){
if(arr[0].toLowerCase().indexOf(caractere[i])==-1){
return false;
}
}
return true;
}
mutation([“voodoo”, “no”]);
function mutation(arr) {
var i=0,w1=arr[0].toLowerCase(),w2=arr[1].toLowerCase().split(’’);
for(i=0;i<w2.length;i++){
if(w1.indexOf(w2[i])==-1){
return(false);
}
}
return(true);
}
can someone please tell me how is this syntax correct?
if (target.indexOf(test[i]) < 0)
return false;
why is this not working (it gives me wrong ‘true’ on first of examples), while this has to be the right syntax :
if (target.indexOf(test[i]) < 0) {
return false;
}
Don’t expect anyone to help you when you only posted one if statement. You’re missing the rest of the code, and it seems like you only blindly copied and pasted the solution code anyway.
My solution code, I guess a bit over-complicated but working just fine.
function mutation(arr) {
arr = arr.join(" ").toLowerCase().split(" ");
let temp = 0;
for(let i = 0; i < arr[1].length; i++) {
if((arr[0].indexOf(arr[1][i]) >= 0)) {
temp += 0;
} else {
temp += 1;
}
}
if(temp > 0) {
return false;
} else {
return true;
}
}
mutation(["hello", "hey"]);
I found a similar solution using a counter to keep track of matches and then testing it against the length of the second string to return true or false.
function mutation(arr) {
//break array into two separate strings that are lowercase.
var first = arr.shift().toLowerCase();
var second = arr.shift().toLowerCase();
//record length of second string to later check against match counter.
var secondCount = second.length;
//split second string into an array to iterate through.
var secondSplit = second.split("");
//load up the first letter to be checked.
var secondLetter = (secondSplit.pop());
//create a counter to keep track of matches
var count = 0;
//iterate through secondSplit to check for first.indexOf matches.
while (first.indexOf(secondLetter) > -1) {
//if a match happens, add 1 to count.
count++;
//load up next letter to be checked.
secondLetter = (secondSplit.pop());
//if the counter is the same number as the original second.length then return true.
} return count === secondCount;
}
mutation(["Floor", "for"]);
I am n00b
Here’s my annotated answer. I commented it so its self explanatory
brief explanation
- There’s 2 loops here, the first iterates through the arr[1], the second loop is through arr[0]
- No
indexOf
statements used here
function mutation(arr) {
//Lowercase array keys to store string vars
var sampleStr = arr[0].toLowerCase();
var checkStr = arr[1].toLowerCase();
//Bool condition if letters in "Hey" match "Hello"
var validateLetter = false;
//Test for failure
for (let i=0; i<checkStr.length; i++){
//Assume fail conditions per "h","e","y" checks
validateLetter = false;
//Check
for (let j=0; j<sampleStr.length; j++){
if (checkStr[i] == sampleStr[j]){
validateLetter = true;
}
}
//Stop execution when "y" in "hey" doesn't match any char in "Hello"
if (!validateLetter){
return false;
}
}
//If no failure then must be true
return true;
}
mutation(["hello", "hey"]);
modified with indexOf solution
After writing basic solution I modified it with indexOf
string method using same for loop / iteration logic
function mutation(arr) {
//Lowercase array keys to store string vars
var sampleStr = arr[0].toLowerCase();
var checkStr = arr[1].toLowerCase();
//Compare/Test for failure
for (let i=0; i<checkStr.length; i++){
if (sampleStr.indexOf(checkStr[i])==-1){
return false;
}
}
return true;
}
mutation(["hello", "hey"]);
Looking at official solutions this is more or less what basic solution looks like, except I called target == sampleStr
and test == checkStr
there is another solution .
function mutation(arr) {
var s1,s2;s1=arr[0].toLowerCase().split(""); s2=arr[1].toLowerCase().split(""); var c=0; for(var i=0;i<arr[1].length;i++) { for(var j=0;j<arr[0].length;j++) { if(s2[i]==s1[j]) { c++; break; } } } return (c>=arr[1].length); }
mutation(["voodoo", "no"]);
Would you mind posting your full, current code you have at the moment? It’d help us further teach you
No idea about efficiency in the code but ended up with this
function mutation(arr) {
var benchmarkString = arr[0].toLowerCase();
var checkString = arr[1].toLowerCase().split(’’);
return Math.min.apply(Math,checkString.map(function (counter) { return benchmarkString.indexOf(counter);})) >= 0 ;
}
MY solution :
function mutation(arr) {
var str0 = arr[0].toLowerCase().split("");
var str1 = arr[1].toLowerCase().split("");
var search = [];
for (var x = 0; x < str1.length ; x++){
search.push(str0.indexOf(str1[x]));
}
search.sort(function(a,b){return a-b;});
if (search[0] >= 0){
return true;
}else{
return false ;
}
}
function mutation(arr) {
for(var i in arr){
arr[i]= arr[i].toLowerCase(); // replace the the elements
}
var fs=arr[0].split(’’);
var ss=arr[1].split(’’);
var results=true;
for(var x in ss){ // how come they still teach i++ version isn’t this one easier?
if(fs.indexOf(ss[x]) == -1)
results = false;
}
return results;
}
mutation([“HELLO”, “hey”]);
was I thinking to much into this example because your code was very simple
My solution … surely could be cleaned up a little, but I am really happy that I passed