##Problem Samples
####Logging hello world How would you output the string "Hello world!" to the javascript console.
Answer:
console.log("Hello World!");
####Factorial function Write a function to compute n factorial (n!) and then output the result of 23! to the console.
Answer:
function factorial(n) {
if (n===1) {return 1};
return n * factorial (n-1);
}
console.log(factorial(23));
####Declare local variables Although the following code executes without error, it is problematic. Why?
//function that swaps index i and j of an array
function swap (arr, i, j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Answer: The variable temp has global array Source: Effective Javascript, Item 9
####Using logical and to avoid type errors Explain the use of the logical and (&&) operator in the code below. Bonus: Is there anything strange about the way it computes the average score?
//function that takes an array of Students and returns their average score on a given quiz
function avgScore (students, quizNo) {
var sum = 0;
var quizGrade;
for (var i =0; i < students.length; i++) {
quizGrade = students[i].quizzes[quizNo] &&
students[i].quizzes[quizNo].grade;
if (typeof quizGrade === "number") {sum += quizGrade};
}
return sum / students.length;
}
Answer: The && is used to ensure that the student has taken the quiz. Without the && we would get a TypeError whenever the students quiz array is undefined for that quiz number. Bonus: Because we are dividing by students.length instead of counting the number of students who have a score for that quiz number, we are counting the students who missed a quiz as scores of 0. So if there are 3 students, and two of them aced the quiz (score of 100), the average will be 200 / 3 = 67, and not 200 / 2 = 100. Source:
####Using logical and to avoid type errors
(1) The code below sometimes produces a type error. Why, and how can you resolve it?
(2) Why is if (typeof quizGrade === "number") {...}
necessary?
//function that takes an array of Students and returns their average score on a given quiz
function avgScore (students, quizNo) {
var sum = 0;
var quizGrade;
for (var i =0; i < students.length; i++) {
quizGrade = students[i].quizzes[quizNo].grade;
if (typeof quizGrade === "number") {sum += quizGrade};
}
return sum / students.length;
}
Answer: If a student has missed a quiz then students[i].quizzes[quizNo] will be undefined, and trying to access its grade property will produce a type error. We can fix this by testing for undefined before trying to extract the grade property:
quizGrade = students[i].quizzes[quizNo] && students[i].quizzes[quizNo].grade;
####Avoiding implicit casting of undefined into NaN
Why is it necessary to test if (typeof quizGrade === "number") {...}
in the code below?
//function that takes an array of Students and returns their average score on a given quiz
function avgScore (students, quizNo) {
var sum = 0;
var quizGrade;
for (var i =0; i < students.length; i++) {
quizGrade = students[i].quizzes[quizNo] &&
students[i].quizzes[quizNo].grade;
if (typeof quizGrade === "number") {sum += quizGrade};
}
return sum / students.length;
}
Answer: Because sometimes quizGrade might be undefined, in which case it would implicitly cast into NaN, and the function would return NaN.