📅  最后修改于: 2023-12-03 15:15:53.347000             🧑  作者: Mango
Isogram is a piece of Javascript code that checks if a given string is an isogram or not. An isogram is a word in which no letter occurs more than once. For example, "puzzle" is an isogram, but "banana" is not.
To use Isogram in your Javascript project, you can either download the code from the Github repository or install it using npm:
npm install isogram-js
Once installed, you can import Isogram in your Javascript file and call the isIsogram()
function, passing in the word you want to check as an argument:
import isIsogram from 'isogram-js';
const word = 'puzzle';
const isIsogram = isIsogram(word);
console.log(isIsogram);
// output: true
The isIsogram()
function works by creating a frequency map of the letters in the word. If a letter appears more than once in the word, the function returns false. Otherwise, it returns true. Here is the code implementation:
export default function isIsogram(word) {
const letterFrequency = {};
for (let i = 0; i < word.length; i++) {
const letter = word[i];
if (letterFrequency[letter]) {
return false;
}
letterFrequency[letter] = 1;
}
return true;
}
We can test our Isogram function using a simple test suite. Here is an example:
import isIsogram from 'isogram-js';
describe('isIsogram', () => {
test('correctly identifies isograms', () => {
expect(isIsogram('puzzle')).toBe(true);
expect(isIsogram('jigsaw')).toBe(true);
expect(isIsogram('qjkfmxgzpvbthns'))->toBe(true);
});
test('correctly identifies non-isograms', () => {
expect(isIsogram('banana')).toBe(false);
expect(isIsogram('beetroot'))->toBe(false);
expect(isIsogram('hello world'))->toBe(false);
});
});
In this article, we introduced Isogram, a piece of Javascript code that checks if a given word is an isogram. We also saw how to install and use Isogram in your Javascript project, as well as how to test its functionality. Isogram is a simple but useful tool that can make your code more efficient and accurate.