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
Additional Code (hidden code that runs before the puzzle’s code):

const _words = ['bait','cart','cast','daft','dart','east','fact','fast','gait','gast','halt','last','malt','mast','pact','pant','part','past','raft','rant','salt','tact','tart','vast','waft','wait','want','wart','watt','cats','bats','rats','bate','bath','bats','cats','data','date','eats','fate','fats','gate','hats','late','mate','math','oath','oats','rate','rats','baits','darts','attach','attack','attain','strata','tartan','tartar','tatars','aeronautics','alterations','astronautic','automations','devastation','evacuations','evaluations','salutations','situational','accusation','aeronautic','agitations','alteration','automating','automation','gelatinous','hesitation','infatuated','passionate','poinsettia','quotations','restaurant','valuations','adult','basil','bones','doses','early','heads'];

defineModule(
  'cross.words',
  {
    randomSpace: pickRandom(_words).split('').map(i=>pickRandom([i,'-'])).join(''),
    randomWord: pickRandom(_words)
  }
);
3 Likes

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

2 Likes

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

3 Likes

Great puzzle!
Learnt from this :slight_smile:

2 Likes

Hi, please check my code, is it good and proper code or not ?

The function itself looks great, except you are returning the string 'match' or 'not match', when we want to instead return true or false.

Delete the strings, and replace them with the true or false keys from the keyboard.

Hope this helps! Let me know if you have any questions.
Ben

Hi, i dont know what is wrong.

Hey there,

It looks like you’re returning a string with the text 'false' in your first if statement, rather than the boolean value false.

Delete the string, and replace it with the false key on the keyboard.

Hope this helps!
Ben

Can’t get my code to Work. What is wrong?

How i posting a screenshot?

How do you post the screenshot?

The if statement is only returning ‘false’ when the test running through it is true and it is not returning ‘true’ when the test running through it is false.

So to complete the puzzle add :

return true
After the {} of the if statement.

I hope you understand the problem. Only solving the puzzle and not understanding is a waste of time.

1 Like

Thanks, i think i got it!

Hope this helps!
Ben

Ben, I dont hace this picturei dont hace this picture

Taking a screenshot depends on what kind of device you are using. Try searching for a tutorial on the internet, for example, googling “How to take a screenshot on Android”

Good luck!