Practice Exercises
For each of the following, write a function that takes the given arguments, and returns
or produces (e.g., console.log) the given result.
-
Given
r(radius) of a circle, calculate the area of a circle (A = π _ r _ r). -
Simulate rolling a dice using
random(). The function should allow the caller to specify any number of sides, but default to 6 if no side count is given:roll()(assume 6 sided, return random number between 1 and 6) vs.roll(50)(50 sided, return number between 1 and 50). -
Write a function that converts values in Celcius to Farenheit:
convert(0)should return"32 F". -
Modify your solution to the previous function to allow a second argument:
"F"or"C", and use that to determine what the scale of the value is, converting to the opposite:convert(122, "F")should return"50 C". -
Function taking any number of arguments (
Numbers), returningtrueif they are all less than 50:isUnder50(1, 2, 3, 5, 4, 65)should returnfalse. -
Function allowing any number of arguments (
Numbers), returning their sum:sum(1, 2, 3)should return6. -
Function allowing any number of arguments of any type, returns
trueonly if none of the arguments is falsy.allExist(true, true, 1)should returntrue, butallExist(1, "1", 0)should returnfalse. -
Function to create a JavaScript library name generator:
generateName("dog")should return"dog.js" -
Function to check if a number is a multiple of 3 (returns
trueorfalse) -
Check if a number is between two other numbers, being inclusive if the final argument is true:
checkBetween(66, 1, 50, true)should returnfalse. -
Function to calculate the HST (13%) on a purchase amount
-
Function to subtract a discount % from a total. If no % is given, return the original value.
-
Function that takes a number of seconds as a
Number, returning aStringformatted like"X Days, Y Hours, Z Minutes"rounded to the nearest minute. -
Modify your solution above to only include units that make sense:
"1 Minute"vs."3 Hours, 5 Minutes"vs."1 Day, 1 Hour, 56 Minutes"etc -
Function that takes any number of arguments (
Numbers), and returns them in reverse order, concatenated together as a String:flip(1, 2, 3)should return"321" -
Function that takes two
Numbers and returns their sum as anIntegervalue (i.e., no decimal portion):intSum(1.6, 3.333333)should return4 -
Function that returns the number of matches found for the first argument in the remaining arguments:
findMatches(66, 1, 345, 2334, 66, 67, 66)should return2 -
Function to log all arguments larger than
255:showOutsideByteRange(1, 5, 233, 255, 256, 0)should log256to theconsole -
Function that takes a
Stringand returns its value properly encoded for use in a URL.prepareString("hello world")should return"hello%20world" -
Using the previous function, write an enclosing function that takes any number of
Stringarguments and returns them in encoded form, concatenated together like so:"?...&...&..."where "..." are the encoded strings.buildQueryString("hello world", "goodnight moon")should return"?hello%20world&goodnight%20moon" -
Function that takes a
Functionfollowed by any number ofNumbers, and applies the function to all the numbers, returning the total:applyFn(function(x) { return x * x;}, 1, 2, 3)should return 14.
After you try writing these yourself, take a look at a possible solution