2.6 KiB
2.6 KiB
title | id | challengeType | forumTopicId |
---|---|---|---|
ABC Problem | 594810f028c0303b75339acc | 5 | 302220 |
Description
(B O) (X K) (D Q) (C P) (N A) (G T) (R E) (T G) (Q D) (F S) (J W) (H U) (V I) (A N) (O B) (E R) (F S) (L Y) (P C) (Z M)
Instructions
- Once a letter on a block is used, that block cannot be used again.
- The function should be case-insensitive.
Tests
tests:
- text: <code>canMakeWord</code> should be a function.
testString: assert(typeof canMakeWord === 'function');
- text: <code>canMakeWord</code> should return a boolean.
testString: assert(typeof canMakeWord('hi') === 'boolean');
- text: <code>canMakeWord("bark")</code> should return true.
testString: assert(canMakeWord(words[0]));
- text: <code>canMakeWord("BooK")</code> should return false.
testString: assert(!canMakeWord(words[1]));
- text: <code>canMakeWord("TReAT")</code> should return true.
testString: assert(canMakeWord(words[2]));
- text: <code>canMakeWord("COMMON")</code> should return false.
testString: assert(!canMakeWord(words[3]));
- text: <code>canMakeWord("squAD")</code> should return true.
testString: assert(canMakeWord(words[4]));
- text: <code>canMakeWord("conFUSE")</code> should return true.
testString: assert(canMakeWord(words[5]));
Challenge Seed
function canMakeWord(word) {
}
After Test
const words = ['bark', 'BooK', 'TReAT', 'COMMON', 'squAD', 'conFUSE'];
Solution
function canMakeWord(word) {
const characters = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM';
const blocks = characters.split(' ').map(pair => pair.split(''));
const letters = [...word.toUpperCase()];
let length = letters.length;
const copy = new Set(blocks);
letters.forEach(letter => {
for (let block of copy) {
const index = block.indexOf(letter);
if (index !== -1) {
length--;
copy.delete(block);
break;
}
}
});
return !length;
}