Lab 09 - JavaScript Functions
In this lab, you’ll be creating several programs:
- greetings
- pluralize
- double
- factorial
Instructions
Setup
ProTip: Bookmark the following page so that you can easily repeat these steps in your future labs!
Cloning a Remote Repo Using Your GitHub API Access Token
- On GitHub, create and name your new repository lab-09-functions and clone it inside your /mtec1003 folder.
- Once you've completed the steps at the link above, you're ready to begin.
Open up your text editor (e.g. Atom, Sublime Text, etc.)... - Note: ALL FILES YOU CREATE BELOW MUST BE SUBMITTED IN YOUR NEW REPOSITORY THIS WEEK.
Note: ALL SOLUTIONS TO THESE PROBLEMS MUST BE SHOWN IN THE FORM OF A FUNCTION DEFINITION AND A CALL TO THAT FUNCTION.
greetings
Write a program that says hello three times.
- using your text editor (e.g. SublimeText, Atom, VS Code, etc.), create a new file called greetings.html in your repository directory: ~/myClasses/mtec1003/lab-10-functions/
- setup an html file, and add script tags… start writing your JavaScript between the script tags
- the program should say “hello” FIVE times in the JavaScript console
- do this by DEFINING a function called greet()
- the greet() function will print out hello exactly once
- CALL the greet() function five times
- use git status, add, commit, and push to save your file in version control and submit it
- example output below…
hello
hello
hello
hello
hello
pluralize
Write a program that creates a plural version of a word inputted by a user.
- using your text editor (e.g. SublimeText, Atom, VS Code, etc.), create a new file called pluralize.html in your repository directory: ~/myClasses/mtec1003/lab-10-functions/
- setup an html file, and add script tags… start writing your JavaScript between the script tags
- define a function called pluralize(word)
- it should have one parameter, word
- it should give back a value - the plural version of the word passed in
- ask for a word: “Word please:”
- it should call your pluralize function
- pluralize will give back the word with an s appended to the end of it
- use the result of calling your pluralize function to print the plural version to the JavaScript console
- add an if-else-if statement to handle the following words: moose (moose), automaton (automata), mouse (mice)
- use git status, add, commit, and push to save your file in version control and submit it
- example interaction is below (everything after the greater than sign (>) is user input using the prompt function):
(prompt) Word please:
> car
cars
double
Write a program that calculates the twice of a number entered by the user and prints it to the console log. The function should be called - double(n). First define the function. Then ask the user for a input of a number to be doubled. Then parseInt that input value and then use it in a function call to double(n).
factorial
Write a program that calculates the factorial of a number entered by the user. Factorial is the product of all positive integers less than or equal to a number. 4! (or 4 factorial) = 4 * 3 * 2 * 1 = 24
- using your text editor (e.g. SublimeText, Atom, VS Code, etc.), create a new file called factorial.html in your repository directory: ~/myClasses/mtec1003/lab-10-functions/
- setup an html file, and add script tags… start writing your JavaScript between the script tags
- define a function called factorial(n)
- it should have one parameter, n
- it should give back an number - the product of all positive numbers less than or equal to the user input
- ask for a word: “Number please::”
- it should then CALL your factorial function
- factorial will give back the result of the calculation
- use the result of calling your factorial function to print the factorial
- hint: use a for loop to do this and use the function that returns a number from a string
- example interaction is below (everything after the greater than sign (>) is user input using the prompt function):
(prompt) Number please:
> 4
24