-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain.js
More file actions
122 lines (93 loc) · 2.23 KB
/
main.js
File metadata and controls
122 lines (93 loc) · 2.23 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// For answers to be checked the other problems will need to be commented out due to variables aleady be declared
// Problem 1
for (i = 1; i <= 10; i++){
console.log(i)
}
// Problem 2
for (i = 1; i <= 10; i++) {
console.log(i * i)
}
// Problem 3
let n = prompt('Please enter positive number');
for (i = 1; i < n; i++) {
if (i % 2 == 0) {
console.log(i)
}
}
// Problem 4
let n = prompt('Please enter positive number')
n = Number(n)
let m = prompt('Please enter positive number')
m = Number(m)
let sum = 0
while (n < m) {
sum += n;
n++;
}
alert(sum)
// started alternative method if n was larger than m
// if (n < m) {
// sum += n;
// n++;
// } else if (n > m) {
// sum += n - 1;
// n--;
// }
// Problem 5
do {
userInput = prompt('Are we there yet?')
} while (userInput !== 'Yes')
alert('Good!')
// Problem 6
let rows = 5;
for (let currentRow = 1; currentRow <= rows; currentRow++) {
let stars = ""
for (let j = 1; j <= currentRow; j++) {
stars += "*";
}
console.log(stars);
}
// Solved without nested loop
// let output = ''
// for (let i = 1; i < 6; i++) {
// output += '*';
// console.log(output);
// }
// Problem 7
rows = 4
columns = 4
for (let currentRow = 1; currentRow <= rows; currentRow++) {
let num = ""
for (let currentCol = 1; currentCol <= columns; currentCol++){
let product = ""
product += currentCol * currentRow
if (currentCol === 1){
num += `|`
}
if (product < 10 && currentCol > 1) {
num += ` ${product} |`
} else {
num += ` ${product} |`
}
}
console.log(num)
}
// Problem 8
function timesTable(rows, columns) {
for (let currentRow = 1; currentRow <= rows; currentRow++) {
let num = ""
for (let currentCol = 1; currentCol <= columns; currentCol++){
let product = ""
product += currentCol * currentRow
if (currentCol === 1){
num += `|`
}
if (product < 10 && currentCol > 1) {
num += ` ${product} |`
} else {
num += ` ${product} |`
}
}
console.log(num)
}
}