-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07Mar.js
72 lines (61 loc) · 1.57 KB
/
07Mar.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//get the id of the element to output the result
var resultDiv = document.getElementById("result");
//1. Hello World
function hello() {
resultDiv.innerHTML = "Hello World";
}
//2. Get Name and Greet
function nameGreet(name1) {
resultDiv.innerHTML = "Hello " + name1;
}
//3. Get Name and Greet only Alice or Bob
function nameGreet1(name2) {
if (name2 == "Alice" || name2 == "Bob") {
resultDiv.innerHTML = "Hello " + name2;
} else {
resultDiv.innerHTML = "Hello";
}
}
//4. Sum N numbers
function sumN(n2) {
n2 = parseInt(n2);
resultDiv.innerHTML = "The sum of numbers upto " + n2 + " is " + (n2 / 2) * (n2 + 1);
}
//5. Sum of N numbers multiple of 3 & 5 only not both
function sumSpecial(n1) {
var sum = 0;
for (i = 0; i < n1; i++) {
if ((i % 3 === 0 && i % 5 !== 0) || (i % 3 !== 0 && i % 5 === 0)) {
sum += i;
}
}
resultDiv.innerHTML = "The result is " + sum;
}
//6. Multiplication Table
function multiply(which, m) {
var upto = m || 12;
var table = "";
for (i = 1; i <= upto; i++) {
table += which + " x " + i + " = " + which * i + "<br/>";
}
resultDiv.innerHTML = table;
}
//7. Leap year 100 years
function leap(yr) {
yr = parseInt(yr);
yr = (yr % 4 === 0 ? yr : yr + (4 - (yr % 4)));
resultDiv.innerHTML = printLeap(yr, 100);
function printLeap(start, upto) {
var years = "";
var year;
for (i = 0; i <= upto; i += 4) {
year = start + i;
if (year % 100 !== 0) {
years += year + "<br/>";
} else if (year % 400 === 0) {
years += year + "<br/>";
}
}
return years;
}
}