-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_excersizes.js
180 lines (114 loc) · 4 KB
/
js_excersizes.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// # Write a method that prints out the numbers 1 to 1000 that are divisible by 3.
// def print_numbers_divisible_by_three
// index = 1
// while index <= 1000
// if index % 3 == 0
// puts index
// end
// index += 1
// end
// end
// print_numbers_divisible_by_three
function printNumbersDivisibleByThree() {
var index = 1;
while (index <= 1000) {
if (index % 3 === 0) {
console.log(index);
}
index += 1;
}
}
printNumbersDivisibleByThree();
// # Write a method that accepts an array of strings and prints out every other string.
// def print_every_other_item(strings)
// index = 0
// strings.each do |string|
// if index % 2 == 0
// puts string
// end
// index += 1
// end
// end
// print_every_other_item(["a", "b", "c", "d", "e"])
function printEveryOtherItem(strings) {
var index = 0;
strings.forEach(function(string) {
if (index % 2 === 0) {
console.log(string);
}
index += 1;
});
}
printEveryOtherItem(["a", "b", "c", "d", "e"]);
// # Write a method that accepts an array of numbers and returns the sum.
// def compute_sum(numbers)
// sum = 0
// numbers.each do |number|
// sum += number
// end
// return sum
// end
// puts compute_sum([2, 4, 5])
function computeSum(numbers) {
var sum = 0;
numbers.forEach(function(number) {
sum += number;
});
return sum;
}
console.log(computeSum([2, 4, 5]));
// # Start with the hash: city_populations = {chi: 2700000}
// # Add populations to the city_populations hash for New York City (8.4 million) and San Francisco (800,000).
// # The result should be: {chi: 2700000, nyc: 8400000, sf: 800000}
// city_populations = {chi: 2700000}
// city_populations[:nyc] = 8400000
// city_populations[:sf] = 800000
// p city_populations
var cityPopulations = {chi: 2700000};
cityPopulations["nyc"] = 8400000;
cityPopulations.sf = 800000; // same as cityPopulations["sf"]
console.log(cityPopulations);
// # Write a method that prints out every number from 1 to 100.
function printOneThroughOneHundred() {
var index = 0;
while (index <= 100) {
console.log(index);
index ++;
}
}
printOneThroughOneHundred
// # Write a method that prints out every other number from 1 to 100. (That is, 1, 3, 5, 7 … 99).
function printEveryOtherNumber() {
var index = 0;
while (index <= 100) {
if (index % 2 !== 0) {
console.log(index);
}
index++;
}
}
printEveryOtherNumber
// # Write a method that accepts an array of numbers as a parameter, and counts how many 55’s there are in the array.
function printCountsNumbers(numbers) {
var count = 0;
numbers.forEach(function(number) {
if (number === 55) {
console.log(number);
count++;
}
});
console.log(count)
}
printCountsNumbers[55, 4, 67, 55, 34, 21, 55]
// # Write a method that accepts an array of strings and returns a new array that has the string “awesomesauce” inserted between every string.
// # For example, if the initial array is [“a”, “b”, “c”, “d”, “e”], then the returned array should be [“a”, “awesomesauce”, “b”, “awesomesauce”, “c”, “awesomesauce”, “d”, “awesomesauce”, “e”].
// # Start with the hash: item_amounts = {chair: 5, table: 2}
// # Someone just bought two chairs. Change the hash such that the chair amount is 3.
// # The final result should be: {chair: 3, table: 2}
// # Start with the hash: item_amounts = {chair: 5, table: 2}
// # You received 7 desks to sell. Change the hash to include desks.
// # The final result should be: {chair: 5, table: 2, desk: 7}
// # Write a method that accepts a number and returns its factorial.
// # For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
// # Write a method that accepts two arrays of numbers, and prints the sum of every combination of numbers from first and second array.
// # For example, if the method receives [1, 5, 10] and [100, 500, 1000], the method should print a list: 101, 501, 1001, 105, 505, 1005, 110, 510, 1010].