Week 3 - Functions and strings

Functions

String functions

JavaScript Modules

Scope

IDE navigation

DRY (Don’t Repeat Yourself)

Function size

Using LLMs for efficient learning

Practice

Assignment

Back to core program

Content

Let’s get practical

Ideas:

Exercise 1 - Making comma separated strings readable

Write a function that takes a comma separated string and makes it a readable sentence. For example:

convertToReadableString('hello,this,is,a,difficult,to,read,sentence'; // returns 'hello this is a difficult to read sentence'

Exercise 2 - Hide email address function

Sometimes we want to indicate to the user what email address we are sending an email to but not fully show the address. For example, when our system works with usernames and the user requests a new password. Write a function that hides most of the email address with asterisks (*). For example:

hideEmailAddress('[email protected]') // returns j***@doe.com
hideEmailAddress('[email protected]') // returns s******@protonmail.com

<aside> 💡

Hint: you will need to use a String function that we haven’t discussed yet. Look up indexOf

</aside>

Exercise 3 - Updating game score

Create a function called updateScore that takes a player name and points to add. The function should update a global score variable and display the current score. Then, create another function resetGame that resets the score back to zero.

let score = 0;

function updateScore(playerName, points) {
  // Your code here
  // Update the score and log: "PlayerName scored X points! Total score: Y"
}

function resetGame() {
  // Your code here
  // Reset score to 0 and log: "Game reset! Score is now 0"
}

// Test your functions
updateScore('Alice', 10);
updateScore('Bob', 5);
resetGame();
updateScore('Charlie', 8);

Questions to consider:

<aside> 💡

If you would like to practice more then remember that you can ask AI to generate more exercises for you!

</aside>


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*