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);
  checkLength(string);
  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()

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().

–Frankie

3 Likes

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😂

1 Like

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.

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?

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).

–Frankie

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