Your assignment is to write the following functions in the descriptions below - good luck!
Part 1
difference
- this function takes in two parameters and returns the difference of the two;
difference(2,2);
difference(0,2);
product
- this function takes in two parameters and returns the product of the two;
product(2,2);
product(0,2);
printDay
- this function takes in one parameter (a number from 1-7) and returns the day of the week (1 is Sunday, 2 is Monday, 3 is Tuesday etc.). If the number is less than 1 or greater than 7, the function should return undefined;
printDay(4);
printDay(41);
lastElement
- this function takes in one parameter (an array) and returns the last value in the array. It should return
undefined
if the array is empty.
lastElement([1,2,3,4]);
lastElement([]);
numberCompare
- this function takes in two parameters (both numbers). If the first is greater than the second, this function returns "First is greater". If the second number is greater than the first, the function returns "Second is greater". Otherwise the function returns "Numbers are equal"
numberCompare(1,1);
numberCompare(2,1);
numberCompare(1,2);
singleLetterCount
- this function takes in two parameters (two strings). The first parameter should be a word and the second should be a letter. The function returns the number of times that letter appears in the word. The function should be case insensitive (does not matter if the input is lowercase or uppercase). If the letter is not found in the word, the function should return 0.
singleLetterCount('amazing','A');
singleLetterCount('Inf-Paces School','o');
Part 2
multipleLetterCount
- this function takes in one parameter (a string) and returns an object with the keys being the letters and the values being the count of the letter.
multipleLetterCount("hello");
multipleLetterCount("person");
arrayManipulation
- this function should take in at most four parameters (an array, command, location, and value).
- If the command is "remove" and the location is "end", the function should remove the last value in the array and return the value removed. (In this case, the function only needs three arguments.)
- If the command is "remove" and the location is "beginning", the function should remove the first value in the array and return the value removed. (In this case, the function only needs three arguments.)
- If the command is "add" and the location is "beginning", the function should add the value (fourth parameter) to the beginning of the array and return the array.
- If the command is "add" and the location is "end", the function should add the value (fourth parameter) to the end of the array and return the array.
arrayManipulation([1,2,3], "remove", "end");
arrayManipulation([1,2,3], "remove", "beginning");
arrayManipulation([1,2,3], "add", "beginning", 20);
arrayManipulation([1,2,3], "add", "end", 30);
isPalindrome
- A Palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. This function should take in one parameter and returns
true
or false
if it is a palindrome. As a bonus, allow your function to ignore whitespace and capitalization so that isPalindrome('a man a plan a canal Panama');
returns true
isPalindrome('testing');
isPalindrome('tacocat');
isPalindrome('hannah');
isPalindrome('robert');
Part 3
Rock / Paper / Scissor
- using your knowledge so far, build a game of Rock/Paper/Scissor where through the use of the
prompt
function, a user can enter their choice and based on a random selection - they can either tie/win or lose against a computer.
Code
You can find the code to all solutions here