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;
}
}
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.
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"]);
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
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
This was so hard for me, don’t know why, the test[i] part I couln’t put in code, anyway this is my solution:
function mutation(arr) {
let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();
for (let i = 0; i < test.length; i++) {
if (!~target.indexOf(test[i]))
return false;
}
return true;
}
mutation(["Alien", "line"]);