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
function alphabetSubsequence(str) {
// write code here.
//If it has duplicates then return false
//Set just gets the unique values, so if it's not the same length as the string then there must be duplicates
if(new Set(str.split('')).size !== str.length) {
return false;
}
//Now lets check if the char before the current one is less
for(let i = 1; i < str.length; i++) {
if(str.charCodeAt(i) < str.charCodeAt(i-1)) {
return false;
}
}
//We've checked for two different reasons it's not a subsequence, so if we've got this far it must be a subsequence
return true;
}
/**
* Test Suite
*/
describe('alphabetSubsequence()', () => {
it('returns false when it has duplicate letters', () => {
// arrange
const str = 'effg';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(false);
});
it('returns false when NOT in ascending a - z order', () => {
// arrange
const str = 'cdce';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 2: ", result);
// assert
expect(result).toBe(false);
});
it('returns true whenno duplicates and is ascending a - z order ', () => {
// arrange
const str = 'ace';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 3: ", result);
// assert
expect(result).toBe(true);
});
it('returns false when NOT in ascending a - z order and no duplicates', () => {
// arrange
const str = 'dce';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 4: ", result);
// assert
expect(result).toBe(false);
});
});