Source

Source: Exercism.io > C exercises > Isogram

Description

Return true if a word or phrase is an isogram.

Define an isogram as a word or phrase without a repeating alphabetic character. Exclude numerals, spaces, and punctuation characters.

1
2
3
4
5
6
7
8
9
10
"lumberjack" ⟹ true
"background" ⟹ true
"downstream" ⟹ true

"lumberjack zip toys" ⟹ true
"the background is"   ⟹ true

"isograms" ⟹ false

"lumberjacks zip around" ⟹ false

My solutions

Imperative solution.

1
2
3
4
5
6
7
8
const isIsogram = s => {
    const uniq = [];
    for (const c of s) {
        if (uniq.includes(c)) return false;
        if (c !== ' ') uniq.push(c);
    }
    return true;
};

Functional solution.

1
2
3
4
const despace = s => s.replace(/\s/g, '');

const isIsogram = s =>
    new Set(despace(s)).size === despace(s).length;

1
module.exports = {isIsogram};

Tests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const assert = require('assert')
    , isIsogram = require('./isogram.js').isIsogram;

describe('isogram word tests', function() {
    it('should return true for isogram words', function(){
        assert(isIsogram('lumberjack'));
        assert(isIsogram('background'));
        assert(isIsogram('downstream'));
    });
});

describe('isogram phrase tests', function() {
    it('should return true for isogram phrases', function(){
        assert(isIsogram('lumberjack zip toys'));
        assert(isIsogram('the background is'));
    });
});

describe('non-isogram tests', function() {
    it('should return false for non-isograms', function(){
        assert(!isIsogram('isograms'));
        assert(!isIsogram('lumberjacks zip around'));
    });
});