scrimba
Note at 0:00
Go Pro!Bootcamp

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

Note at 0:00
AboutCommentsNotes
Note at 0:00
Expand for more info
main.js
run
preview
console
function alphabetSubsequence(str) {
// write code here.
orderCheck = () => {
//loops through original string returning true if next character is greater than last alphabetically
const alphabet = "abcdefghijklmnopqrstuvwxyz";
//initialise result as true
let result = true;
//loop through each character (except last as nothing to compare against next)
for( let i = 0; i <= str.length -2; i++){
//easier names for current and next item
let currentCharIndex = alphabet.indexOf(str[i])
let nextCharIndex = alphabet.indexOf(str[i+1])

//return false if next item is not greater than current item
if ( nextCharIndex <= currentCharIndex){
result = false;
break;
}
}
return result;
}


duplicateCheck = () => {
//creates new set (unique) and returns true if set size is smaller than str length
const uniqueChars = new Set(str);
return str.length > uniqueChars.size? true: false;
}

//returns true if both checks are correct (duplicate check should be false)
return orderCheck() && (!duplicateCheck()) ? true : false;
}

/**
* 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 when no 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);
});
});
Console
"result 1: "
,
false
,
"result 3: "
,
true
,
"result 2: "
,
false
,
/index.html
LIVE