Skip to content

Commit 0cdf676

Browse files
committed
js2_week3_homeworks updated execpt pomodoroclock homework from 2 weeks ago
1 parent e7a04f9 commit 0cdf676

5 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Declare a function called createBase. It should return a closure, that adds a number to the base number argument.
4+
// Call the function three times. The return values should be:
5+
// 15
6+
// 24
7+
// 36
8+
// It should look a little like this:
9+
// function createBase() {
10+
// // Put here your logic...
11+
// }
12+
// const addSix = createBase(6);
13+
// // Put here your function calls...
14+
// addSix();
15+
16+
function createBase(x) {
17+
return function addSix(y) {
18+
return x + y;
19+
};
20+
}
21+
22+
const addNumber = createBase(-1);
23+
24+
console.log(addNumber(2));
25+
console.log(addNumber(3.3));
26+
console.log(addNumber(44));
27+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
// Write a function called removeDuplicates, that takes in an array as an argument:
4+
// const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b'];
5+
// The function should remove duplicate letters. So the result should be:
6+
// letters === ['a', 'b', 'c', 'd', 'e', 'f'];
7+
8+
9+
const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b'];
10+
const removeDuplicates = letters => {
11+
const fixLetters = {};
12+
return letters.filter(figure => (fixLetters.hasOwnProperty(figure) ? false : (fixLetters[figure] = true)));
13+
};
14+
console.log(removeDuplicates(letters));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
'use strict';
3+
4+
// In this exercise you'll be presented with a code snippet. Your task is to guess the
5+
// output and write out your reasoning in 50 words or less.
6+
7+
// // Snippet
8+
// let a = 10;
9+
// const x = (function() {
10+
// a = 12;
11+
// return function() {
12+
// alert(a);
13+
// };
14+
// })();
15+
// x();
16+
17+
//////////////////////////////
18+
19+
let a = 10;
20+
const x = (function() {
21+
a = 12;
22+
return function() {
23+
alert(a);
24+
};
25+
})();
26+
27+
x();
28+
29+
30+
// First line declares "a" with the value of 10.
31+
// Second line creates the x function. This function that executes as soon as it is defined.
32+
// Because It is the IIFE (Immediately Invoked Function Expression).
33+
// At the end of function there is parenthesis.It means that it is created immediately .
34+
// Then "a" becomes 12.
35+
// After that the closure function begins. It uses the inner values.
36+
// So it alerts as 12 in the browser.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
// In this exercise you'll be presented with another code snippet.
4+
// Guess the output and write out your reasoning in 50 words or less.
5+
6+
// Snippet
7+
const x = 9;
8+
function f1(val) {
9+
val = val + 1;
10+
return val;
11+
}
12+
f1(x);
13+
console.log(x);
14+
15+
const y = { x: 9 };
16+
function f2(val) {
17+
val.x = val.x + 1;
18+
return val;
19+
}
20+
f2(y);
21+
console.log(y);
22+
23+
24+
// When we use in the first function value 9, it makes x as 10, but this doesn't change or reassign the value of x as 10, x stays as 9.
25+
// But in the second case f2(y) function creates y={x:9} as an object.
26+
// there is a mutating situation here. It is being placed with reference so when we assign variable with a value it affects the place where it is in
27+
// The new value of x becomes 10 and because of this also it is again being mutated as 10 in (y) function
28+
// console log (y) becomes 10
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
// Don't you just love the thrill of the lottery? What if I told you we can
4+
// make our own lottery machine? Let's get started!
5+
// Write a function that takes 4 arguments.
6+
// A start value
7+
// An end value
8+
// A callback that executes if the number is divisible by 3
9+
// A callback that executes if the number is divisible by 5
10+
// The function should first generate an array containing values from start
11+
// value to end value (inclusive).
12+
// Then the function should take the newly created array and iterate over it,
13+
// and calling the first callback if the array value is divisible by 3.
14+
// The function should call the second callback if the array value is
15+
// divisible by 5.
16+
// Both functions should be called if the array value is divisible by both 3
17+
// and 5.
18+
// Here is a starter template:
19+
20+
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
21+
const numbers = [];
22+
// make array
23+
// start at beginning of array and check if you should call threeCallback
24+
// or fiveCallback or go on to next
25+
}
26+
27+
threeFive(10, 15, sayThree, sayFive);
28+
// Should create an array [10,11,12,13,14,15]
29+
// and call sayFive, sayThree, sayThree, sayFive
30+
31+
////////////////////////////////////
32+
33+
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
34+
const numbers = [];
35+
36+
for (let i = startIndex; i <= stopIndex; i++) {
37+
numbers.push(i);
38+
}
39+
40+
console.log(numbers);
41+
42+
for (const number of numbers) {
43+
threeCallback(number);
44+
fiveCallback(number);
45+
}
46+
}
47+
48+
const sayThree = number => {
49+
if (number % 3 === 0) {
50+
return console.log("The number" + number + "can be divisible with 3");
51+
}
52+
};
53+
54+
const sayFive = number => {
55+
if (number % 5 === 0) {
56+
return console.log(`The number ${number} can be divisible with 5`);
57+
}
58+
};
59+
60+
threeFive(10, 15, sayThree, sayFive);
61+
threeFive(17, 33, sayThree, sayFive);

0 commit comments

Comments
 (0)