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

1 Like

Hello.

I’ve copied the exact solution into the field and I’m still not getting it correct. Is there something I’m missing or doing incorrectly? Thanks

Hey there, try posting a screenshot of your code. I can take a look and see what the issue is.

Thanks!
Ben

Hey there,

It looks like everything after the 1st return false is inside the code block { } of the if statement that compares the lengths of the space and the word.

Try resetting the puzzle, and making sure the for loop is added after the closing bracket of the 1st if statement.

Hope this helps!
Ben

1 Like

I still don’t get it

No problem! I can give a visual example.

An if statement runs a block of code if a certain condition is true. The block of code is surrounded by {}. In your code, everything is inside the code block of the 1st if statement. You can tell because there isn’t a closing bracket } after the return false

Your first if statement should look like:

if (space.length !== word.length) {
    return false;
}

And then below it, the for loop:

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

And then after that, the return true.

Hope that helps!
Ben

2 Likes

Great puzzle!
Learnt from this :slight_smile:

1 Like