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:
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.jsfile 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.
Write a function called
printGreeting with a parameter
name that returns a greeting with the argument interpolated into the greeting.
=> Hello there, Slimer!
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"
Write a function called
This function should take three arguments: two numbers and a string.
Name the parameters
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
Do the same thing for multiplication "mult", division "div", and exponent "exp" (where
num2 is the exponent of
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.
15234is 1-to-n pandigital because it is 5 numbers long and includes 1, 2, 3, 4, and 5.
333is not 1-to-n pandigital.
0is not 1-to-n pandigital.
987654321is 1-to-n pandigital.
Write a function that checks if a number is 1-to-n pandigital.
const userInput = prompt("Please enter some input");
userInputis now whatever the user entered.
alert(). You probably have heard of it. It takes a string as a parameter. Read about it on mdn.
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
8. Variable number of arguments
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: