Bhaj's Crossword Helper 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 takes a word and returns the last letter of the word.

Start the puzzle by adding a parameter to the lastLetter() function. Give it a name like word or something similar.

Create a local variable inside the function to store the last index value, for example: let lastIndex = word.length - 1.

Then, use this variable to return the last letter of the word, for example: return word[lastIndex]
Sample code solution:

import { randomWord } from 'cross.words';

function lastLetter(word) {
    let lastIndex = word.length - 1;
    return word[lastIndex];
}

console.log(randomWord);
console.log(lastLetter(randomWord));

Javascript Concepts: Functions, .length, Strings, Variables
Additional Code (hidden code that runs before the puzzle’s code):

const _words = ['do','if','in','for','new','try','case','else','this','void','with','break','catch','class','super','throw','while','delete','export','import','return','switch','default','extends','finally','continue','debugger','function'];

let randomWord = pickRandom(_words);
3 Likes

Hi,
I solved it differently.
Here’s a screenshot, would there be any problem with my code since I didnt use the let declaration?

2 Likes

Nope! They are the same, and the function will have the same output whichever method you choose.

2 Likes

So I solved this lab without needing the .lastIndex dot notation (which is an option), and wondered why it was there so I came to forum to see the solution and I see that you didn’t use it either. What’s that about?

Hey there,

When this course was published, it wasn’t possible to write something like this in the app:

let lastPuppy = puppies[puppies.length - 1];

Tapping the - and then num keys would have automatically made the code look like:

let lastPuppy = puppies[puppies.length] - 1;

Which is not what we want. The workaround for this at the time of release was creating a separate variable, which we typically named lastIndex, and then accessing it like this:

let lastIndex = puppies.length - 1;
let lastPuppy = puppies[lastIndex];

Later, we fixed the issue with the keyboard, so you can now use both methods.

Hope this clarifies things.
Ben

2 Likes

At the top of the code it says import randomWord from ‘cross.words’. Would there be a way that i could change the words in ‘cross.words’? I think it would be cool if we could change the words or make them come in a certain order .

1 Like

The words imported into the puzzle can’t be changed, but you can try rewriting the function in the code playground and using your own array of strings.

2 Likes

hello my code seems to still be crashing , please help.

Hey there, console.log() will print whatever argument will pass into it, but it doesn’t return anything. That means that when you have something like return console.log(), your function will return undefined, as console.log() is not returning a value.

Try changing your return statement to just return s[last]. That should fix the problem.

Ben

1 Like

Tell the mistake sir .
As it says pickRandom Not defined

Hey there, pickRandom() is a function we made for use within the Grasshopper app. It’s not part of the JavaScript standard library (meaning the functions built-in to JavaScript).

You can, however, recreate it on your own. Take a look at this post for some guidance.

Hope this helps!
Ben