Exercise: Function, strings, numbers, if/else, try/catch/finally, typeof, undefined, NaN
Notice the single quote in
... and I'm ...
console.l..........my name is Chris and I'm 29 ye..........
whoAmI
and call that functionfunction..........
console.l..........my name is Chris and I'm 29 ye..........
}
whoA.....
name
and age
funct..........(name, age){
console.l..........my name is Chris and I'm 29 ye..........
}
whoAmI(..........);
yearOfBirth
and calculate the year based on age (ex 2016 - age).
Then add another console.log
statement that outputs I was born in [insert yearOfBirth]
.funct..........(name, age){
var yearOf..........16 - age;
console.l..........my name is Chris and I'm 29 ye..........
console.log("I was born in " + yearOf.......
}
whoAmI(..........);
Do you get an error if the variable and function are both name
yearOfBirth
?
Uncaught TypeError: yearOfBirth is not a function
at whoAmI (<anonymous>:6:21)
at <anonymous>:10:1
function yearOfBirth(age){
..........
}
function whoAmI(name, age){
var yob = yearOfBirth(age);
..........
..........
}
whoAmI(..........);
try/catch
Note: a negative age doesn't cause a runtime error but it doesn't make sense.
function yearOfBirth(age){
..........
throw new Error("Age can not be negative");
..
return 2016 - age;
}
.
.
.
.
.
.
whoAmI("Chris", -5);
name
and age
have been entered. Create a conditional which outputs console.error("Arguments not valid")
.Hint: How do you check if a parameter or variable has not been "defined"?
"29"
as the age? What about "twenty nine"
? Implement a check to ensure the name is a string
and the age is a number
.Hint:
typeof age
Investigate: What isNaN
?