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);
2 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?

1 Like

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

1 Like

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

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 .

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.

1 Like