Favorite Animal 1
Let’s discuss parameters and arguments in the context of the following example function:
function favoriteAnimal(animal) {
return animal + " is my favorite animal!"
}
console.log(favoriteAnimal("Goat"))
Examples
I tried the following examples:
console.log(favoriteAnimal("cheetah"))
Assignment
Let’s write some functions! Write these in the script tag of a skeleton HTML file. If you’ve forgotten how to set it up, review the instructions from Fundamentals Part 1.
/**
* 1 - Write a function called add7 that takes one number and returns that number + 7.
* 2 - Write a function called multiply that takes 2 numbers and returns their product.
* 3 - Write a function called capitalize that takes a string and returns that string with only the first letter capitalized
* Make sure that it can take strings that are lowercase, UPPERCASE or BoTh.
* 4 - Write a function called lastLetter that takes a string and returns the very last letter of that string:
* lastLetter("abcd") should return "d"
*/
Solution
Description
/* Assignment */
// (1)
function add7(number) {
return (number + 7);
}
console.log(add7(-42));
console.log(add7(0));
console.log(add7(42));
// (2)
function multiply(num1, num2) {
return (num1 * num2);
}
console.log(multiply(42, 42));
console.log(multiply(5, "" + 2));
console.log(multiply(NaN, -42));
// (3)
function capitalize(str) {
let firstLetterCapitalized;
firstLetterCapitalized = str[0].toUpperCase();
return (firstLetterCapitalized + str.substr(1));
}
let str = "hello Mama! i need to go home.";
console.log(str, " => ", capitalize(str));
str = "testing this out!"
console.log(str, " => ", capitalize(str));
// (4)
function lastLetter(str) {
let lastIndex = str.length - 1;
return str[lastIndex];
}
console.log(lastLetter("abcd"));
console.log(lastLetter("pulgamecanica"));