Word Checker Explainer

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)
  }
);
6 Likes

I need help :frowning:

2 Likes

1 Like

I need help am stuck

My code is exact the same as the example one and keeps not working

5 Likes

Please i need a help I’m stuck on this topic. Please someone should help me

2 Likes

fuck m stuck fuck fuck

Hi,
Kindly help me here

1 Like

Am also stuck🤔.
This level seems to have error

1 Like

Greetings everyone! It seems like there is a problem with the && (AND) usage in the if’s. Just try to separate those into nested if’s and it will work

3 Likes

1 Like

Hi, you haven’t typed space(-) inside the string, like: space[ i ] !== ’ '.

Remember, space(-) is also a string!

So you must not keep the string empty there and use (-) inside the ’ ’ which looks like ‘-’.

Hope you understood🙂

1 Like

Thank you so much to both you and Hugo_Tacconi, you did such a great help! Otherwise I would have never know where to use that “return true;” keyword.

Hi, you have used underscore(_) instead of space(-).That’s where the fault is!

The ‘if statement’ should be like this: (space[ i ] !== ‘-’ not (space[ i ] !== ‘_’.

I hope it’s clear now,
Sabik

1 Like


I have done it right but it seems like I have not done it.

Me stuck. Do I have to change something?

Why we are not using else if ?

:cut_of_meat: like me out cut of meat tt