Hoping It Works explainer


#1

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


#3

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.


#4

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


#5

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😂


#6

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.


#7

Form money changer on its getting really difficult


#8

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?


#9

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