Lab 10 - JavaScript Functions
In this lab, you’ll be creating several programs:
- greetings
- pluralize
- double
- factorial
- trees
Instructions
ALL OF THESE FILES MUST BE CREATED IN THE LAB 10 REPOSITORY THAT YOU CREATED FOR THIS LAB
Note that ALL SOLUTIONS TO THESE PROBLEMS MUST BE SHOW THE FORM OF A FUNCTION DEFINITION AND A CALL TO THAT FUNCTION.
greetings
Write a program that says hello three times.
- using SublimeText, create a new file called greetings.html in your repository directory: ~/Desktop/llorenzo/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 SublimeText, create a new file called pluralize.html in your repository directory: ~/Desktop/llorenzo/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 SublimeText, create a new file called factorial.html in your repository directory: ~/Desktop/llorenzo/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
trees
Write a program that draw the number of trees that the user inputs. Ask the user how many trees he/she wants, and draw that number. Its up to you to design and style what kind of tree to draw.