Bhaj's Word Checker explainer

Aim of the puzzle: Find the key information in a word problem and solve the problem using code.
Walk through of solution: Bhaj wants a function that checks if a word could potentially fit as a crossword answer.

This function will take a word, for example, 'cat', and a space, for example, '-a-', and will return true if the word can fit in the space, and false if it cannot.

First, we know that if the word and the space are different lengths, the word will not fit. Therefore, add an if statement that compares the lengths, and returns false if they are not equal. This will take care of any words that are too long or too short.

Next, we need to check the words that are the right length. Add a classic for loop to iterate through the space.

Inside the for loop, add an if statement. This if statement should check 2 things. 1st, we want to check the characters in the space that are not '-'.

2nd, we want to check if the characters that are not '-' are not equal to the character in the same position in the word.

It will look like this:

    for (let i = 0; i < space.length; i++) {
        if (space[i] !== '-' && space[i] !== word[i]) {
            return false;
        }
    }

Finally, at the end of the function, return true. The function will only execute this line of code if the for loop finishes iterating without returning false early.

Sample code solution:

import { randomWord } from 'cross.words';

function check(space, word) {
    if (space.length !== word.length) {
        return false;
    }
    for (let i = 0; i < space.length; i++) {
        if (space[i] !== '-' && space[i] !== word[i]) {
            return false;
        }
    }
    return true;
}

console.log('-a-t');
console.log(randomWord);
console.log(check('-a-t', randomWord));

Javascript Concepts: Functions, If Statements, For Loops, Strings, Bracket Notation, Indexing, Return Statements