Skip to content

Instantly share code, notes, and snippets.

@matt-winzer
Last active September 19, 2019 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-winzer/263bef0e5ba08ce823d7f6367e53fa97 to your computer and use it in GitHub Desktop.
Save matt-winzer/263bef0e5ba08ce823d7f6367e53fa97 to your computer and use it in GitHub Desktop.

Hack Reactor: JavaScript 101 Workshop Solutions

Solutions to the exercises found here.

NOTE: Perfectly valid solutions can be written in a different style than the solutions below. If your code works, it works! Don't feel like your functions need to have the exact same syntax as these solutions.

Exercises

Exercise 1. Restaurant Tip Calculator

  1. Define a function named calculateTip and give it two arguments: total and percent

  2. Inside your function, use the return keyword to output a tip according to the percent argument.

Extra Credit: Want to shave off the extra decimal places? Click here to learn about the .toFixed() function and how to use it.

In the example below, your function should output 7.965.

var calculateTip = function(total, percent) {
  var tip = total * (percent / 100);
  return tip;
}

calculateTip(44.25, 18)

Exercise 2. Picking apart a URL

  1. Define a function named removePrefix and give it one argument: url

  2. Inside your function, return the url without "www." included. Hint: Use .slice

Hint: Don't forget to wrap quotes around your strings!

In the example below, your function should output google.com.

var removePrefix = function(url) {
  return url.slice(4);
}

removePrefix('www.google.com')

Exercise 3. Random Number Generator

  1. Define a function named randomNumberGen with no arguments.

  2. Inside your function, return a random number between 1 and 10. Hint: Use Math.random() and Math.floor(number)

In the example below, your function should output a whole number between 1 and 10.

var randomNumberGen = function() {
  var randomNumber = Math.random() * 10 + 1;
  var roundedRandomNumber = Math.floor(randomNumber);
  return roundedRandomNumber;
}

randomNumberGen()

Super Extra Credit: "Sentence Reverser"

  1. Define a function named reverseSentence with one argument: sentence

  2. Inside your function, return the reversed copy of the sentence that is passed in as an argument. Hint: Use String.split() Array.reverse() and Array.join()

In the example below, your function should output "!ecnetnes emosewa na si sihT".

var reverseSentence = function(sentence) {
  var sentenceArray = sentence.split('');
  var reversedSentenceArray = sentenceArray.reverse();
  var reversedSentenceString = reversedSentenceArray.join('');
  return reversedSentenceString;
}

reverseSentence('This is an awesome sentence!')

A more concise version:

var reverseSentence = function(sentence) {
  return sentence.split('').reverse().join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment