-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelegantJavascript.js
155 lines (128 loc) · 3.37 KB
/
elegantJavascript.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// -------------- LOOP ---------------
/** Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).
*/
for (let n = 1; n <= 100; n++) {
let output = "";
if (n % 3 == 0) output += "Fizz";
if (n % 5 == 0) output += "Buzz";
console.log(output || n);
}
/** Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a "#" character. The characters should form a chessboard.
*/
let size = 8;
let board = "";
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if ((x + y) % 2 == 0) {
board += " ";
} else {
board += "#";
}
}
board += "\n";
}
console.log(board);
// --------------------- Functions ------------------
/**
// return minimum number
function min(num1, num2) {
return num1 <= num2 ? num1 : num2;
}
console.log(min(1, 10));
// recursion
function isEven(number) {
if ((number = 0)) {
return true;
} else if (number == 1) {
return false;
} else if (number < 0) {
return isEven(-number);
} else {
return isEven(number - 2);
}
}
console.log(isEven(2));
// growing functions: count Bs
function countChar(string, ch) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] == ch) {
count += 1;
}
}
return count;
}
function countBs(string) {
return countChar(string, "B");
}
// -----------Data Structures : Objects and Arrays --------
// SUM OF A RANGE
function range(start, end, step = start < end ? 1 : -1) {
let array = [];
if (step > 0) {
for (let i = start; i <= end; i += step) {
array.push(i);
}
} else {
for (let i = start; i >= end; i += step) {
array.push(i);
}
}
return array;
}
function sum(arr) {
let total = 0;
for (let i of arr) {
total += i;
}
return total;
}
console.log(range(1, 10));
console.log(range(5, 2, -1));
console.log(sum(range(1, 10)));
// REVERSE AN ARRAY
function reverseArray(arr) {
let newArray = [];
for (let element of arr) {
newArray.unshift(element);
}
return newArray;
}
// function reverseArray(arr) {
// let output = [];
// for (let i = arr.length - 1; i >= 0; i--) {
// output.push(arr[i]);
// }
// return output;
// }
function reverseArrayInPlace(arr) {
const middlePoint = Math.floor(arr.length / 2);
for (i = 0; i < middlePoint; i++) {
let temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
return arr;
}
console.log(reverseArray([1, 2, 3]));
console.log(reverseArrayInPlace(["a", "b", "c"]));
// A LIST ...continue...
*/
// --------- higher order functions ------------
// Falttening: reduce, concat
let arrays = [[1, 2, 3], [4, 5], [6]];
console.log(arrays.reduce((flat, current) => flat.concat(current), []));
// My own loop: higher order function loop
function loop(start, test, update, body) {
for (let value = start; test(value); value = update(value)) {
body(value);
}
}
loop(
3,
(n) => n > 0,
(n) => n - 1,
console.log
);
// Everything