How to do Nested For Loops in Functional Style

泪湿孤枕 提交于 2019-12-05 19:18:47

You can use every or some together with a suitable string function:

function isIsogram(string) {
    string = string.toLowerCase(); // case insensitive
    return string.split('').every(function(character, index) {
        return !string.includes(character, index+1);
    });
}

Instead of includes you might also have utilised indexOf.

You can sort the String first and then apply every on it. It will stop the iteration as soon as two successive letters are the same:

Here is an improved implementation. Credit goes to @Xotic750:

function isIsogram(x) {
  return Array.from(x.toLowerCase()).sort().every((y, i, xs) => i === 0 
   ? true
   : y !== xs[i - 1]);
}

console.log( isIsogram("consumptively") );
console.log( isIsogram("javascript") );

The implementation uses Array.prototype.every's second parameter, which represents the index of the current element (of the iteration). Please note that isIsogram solely depends on functions and their arguments.

Another example, like @Bergi but using some ES6 features for comparison.

function isIsogram(string) {
  string = string.toLowerCase(); // case insensitive
  for (let character of Array.from(string).entries()) {
    if (string.includes(character[1], character[0] + 1)) {
      return false;
    }
  }
  return true;
}

console.log(isIsogram('abc'));
console.log(isIsogram('abca'));

How your ES3 style code could have looked (noting some of the issues pointed out in the comments)

function isIsogram(string) {
  string = string.toLowerCase(); // case insensitive
  var length = string.length;
  for (var i = 0; i < length; i += 1) {
    for (var j = i + 1; j < length; j += 1) {
      if (string.charAt(i) === string.charAt(j)) {
        return false;
      }
    }
  }
  return true;
}

console.log(isIsogram('abc'));
console.log(isIsogram('abca'));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!