最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return true if the string in the first element of the array contains all of the letters of the string in the second

programmeradmin1浏览0评论

This is my code so far; All I've done is loop through each letter. I'm stuck on how to test whether or not the strings have the same characters.

function mutation(arr) {
  for (var i=0;i<arr.length;i++){
      for(var j =0;j<arr[i].length;j++){
      }
  }
}

mutation(['hello', 'hey']);

The characters don't have to be at the same index, so for example, ['Alien', 'line'], should return true.

This is my code so far; All I've done is loop through each letter. I'm stuck on how to test whether or not the strings have the same characters.

function mutation(arr) {
  for (var i=0;i<arr.length;i++){
      for(var j =0;j<arr[i].length;j++){
      }
  }
}

mutation(['hello', 'hey']);

The characters don't have to be at the same index, so for example, ['Alien', 'line'], should return true.

Share Improve this question asked Jul 20, 2015 at 16:17 natalienatalie 991 gold badge3 silver badges7 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

I used the answer above but modified it to remove the case, otherwise i was getting a false on Hello and hello

function mutation(arr) {

//first split the arr into the two inputs and convert all to lower case

var firstArray = arr[0].toLowerCase().split("");
var secondArray = arr[1].toLowerCase().split("");

//now using the code provided by the above ment which is really clean

var count = 0;
for (var i =0; i < secondArray.length; i++) {
    if(firstArray.indexOf(secondArray[i]) > -1 ) {
        count++;
    }
}

if (count == secondArray.length) {
    return true
}

//changed the code provided above to handle the true/false criteria of the excercise

else {return false; }
}

mutation(['hello', 'hey']);

Clean, modern and easy to read:

function mutation(arr) {
  const firstEl = arr[0].toLocaleLowerCase();
  const secondEl = arr[1].toLocaleLowerCase().split('');
  return secondEl.every(el => firstEl.includes(el));
}

console.log('mutation(["hello", "hey"]): ', mutation(["hello", "hey"]));
console.log('mutation(["Alien", "line"]): ', mutation(["Alien", "line"]));

You only have to loop once, and pare the second array against the first.

function mutation(arr) {
    var arr1 = arr[0].split('');
    var arr2 = arr[1].split('');
    var count = 0;
    for (var i =0; i < arr2.length; i++) {
        if(arr1.indexOf(arr2[i]) > -1 ) {
            count++;
        }
    }

    if (count == arr2.length) {
        console.log('all in');
    }
}

mutation(['alien', 'line']);

Or you could use filter:

function mutation(arr) {
    var arr1 = arr[0].split('');
    var arr2 = arr[1].split('');

    if (arr2.filter(function(element, index) { return arr1.indexOf(element); }).length === arr2.length) {
        console.log('all in');
    }
}

mutation(['alien', 'line']);

The counter in the accepted answer is unnecessary.

function mutation(arr) {
  var arr1 = arr[0].toLowerCase().split('');
  var arr2 = arr[1].toLowerCase().split('');
  for (var i=0; i < arr2.length; i++) {
    if(arr1.indexOf(arr2[i]) == -1 ) {
      return false;
    }
  }
  return true;
}
mutation(["hello", "hey"]);

How about something cleaner? Just a little modification to an above code.

function mutation(arr) {

var first = arr[0].toLowerCase().split('');
var second = arr[1].toLowerCase().split('');
var count = 0;
// Check every character and if the index is found add one
for (var s in second){
    if (first.indexOf(second[s]) > -1) {
        count+= 0;
    } else
        count++;
    }
if (count === 0)
    return true;
else
    return false;
}

even lesser code for this problem

function mutation(arr) {
  var arr1 = arr[0].toLowerCase().split('');
  var arr2 = arr[1].toLowerCase().split('');
  for(var i of arr2)
    if(arr1.indexOf(i)===-1)
      return false;
  return true;
}
mutation(["hello", "hey"]);

the code above will not work in all cases because if the first word in array is shorter than second one you need second for loop

    for (var i=0; i<arr1.length; i++){
if (arr2.indexOf(arr1[i])==-1){
return false;   

you need 2 for loops

function mutation(arr) {
 var arr1=arr[0].toLowerCase().split("");
 var arr2=arr[1].toLowerCase().split("");
 if (arr1.length<arr2.length ){
 for (var i=0; i<arr1.length; i++){
if (arr2.indexOf(arr1[i])==-1){
  return false;
}
}
}
 else if (arr1.length>=arr2.length ){

 for (var j=0; j<arr2.length; j++){
if (arr1.indexOf(arr2[j])==-1){
  return false; 
}
}
}


  return true;
}
//mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
mutation([ "qrstu", "zyxwvutsrqponmlkjihgfedcba"]);
enter code here

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论