Practice Exercises
-
Write a function
logthat takes anArrayofStrings and displays them on theconsole. -
An application uses an
Arrayas a Stack (LIFO) to keep track of items in a user's shopping history. Every time they browse to an item, you want toaddItemToHistory(item). How would you implement this? -
Write a function
buildArraythat takes twoNumbers, and returns anArrayfilled with all numbers between the given number:buildArray(5, 10)should return[5, 6, 7, 8, 9, 10] -
Write a function
addDollarsthat takes anArrayofNumbers and uses the array'smap()method to create and return a newArraywith each element having a$added to the front:addDollars([1, 2, 3, 4])should return['$1', '$2', '$3', '$4'] -
Write a function
tidythat takes anArrayofStrings and uses the array'smap()method to create and return a newArraywith each element having all leading/trailing whitespace removed:tidy([' hello', ' world '])should return['hello', 'world']. -
Write a function
measurewhich takes anArrayofStrings and uses the array'sforEach()method to determine the size of each string in the array, returning the total:measure(['a', 'bc'])should return3. Bonus: try to rewrite your code using theArray'sreduce()method. -
Write a function
whereIsWaldothat takes anArrayofStrings and uses the array'sforEach()method to create a newArraywith only the elements that contain the text"waldo"or"Waldo" somewhere in them:whereIsWaldo(['Jim Waldorf, 'Lynn Waldon', 'Frank Smith'])should return'Jim Waldorf, 'Lynn Waldon']. Bonus: try to rewrite your code using theArray'sfilter()method. -
Write a function
checkAgesthat takes two arguments: anArrayof ages (Number); and a cut-off age (Number). Your function should returntrueif all of the ages in theArrayare at least as old as the cut-off age:checkAges([16, 18, 22, 32, 56], 19)should returnfalseandcheckAges([16, 18, 22, 32, 56], 6)should returntrue. Bonus: try to rewrite your code using theArray'severy()method. -
Write a function
containsBadWordthat takes two arguments:badWords(anArrayof words that can't be used), anduserName(aStringentered by the user). Check to see if any of the words inbadWordsare contained withinuserName. Return thebadWordthat was found, ornullif none are found. -
A
Stringcontains a Key/Value pair separated by a":". UsingStringmethods, how would you extract the two parts? Make sure you also deal with any extra spaces. For example, all of the following should be considered the same:"colour: blue","colour:blue","colour : blue","colour: blue ". Bonus: how could you use aRegExpinstead? -
A
Stringnamedaddressescontains a list of street addresses. Some of the addresses use short forms:"St."instead of"Street"and"Rd"instead of"Road". UsingStringmethods, convert all these short forms to their full versions. -
Room booking codes must take the following form: room number (
1-305) followed by-followed by the month as a number (1-12) followed by the day as a number (1-31). For example, all of the following are valid:"1-1-1","250-10-3","66-12-12". Write aRegExpto check whether a room booking code is valid or not, which allows any of the valid forms. -
Write a function that takes a
Stringand checks whether or not it begins with one of"Mr.","Mrs.", or"Ms.". Returntrueif it does, otherwisefalse. Bonus: try writing your solution using regularStringmethods and again as aRegExp. -
Write a function that takes a password
String, and validates it according to the following rules: must be between 8-32 characters in length; must contain one Capital Letter; must contain one Number; must contain one Symbol (!@#$%^&*-+{}). Returntrueif the given password is valid, otherwisefalse. -
Write a
RegExpfor a Canadian Postal Code, for example"M3J 3M6". Allow spaces or no spaces, capitals or lower case.
A Larger Problem Combining Everything:
You are asked to write JavaScript code to process a String which is in the form of a Comma-Separated Values (CSV) formatted data dump of user information. The data might look something like this:
0134134,John Smith,555-567-2341,62 inches
0134135 , June Lee , 5554126347 , 149 cm
0134136, Kim Thomas , 5324126347, 138cm`
Write a series of functions to accomplish the following, building a larger program as you go. You can begin with exercise.js:
-
Split the string into an
Arrayof separate rows (i.e., anArraywith rows separated by\n). Bonus: how could we deal with data that includes both Unix (\n) and Windows (\r\n) line endings? -
Each row contains information user info:
ID,Name,Phone Number, andHeightinfo all separated by commas. Split each row into anArraywith all of its different fields. You need to deal with extra and/or no whitespace between the commas. -
Get rid of any extra spaces around the
Namefield -
Using a
RegExp, extract the Area Code from thePhone Numberfield. AllPhone Numbers are in one of two formats:"555-555-5555"or"5555555555". -
Check if the
Heightfield has"cm"at the end. If it does, strip that out, convert the number to inches, and turn it into aStringin the form"xx inches". For example:"152 cm"should become"59 inches". -
After doing all of the above steps, create a new record with
ID,Name,Area Code,Height In Inchesand separate them with commas -
Combine all these processed records into a new CSV formatted string, with rows separated by
\n.
A sample solution is provided in solution.js.