Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
includes, indexOf solution in Clear category for Words Order by Sim0000
import assert from "assert";
function wordsOrder(text: string, words: string[]) {
const wordList = text.split(' ');
let last = -1;
let current;
for(const word of words){
if(!wordList.includes(word) || (current = wordList.indexOf(word)) <= last) return false;
last = current;
}
return true;
}
console.log('Example:');
console.log(wordsOrder('hi world im here', ['world', 'here']));
// These "asserts" are used for self-checking
assert.equal(wordsOrder('hi world im here', ['world', 'here']), true);
assert.equal(wordsOrder('hi world im here', ['here', 'world']), false);
assert.equal(wordsOrder('hi world im here', ['world']), true);
assert.equal(wordsOrder('hi world im here',
['world', 'here', 'hi']), false);
assert.equal(wordsOrder('hi world im here',
['world', 'im', 'here']), true);
assert.equal(wordsOrder('hi world im here',
['world', 'hi', 'here']), false);
assert.equal(wordsOrder('hi world im here', ['world', 'world']), false);
assert.equal(wordsOrder('hi world im here',
['country', 'world']), false);
assert.equal(wordsOrder('hi world im here', ['wo', 'rld']), false);
assert.equal(wordsOrder('', ['world', 'here']), false);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
May 9, 2020