-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-count.js
More file actions
55 lines (40 loc) · 1.17 KB
/
1-count.js
File metadata and controls
55 lines (40 loc) · 1.17 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
//let count = 0;
//count = count + 1;
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// Line 3 returns the incremented value of count. The operator = assigns count +1 to the varibale count in the
// left.
// Described what line three is doing.
// Example from codewar (from programming workshop)
function drawStairs(n) {
let result = "";
if (n < 1) return result; // nothing to do
let iCount = 0;
while (iCount < n) {
// add spaces before the I (for all lines after the first)
let spaceCount = 0;
while (spaceCount < iCount) {
result = result.concat(" ");
spaceCount = spaceCount + 1;
}
// add the "I"
result = result.concat("I");
// add a newline after each line except the last one
if (iCount < n - 1) {
result = result.concat("\n");
}
// update the count
iCount = iCount + 1;
}
return result;
}
console.log(drawStairs(10));
/// Another example
function drawStairs(n) {
let result = [];
for (let i = 0; i < n; i++) {
result[i] = `${' '.repeat(i)}I`;
}
return result.join('\n');
}
console.log(drawStairs(5));