Find file Copy path
b3c687c Mar 21, 2019
2 contributors

Users who have contributed to this file

@reuben @JimHaff
179 lines (99 sloc) 6.43 KB

ga logo

Functions lab!

Course: wdi-cc
Created by: GA Instructional Staff
Competencies: Arrow functions, researching with MDN, careful reading of instructions


In today's directory, clone this lab, and create and test our beloved client-side folder structure:

client side js folder structure

Commit once it's linked up.

Paste each problem in as a comment and answer below it. This is for several reasons:

  • gets you in the habit of reminding yourself what you're trying to using comments
  • the completed lab app.js file is more useful as a learning tool on its own

Do this for your next few homework assignments (3, 4, and 5) as well.

Commit after each question with a nice, terse, meaningful commit message.


Break it down!

Don't try to do everything at once! Break things down into absurdly basic steps. Remember you're much much smarter than a computer and you have to think on a very low level like a computer to be effective at giving instructions to that computer.


In this lab we mostly tell you what to name your functions (and when explicitly told a name, you should use it), but in general it will be up to you. Think carefully about what you're naming your functions. Function names should read like English verbs. A function that adds numbers should be called addNumbers. A function that figures out which user is currently logged in should be called getLoggedInUser. If a function is called check something, then it is probably seeing if a certain situation is true or not, so it should probably return a Boolean value. Someone else using your code should be able to make a pretty educated guess what your functions do (and what data your variables contain) just by how they're named.

The problems.....

1. printGreeting

Write a function called printGreeting with a parameter name that returns a greeting with the argument interpolated into the greeting.


=> Hello there, Slimer!

2. reverseWordOrder

Write a function reverseWordOrder that accepts a single argument, a string. The function should return a string with the order of the words reversed. Don't worry about punctuation.

console.log(reverseWordOrder("Ishmael me Call"));

=> "Call me Ishmael"

console.log(reverseWordOrder("I use Lâncome on my comb"));

=> "comb my on Lâncome use I"

3. calculate

Write a function called calculate.

This function should take three arguments: two numbers and a string.

Name the parameters num1, num2, and operation.

If if the function is called with the third argument as "add", it should return the sum of num1 and num2.

If if the function is called with the third argument as "sub", it should return return num1 minus num2.

Do the same thing for multiplication "mult", division "div", and exponent "exp" (where num2 is the exponent of num1).

console.log(calculate(4, 3, "sub"));

=> 1
console.log(calculate(4, 3, "exp"));

=> 64

4. pandigital numbers

Note: The following question is weird, we know. In interviews, you will absolutely be given coding challenges with "weird" questions and you'll need to be very careful when reading these types of questions to make sure you understand what you're being asked to do.

A number of length n is 1-to-n pandigital if it makes use of all the digits 1 to n exactly once.

  • The number 15234 is 1-to-n pandigital because it is 5 numbers long and includes 1, 2, 3, 4, and 5.

  • The number 333 is not 1-to-n pandigital.

  • The number 0 is not 1-to-n pandigital.

  • The number 987654321 is 1-to-n pandigital.

Write a function that checks if a number is 1-to-n pandigital.

5. printGreeting v2.0

There is a very rudimentary JavaScript function for receiving user input called prompt().


	const userInput = prompt("Please enter some input");

userInput is now whatever the user entered.

There is another rudimentary JavaScript function for displaying text called alert(). You probably have heard of it. It takes a string as a parameter. Read about it on mdn.

Let's revisit printGreeting.

First get the userInput as above. Then write a function called printGreeting2 with a parameter name that returns a greeting with the argument interpolated into the greeting as before, but this time use the alert function to display the greeting to the user.

Hungry for more?

6. Functions + loops: a special partnership

Write a function that, when called ("call" = "invoke") creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space (a "white square") or a # character (representing a black square). Hence, the characters should form a chessboard.

Calling your function should print something like this:

 # # # #
# # # # 
 # # # #
# # # # 
 # # # #
# # # # 
 # # # #
# # # #

7. Modify it to make any size grid.

When you have a function that generates this pattern, modify it to take a parameter size. Make the function work for any size, outputting a properly formatted grid of the given width and height. If it helps you to have this set. The very first square should always be white.

Remember to give it a nice semantic name

This problem was adapted from one in Eloquent Javascript so hopefully you've already seen it because you've been reading Eloquent Javascript. If you haven't yet, read the first 3 chapters (this reads great on a phone, and if you take transit, this is a great thing to read on the train/bus on your way in). Homework will be assigned soon.

8. Variable number of arguments

Modify calculate above so that it continues to work as specified in question 3, but also lets a user get the square root of a number by specifying only 2 parameters: the number they want the square root of as the first parameter, and "sqrt" as the second parameter.

Click "Details" below for a hint:

Hint: use `typeof`