1

I'm trying to see if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example

['hello', 'hey'] = false;
['Army', 'Mary'] = true;

Here is my code

function mutation(arr) {
  a = arr[0].toLowerCase().split("");
  b = arr[1].toLowerCase().split("");
  for(i = 0; i < a.length; i++){
    if(b.indexOf(a[i]) != -1){
      console.log('true');
    } else {
      console.log('false');
    }
  }
}
mutation(['Army', 'Mary']);

UPDATED

I need to see if element 1 contains all the letters for element 2 before I return back anything.

2
  • Good, but what help you need from us ?? Whats question Commented Jul 20, 2015 at 7:30
  • @Panther i updated my question, I left out a crucial part. Commented Jul 20, 2015 at 7:33

4 Answers 4

2

This would do, I'm sure there are better and optimal solutions though,

1) Storing the return result in a boolean, as var result = true;.

2) Check if both the Strings are equal/same, no need to loop, return the result which is true.

3) loop through each characters and see if the target element contains them, if found a mismatch set, result to false, break and return result.

function mutation(arr) {
a = arr[0].toLowerCase().split("");
b = arr[1].toLowerCase().split("");
var result = true;
if(a === b)
return result;
  for(i = 0; i < a.length; i++){
    if(b.indexOf(a[i]) === -1){
      result = false;
      break;
    } 
  }
return result;
}
mutation(['Army', 'Mary']);

UPDATE Added a condition if (a === b) return true; to skip for loop.

Sign up to request clarification or add additional context in comments.

Comments

2

No need of loop, you can take advantage of array functions.

Steps

  1. Sort both arrays
  2. Cast to the string
  3. Check if strings2 contains string1

function mutation(arr) {
  var a = arr[0].toLowerCase().split(''),
    b = arr[1].toLowerCase().split('');

  // For exact equality
  return a.sort().toString() === b.sort().toString();

  // return b.sort().toString().indexOf(a.sort().toString()) > -1;
}

document.write('Army and Mary: ' + mutation(['Army', 'Mary'])); // true

document.write('<br />a and b: ' + mutation(['a', 'b'])); // false

document.write('<br />ab and abc: ' + mutation(['ab', 'abc'])); // false

3 Comments

mutation(['ab', 'abc']); should be true since the first element contains all the letters of the second element don't think this is what he wanted
@herriekrekel No this should be false because c doesn't exist in the first element, we are checking all letters in the second element here, you are mixing up things :)
mutation(['ab', 'abc']) should return false because we are testinf if the string in the first element of the array contains all of the letters of the string in the second element of the array and cin second element isn't in the first, but nice approach man.
1

Simply you need to loop throught the second element letters and return false if a character doesn't exist in first element, or continue the loop if it exists.

Then check if the counter is equal to your string length then it contains all the given letters and return true:

function mutation(arr) {
  a = arr[1].toLowerCase().split("");
  b = arr[0].toLowerCase().split("");
  if (a === b) return true;
  for (i = 0; i < a.length; i++) {
    if (b.indexOf(a[i]) === -1) {
      return false;
    }
  }
  if (i === a.length) {
    return true; // all the letteers of element one exists in the second element
  }
}

if (mutation(['Army', 'Mary'])) {
  alert("Element one contains all letters of second element !");
} else {
  alert("Sorry!");
}

Note:

Make sure you loop throught the second element characters and not the first one, see the a = arr[1].toLowerCase().split("");.

2 Comments

since i did != -1 shouldn't the results be the same tho?
No you need to test if it's === -1 so this character doesn't exist in that case you just need to break the loop and return false, if you test != -1 then itt's found and you need to continue the loop.
0
  //mutation function work ignoring case and order of character in strings
  function mutation(arr) {
  var first = arr[0].toLowerCase();
   var second = arr[1].toLowerCase();
    for(var i = 0; i < second.length; i++){
   if(first.indexOf(second[i]) == -1){
       return false;
  }
  }
  return true;
  }
  //this returns true
  mutation(["hello", "ol"]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.