将字符串转换为数组再转回字符串时出现的Javascript问题

3

我正在尝试解决Codewars上的一个Javascript挑战。

一个“同构词”是指不重复使用字母的单词,无论是连续的还是非连续的。请实现一个函数,用于确定只包含字母的字符串是否为“同构词”。假设空字符串是一个“同构词”,忽略字母大小写。

isIsogram( "Dermatoglyphics" ) == true
isIsogram( "aba" ) == false
isIsogram( "moOse" ) == false // -- ignore letter case

我的翻译如下:

    function isIsogram(str) {
    var arr = str.split("");
    var seen = {};
    var out = [];
    var length = arr.length;
    var j = 0;
    for(var i = 0; i < length; i++) {
         var item = arr[i].toLowerCase;
         if(seen[item] !== 1) {
               seen[item] = 1;
               out[j++] = item;
         }
    }
    console.log(out.toString.toLowerCase);
    console.log(str.toLowerCase);
    if (out.toString.toLowercase === str.toLowerCase) {
     return true;
    }
    else {
      return false;
    }
}

在Codewars中,我的结果是:
console.log(out.toString.toLowerCase); is undefined 

并且结果为

console.log(str.toLowerCase); is [Function: toLowerCase].

这意味着我的解决方案始终评估为false。我希望能得到任何指引或者指出我的错误而不是给我具体的解决方案,这样我可以更有效地学习。谢谢!
5个回答

2
这可能是一个更简单的答案。

function isIsogram(str){

  // Turn all letters of the string to lower case and split it into an array. 

  var letters = str.toLowerCase().split('');
  var checkLetters = [];
  
  /* Check to see if the letter appears in the checkLetters array.
     If the letter is not already in the array it will push the letter into it. */

  letters.forEach(function(letter) {
    if(checkLetters.indexOf(letter) === -1) {
      checkLetters.push(letter);
    }
  });

  /* Now we have two arrays. If the letters array has non-duplicate letters then 
     it will be the same length as the checkLetters array. If not, the checkLetters array
     will be shorter. */

  /* Return true or false depending on whether the lengths of both arrays are equal */
    
  return letters.length === checkLetters.length ? true : false;

}


1

toStringtoLowerCase是函数。在JavaScript中,要执行一个函数,必须在函数名后面加上括号:

out.toString().toLowerCase()
//          ^^            ^^

你需要对所有的函数都这样做:
arr[i].toLowerCase()

str.toLowerCase

out.toString.toLowercase() === str.toLowerCase()

(请注意,调用数组的.toString()方法会包含逗号,例如"a,b,c,d,e"。在这种情况下可能并不重要,但只是为了强调一下)

1

toString和toLowerCase等是函数

使用:

out.toString().toLowerCase()

然而,对于out来说,我认为你想要执行out.join('').toLowerCase()


0

我认为你可以通过使用哈希表系统和every方法,以函数式的方式改进你的解决方案。

每个方法对数组中的每个元素执行一次提供的回调函数,直到找到其中一个回调返回假值为止。

因此,你的代码将更加简洁。

  function isIsogram(str) {
    //Create our hashmap
    var hashmap = {};
    //Return value of 'every' loop
    return str.split("").every(function(elm){
      //If our hashmap get current letter as key
      return hashmap.hasOwnProperty(elm.toLowerCase())
      //Return false, str is not an Isogram
      ? false
      //Otherwise, set letter as key in our hashmap,
      //we can check the next iteration
      : hashmap[elm.toLowerCase()] = true;
    });
  }

console.log(isIsogram('Dermatoglyphics'));
console.log(isIsogram('moOse'));
console.log(isIsogram('aba'));

0

我知道你已经解决了这个问题,但为了多样性。 通过忽略大小写,首先将输入改为小写。

function isIsogram(str) {
     // Change word to lower case
    str = str.toLowerCase();
      // split the word into letters and store as an array
    var arr = str.split("");
      // get the length so the array can be looped through
    var len = arr.length;
      // an array to contain letters that has been visited
    var seen = []
    for (var i = 0; i < len; i++) {
        // check if letter has not been visited by checking for the index
      if(seen.indexOf(arr[i])<0){
         // if the letter doesn't exist,add it to seen and go to next letter
        seen.push(arr[i]);
      }else{
         // if letter exists, the word is not an isogram return false
        return false
      }
    }
       // the loop is complete but each letter appeared once, the word is an isogram
   return true
}

console.log(isIsogram('Dermatoglyphics'));
console.log(isIsogram('moOse'));
console.log(isIsogram('aba'));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接