Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
WO - SLR - Every, IndexOf, &&, Split, RegEx, Filter, Includes solution in Clear category for Words Order by Taylor_Page
import assert from "assert";
function wordsOrder(text: string, words: string[]): boolean {
return words.every((w,i) =>
// Check for no duplicates in "words"
i === words.indexOf(w) &&
// If false returns "false" else will process 2nd half of && statment
// Compare order of filtered "text" to "words"
w === text.split(/\s/g).filter(v => words.includes(v))[i])
}
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);
assert.equal(wordsOrder('hi world world im here',
['world', 'world']), false);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
July 14, 2020