Hoping It Works explainer

The aim of this puzzle: Add a length check to the updateName function definition.

Walkthrough of the solution: The checkLength function is already completed. It takes a string and logs either “Username is too long” or “Username updated” depending on whether the string is more or less than 20 characters.

The updateName function is almost complete. It will replace the typo using the .replace() method, but it should also include a length check after the replacement, just in case the replacement made the username too long.

Since the checkLength() function does exactly what we need, we can simply add checkLength(string) to the updateName function definition {}. Make sure to do this check before the return string, because once the return command runs, the function is over and no more lines of code after it will run.

Sample code solution:
(Tap below to reveal)

function checkLength(string) {
  string.length > 20 ? console.log('Username too long') : console.log('Username updated');

function updateName(string, oldPart, newPart) {
  string = string.replace(oldPart, newPart);
  return string;

console.log('Old: ' + username);
console.log('New: ' + updateName(username, 'grasshoper', 'grasshopper'));

JavaScript Concepts: Binary Expression (>), Code Block (function), Conditionals (ternary statement), console.log(), .length(), .replace()
Additional Code (hidden code that runs before the puzzle’s code):

let username = 'grasshoperTraveler';

Hey, Just needed to add a side note about learning code. Personally started to learn java with this app in its prime and it was easy, some bugs along the way but sure. In all the sidenote is about learning. E.G. Fundamentals II/ defined a function, the pick random was not explained as its own pre-programmed function. Without that information, as a beginner I would have a hard time inferring that fact. Ty.

1 Like

This post lists some of the commonly used functions that are specific to Grasshopper. It also gives some information about how you could implement your own versions of the functions.

There aren’t any puzzles that teach the various JavaScript Math features, like Math.random() or Math.floor(), but hopefully we’ll have those soon. That would be a nice opportunity to implement your own pickRandom().



Personally I don’t understand this, I mean I got the puzzle but I don’t get it . Like I need an explanation in English and not code please. I’ll be really grateful if you can help me. Coding is just my jam😂


I don’t get it, even if I see the answer, it don’t make sense to me how witch code is connected to the other. I really want to know and learn, if somebody can explain please help me.

1 Like

Form money changer on its getting really difficult

Why in checkLength(string) function there is no return comand? On other hand on updateName() funftion there is return comand?
It seems that function within code block will execute without return command, is it correct?

1 Like

Correct. A return isn’t always needed inside of a Function Definition. If there is no return then that usually means the function has edited some global variable, but in this case, the result of running the function is just logging to the console.

But this is different than “returning a string”. The checkLength function does not return anything (its return value is undefined).


what is the different betwen IF and ternary statement ? why use ternary in this code ?

The advantage of using a ternary statement is you can write an if…else statement using less code. To the computer, a ternary is almost the same as an if…else statement. Both say “if this is true, run this code, or else run that code.”

The tradeoff with a ternary statement is that it can be a bit trickier to read by humans.

If you wanted to use an if…else instead of a ternary in the checkLength() function, you could rewrite it like this:

function checkLength(string) {
    if (string.length > 20) {
        console.log('Username too long');
    } else {
        console.log('Username updated');
1 Like

I cant understand why the check.length is displaying in between both the other console.log

Hey there, happy to help, but I’m not sure I understand your question.

Are you asking why the checkLength function is called inside updateName, or why there are 2 console.log statements inside the checkLength function?


Oh yes actually i am asking you 2 questions first of all why checklength(string ) is in the function updatename
And another thing when we play the code why it shows username updated in between

1 Like

Same question. Why does “Username updated” appear before “New:”, not after it?

1 Like

Hey there,

The updateName() function first replaces part of the string, then calls the checkLength() function, which will print a message depending on the string’s length.

As for why 'username updated' appears before 'New:', this is because updateName() is being called inside the parentheses of console.log().

This is telling the computer, “Run the updateName() function, then use whatever is returned inside the console.log().” This means that the updateName() function runs first, which prints “username updated”, then the console.log() function runs.

Hope that helps!

1 Like

Thanks, Ben. That makes sense.

1 Like

Too confusing. And I don’t always follow how the “starter code” got destroyed and I cannot see a “reset” button to reinstate the starter code, so I have no choice but to give up. Messages/explanations are too cryptic. Not allowed to experiment; It just rejects my attempt and I am locked out with a message I don’t understand.

Hey there, if you’re in the mobile app, you can reset the starter code with this button:

If you’re using the website, use this button:

Hope this helps!