用纯javascript检查一个单词是一个isogram

时间:2018-05-02 12:27:49

标签: javascript arrays string

如何使用函数检查给定单词是否是纯javascript的等值线图。该函数必须返回true或false。

isogram是一个具有重复字符的单词。

我知道这段代码有效,但我需要一个更好的解决方案。

function isIsogram(word){
    x = false; y = false;
    for(i = 0; i < word.length; i++){
        wordl = word.substring(0,i)
        wordr = word.substring(i)
        x = wordl.includes(word.charAt(i))
        y = wordr.includes(word.charAt(i))
        //console.log(x,wordl,wordr)
    }
    return x&&y
}
isIsogram("thomas");//False
isIsogram("moses"); //True

10 个答案:

答案 0 :(得分:2)

从字符串中删除重复的字母,然后检查两个长度。如果它是一个等值线。

&#13;
&#13;
function isIsogram(str){
 return str.split('').filter((item, pos, arr)=> arr.indexOf(item) == pos).length == str.length;
}
console.log(isIsogram('thomas'));
console.log(isIsogram('moses'));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

以kishea的回答为基础:

function isIsogram(sWord)
 {
  for (iCharIndex = 0; iCharIndex < sWord.length; iCharIndex++)
    if (sWord.substring(iCharIndex + 1).includes(sWord.charAt(iCharIndex)))
      return false;
  return true;
 }

如果在当前位置(charAt)的右侧找到当前位置(includes)的字符,则返回substring。否则循环运行到结尾并返回false

答案 2 :(得分:1)

以下是使用.split().every()的简单方法:

let isIsogram = (str) => str.split("").every((c, i) => str.indexOf(c) == i);
                            
console.log(isIsogram("thomas"));   /* no repeating letter */
console.log(isIsogram("moses"));    /* s repeat 2 times */
console.log(isIsogram("hello"));    /* l repeat 2 times */
console.log(isIsogram("world"));    /* no repeating letter */
console.log(isIsogram("a b c"));    /* space character repeat 2 times */

<强>文档:

答案 3 :(得分:1)

一种方法!

function isIsogram(str){
  return !str.match(/([a-z]).*\1/i);
}

答案 4 :(得分:0)

function isIsogram(word){
    x = false; y = false;
    for(i = 0; i < word.length; i++){
        wordl = word.substring(0,i)
        wordr = word.substring(i)
        x = wordl.includes(word.charAt(i))
        y = wordr.includes(word.charAt(i))
        //console.log(x,wordl,wordr)
    }
    return x&&y
}
isIsogram("thomas");//False
isIsogram("moses"); //True

答案 5 :(得分:0)

怎么样:

> function isIsogram(word) {
... var a = word.split('')
... var b = Array.from(new Set(a))
... return a.length === b.length;
... }
undefined
> isIsogram("mesos")
false
> isIsogram("thomas")
true

或更好(仅检查每个字符一次):

> function isIsogram2(word) {
... for(var i=0,len=word.length-1;i<len;++i) {
..... var c = word[i]
..... if(word.indexOf(c,i+1) !== -1) return false;
..... }
... return true;
... }
undefined
> isIsogram2("mesos")
false
> isIsogram2("thomas")
true

答案 6 :(得分:0)

var str=prompt('Enter a string');
    var strlen=str.length;

    for(i=0;i<strlen;i++){
       var stc=str.charAt(i);
       var flag=0;
       for(j=0;j<strlen;j++){
        var stk=str.charAt(j);
          if(stc==stk){
            flag=flag+1;
          }
       }

       if(flag!=1){
        break;
       }

    }

    if(flag!=1){
        alert('It is not an isogram');
    }
    else{
        alert('It is an isogram');
    }

答案 7 :(得分:0)

server.com

答案 8 :(得分:0)

const isIsogram = (string) => {
    
    const lowerCased = string.toLowerCase()
    
    const result = lowerCased.split('').every((v,i)=>lowerCased.indexOf(v)===i)
    
    return result

}

console.log(isIsogram('ambidExtRously')) // true
console.log(isIsogram('patteRN')) // false

答案 9 :(得分:-1)

function isIsogram(word){
   return !/(.).*\1|\d/i.test(word)
}