Explorer
project
boot.js
index.html
index.js
jasmine-html.js
jasmine.css
jasmine.js
main.js
Dependencies
Bootcamp
Study group
Collaborate with peers in your dedicated #study-group channel.
Code reviews
Submit projects for review using the /review
command in your #code-reviews channel
/** Throws if argument passed is not a string
* @param String of characters
* @returns Count of unique characters. Case sensitive: A !== a
*/
function differentSymbolsNaive(str) {
if (typeof str !== 'string') {
throw new Error('Invalid input');
}
let uniqueChars = '';
const { length } = str;
for (let i = 0; i < length; i += 1) {
if (!uniqueChars.includes(str[i])) {
uniqueChars += str[i];
}
}
return uniqueChars.length;
}
/* SOLUTION 2
function differentSymbolsNaive(str) {
if (typeof str !== 'string') {
throw new Error('Invalid input');
}
return new Set(str).size;
}
*/
/* SOLUION 3
function differentSymbolsNaive(str) {
if (typeof str !== 'string') {
throw new Error('Invalid input');
}
const uniqueChars = {};
return str.split('').reduce((charCount, char) => {
if (uniqueChars[char]) {
return charCount;
}
uniqueChars[char] = char;
return charCount + 1;
}, 0);
}
*/
/**
* Test Suite
*/
describe('differentSymbolsNaive()', () => {
it('expects differentSymbolsNaive to be a function', () => {
expect( typeof differentSymbolsNaive === 'function').toBe(true);
});
it('expects differentSymbolsNaive(true) to throw an error', () => {
expect(() => differentSymbolsNaive(true)).toThrow(new Error('Invalid input'));
});
it('expects differentSymbolsNaive("cabca") to return 3', () => {
expect(differentSymbolsNaive("cabca")).toBe(3);
});
it('expects differentSymbolsNaive("john.doe@gmail.com") to return 14', () => {
expect(differentSymbolsNaive("john.doe@gmail.com")).toBe(14);
});
it('expects differentSymbolsNaive("https://www.scrimba.com?test=hoho") to return 18', () => {
expect(differentSymbolsNaive("https://www.scrimba.com?test=hoho")).toBe(18);
});
it('expects differentSymbolsNaive("") to return 0', () => {
expect(differentSymbolsNaive("")).toBe(0);
});
it('expects differentSymbolsNaive("javascriptmas is beau") to return 14', () => {
expect(differentSymbolsNaive("javascriptmas is beau")).toBe(14);
});
it('expects differentSymbolsNaive("i\'m sexy and i know it ):") to return 14', () => {
expect(differentSymbolsNaive("i'm sexy and i know it ):")).toBe(17);
});
it('expects differentSymbolsNaive("Arrrrrggggghhhhh") to return 4', () => {
expect(differentSymbolsNaive("Arrrrrggggghhhhh")).toBe(4);
});
it('expects differentSymbolsNaive("AAAAaaaa") to return 2', () => {
expect(differentSymbolsNaive("AAAAaaaa")).toBe(2);
});
it('expects differentSymbolsNaive("https://scrimba.com/scrim/co67445b3ab196c95ef8e7966") to return 24', () => {
expect(differentSymbolsNaive("https://scrimba.com/scrim/co67445b3ab196c95ef8e7966")).toBe(24);
});
});