-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework3_task2.js
More file actions
52 lines (45 loc) · 1 KB
/
Copy pathhomework3_task2.js
File metadata and controls
52 lines (45 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Homework 3 - JavaScript Fundamentals
COMP420 - Spring 2017
Task 2 - solutions
J. Wangsadinata
*/
/* 1 */
for (var i = 1; i <= 10; i++) {
console.log(i);
}
/* 2 */
console.log(typeof("javascript"));
console.log(typeof(3));
console.log(typeof(4 + 5.5));
console.log(typeof(NaN));
console.log(typeof(true));
console.log(typeof([]));
console.log(typeof({}));
console.log(typeof(null));
console.log(typeof(undefined));
/* 3 */
// Basic factorial function
function factorial(n) {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Recursive function
function factorial2(n) {
if (n == 1) {
return 1;
}
else {
return n * factorial2(n - 1);
}
}
// One-liner
function f(n){return((n>1)?n*f(n-1):n)}
/* 4 */
function isPalindrome(s) {
s = s.replace(/\W/g, '').toLowerCase(); // convert things to lowercase and remove whitespaces
return (s == s.split('').reverse().join('')); // construct the reverse of the string and check whether it is equal to the string
}