Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11a428f

Browse files
committed
Homework-Week3
1 parent 87a90b1 commit 11a428f

File tree

9 files changed

+131
-12
lines changed

9 files changed

+131
-12
lines changed

Week3/homework/1-step3.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
function foo(func) {
44
// What to do here?
5+
//As the argument is a function so I will call the argument itself.
6+
return func();
57
}
68

79
function bar() {

Week3/homework/2-step3.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
44
const values = [];
55
// Add your code here
6+
for (let i = startIndex; i <= stopIndex; i++) {
7+
8+
values.push(i);
9+
10+
if (i % 3 === 0 && i % 5 === 0) { // here the order of checking is important.
11+
sayThree();
12+
sayFive();
13+
} else if (i % 3 === 0) {
14+
sayThree();
15+
} else if (i % 5 === 0) {
16+
sayFive();
17+
}
18+
19+
}
20+
console.log(values); // just to print out the array when the programme run
21+
return values;
22+
23+
}
24+
25+
function sayThree() {
26+
console.log("I am Three");
27+
}
28+
29+
function sayFive() {
30+
console.log("I am Five");
631
}
732

833
threeFive(10, 15, sayThree, sayFive);

Week3/homework/3-step3.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
'use strict';
22

33
// use a 'for' loop
4+
45
function repeatStringNumTimesWithFor(str, num) {
5-
// add your code here
6-
return str;
6+
let newStr = '';
7+
for (let i = 0; i < num; i++) {
8+
9+
newStr += str;
10+
}
11+
return newStr;
712
}
813

14+
915
console.log('for', repeatStringNumTimesWithFor('abc', 3));
1016

11-
// use a 'while' loop
17+
// // use a 'while' loop
18+
1219
function repeatStringNumTimesWithWhile(str, num) {
13-
// add your code here
14-
return str;
20+
// // add your code here
21+
let newString = "";
22+
while (num > 0) {
23+
newString += str;
24+
num--;
25+
}
26+
return newString;
1527
}
1628

1729
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
1830

1931
// use a 'do...while' loop
32+
2033
function repeatStringNumTimesWithDoWhile(str, num) {
21-
// add your code here
22-
return str;
23-
}
34+
// // add your code here
35+
let string = "";
36+
if (num <= 0) return string;
2437

25-
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));
38+
do { string += str; num--; }
39+
while (num > 0);
40+
41+
return string;
42+
}
43+
console.log('do..while', repeatStringNumTimesWithDoWhile('abc', 3));

Week3/homework/4-step3.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
'use strict';
2+
23
// paste your freeCodeCamp solutions in here
4+
5+
const Car = function () {
6+
this.wheels = 4;
7+
this.engines = 1;
8+
this.seats = 5;
9+
};
10+
11+
// Only change code below this line.
12+
13+
const MotorBike = function () {
14+
this.wheels = 2;
15+
this.engines = 1;
16+
this.seats = 1;
17+
18+
};
19+
20+
const forSaleMotorBike = new MotorBike();
21+
22+
console.log(forSaleMotorBike);

Week3/homework/5-step3.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
'use strict';
22
// paste your freeCodeCamp solutions in here
3+
function multiplyAll(arr) {
4+
let product = 1;
5+
6+
// Only change code below this line
7+
8+
for (let i = 0; i < arr.length; i++) {
9+
for (let k = 0; k < arr[i].length; k++) {
10+
product *= arr[i][k];
11+
}
12+
}
13+
// Only change code above this line
14+
return product;
15+
}
16+
17+
// Modify values below to test your code
18+
const result = multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
19+
console.log(result);

Week3/homework/6-step3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@ const arr2d = [[1, 2], [3, 4], [5, 6]];
44
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
55

66
// add your solution here, or add a comment on how you would tackle this problem
7+
// //We use recursive function so it iterates through the array and check every element if
8+
//it is array so it calls back the same function but with the array element as an argument(as it is also an array)
9+
// and so on until the number of nested arrays is finished it return the element's value of the last array.
10+
11+
function printArrayItems(arr) {
12+
for (let i = 0; i < arr.length; i++)
13+
Array.isArray(arr[i]) ? printArrayItems(arr[i]) : console.log(arr[i]);
14+
}
15+
16+
printArrayItems(arr3d);

Week3/homework/7-step3.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ console.log(y);
2323

2424
// Add your explanation as a comment here
2525

26+
/*This is because const x is a primitive,it is passed by value, that means when we called our function
27+
and passed x as argument our programme creates a copy of x and used it when the function executed,
28+
but it doesn't change the original value of x, so x value still the original value which is 9.
29+
while const y is an object,it is passed by reference, that means when we called our function and passed y as
30+
argument.Our programme doesn't make a copy of y value but it takes the original value and used it,
31+
so y value is the new value after the function executed which is 10. */

Week3/homework/step4-bonus.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
44

55
// Add your function here. Try and come up with a good name for this function
66

7+
function unique(arr) {
8+
9+
const arr2 = [];
10+
11+
for (let i = 0; i < arr.length; i++) {
12+
if (arr2.indexOf(arr[i]) === -1) [
13+
arr2.push(arr[i])
14+
]
15+
}
16+
return arr2;
17+
}
718
// Replace `yourFunction` with the name of the function you just created
8-
const uniqueValues = yourFunction(values);
19+
const uniqueValues = unique(values);
920

1021
console.log(uniqueValues);

Week3/homework/step4.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
// Add your code here
44

5+
function createBase(value) {
6+
7+
const addNum = function (num) {
8+
return value + num;
9+
};
10+
return addNum;
11+
}
12+
513
const addSix = createBase(6);
614

7-
addSix(10); // returns 16
8-
addSix(21); // returns 27
15+
const result1 = addSix(10);
16+
console.log(result1); // returns 16
17+
const result2 = addSix(21);
18+
console.log(result2);// returns 27

0 commit comments

Comments
 (0)