FUNCTIONS
Text
function makeCats(catName) {
console.log("I have a cat named " + catName);
}
makeCats("Sammy");
What does this print out in the console?
Let's make our first function!
Open up JSFiddle
function functionName(param1, param2, param3, param4) {
//code here
}
function makeAnimals(animal1, animal2,
animal1name, animal2name) {
//code here
}
IMPORTANT NOTE: YOUR PARAMETERS SHOULD BE ON ONE LINE, I HAVE "animal1name" on a newline for formatting purposes only!!!
function makeAnimals(animal1, animal2,
animal1name, animal2name) {
//code here
}
What are the parameters of makeAnimals?
What is the point of having them?
Call it by its name!
function makeAnimals(animal1, animal2,
animal1name, animal2name) {
//code here
}
makeAnimals("dog", "cat", "Leonard", "Sammy");
Why do pass in values to the parameters, instead of just typing them out again?
function makeAnimals(animal1, animal2,
animal1name, animal2name) {
console.log("I have a " + animal1 + "named " + animal1name);
console.log("I have a " + animal2 + "named " + animal2name);
}
makeAnimals("dog", "cat", "Leonard", "Sammy");
makeAnimals(dog, cat, Leonard, Sammy);
Why doesn't this work?
Write a function, called tempConvert, that takes in two parameters: a numerical temperature, and a system (Fahrenheit or Celsius). The function converts temperatures in one system to the temperature in the other system.
ex. tempConvert("C", 30) will print out "86F"
tempConvert("F", 100) will print out "37.78C"
Look up the temperature conversion formulas online!