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