Skip to content

Commit 482fa09

Browse files
committed
Solve the homework of the third week of javascript
Solve the homework of the third week of javascript
1 parent c248be6 commit 482fa09

9 files changed

Lines changed: 288 additions & 17 deletions

File tree

Week3/homework/1-step3.js

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

3-
function foo(func) {
4-
// What to do here?
5-
}
3+
function doHomework ( subject , callback ) {
4+
5+
console.log ( `Starting my ${ subject } homework.` ) ;
66

7-
function bar() {
8-
console.log('Hello, I am bar!');
7+
setTimeout( function ( ) { callback ( ) ; } , 5000 ) ;
98
}
109

11-
foo(bar);
10+
function finishedHomework(){
11+
12+
console.log ( 'Finished my homework' ) ;
13+
}
14+
doHomework ( 'math' , finishedHomework ) ;

Week3/homework/2-step3.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,54 @@
33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
44
const values = [];
55
// Add your code here
6+
7+
for(var i = startIndex; i <= stopIndex; i++){
8+
values.push(i);
69
}
710

8-
threeFive(10, 15, sayThree, sayFive);
11+
console.log( `\nThis is the new array : ${values}` );
12+
13+
const newValues = [];
14+
15+
for (var i = 0 ; i < values.length ; i++){
16+
17+
if ( values[ i ] % 3 === 0 && values[ i ] % 5 === 0 ){
18+
19+
threeCallback ( values[ i ] );
20+
21+
fiveCallback ( values[ i ] ) ;
22+
23+
newValues.push( values[ i ] );
24+
}
25+
26+
else if ( values[ i ] % 3 === 0 ) {
27+
28+
threeCallback ( values[ i ] ) ;
29+
30+
newValues.push( values[ i ] );
31+
32+
}
33+
34+
else if ( values[ i ] % 5 === 0 ) {
35+
36+
fiveCallback ( values[ i ] ) ;
37+
38+
newValues.push( values[ i ] );
39+
}
40+
}
41+
42+
43+
return newValues;
44+
}
45+
46+
47+
function sayThree( num ){
48+
console.log (`\nThe 'sayThree' function get called || ${ num } is divided by 3`);
49+
};
50+
51+
function sayFive( num ){
52+
console.log (`\nThe 'sayFive' function get called || ${ num } is divided by 5`);
53+
};
54+
55+
56+
console.log( threeFive(10, 15, sayThree, sayFive) );

Week3/homework/3-step3.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,48 @@
33
// use a 'for' loop
44
function repeatStringNumTimesWithFor(str, num) {
55
// add your code here
6-
return str;
6+
let result = '';
7+
if ( num === 0 || num < 0){
8+
return result;
9+
}
10+
for(var i = 1 ; i <= num ; i++){
11+
result += str;
12+
}
13+
return result;
714
}
815

916
console.log('for', repeatStringNumTimesWithFor('abc', 3));
1017

1118
// use a 'while' loop
1219
function repeatStringNumTimesWithWhile(str, num) {
1320
// add your code here
14-
return str;
21+
let result = '';
22+
if ( num === 0 || num < 0){
23+
return result;
24+
}
25+
var i = 1 ;
26+
while ( i <= num){
27+
result += str;
28+
i++;
29+
}
30+
return result;
1531
}
1632

1733
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
1834

1935
// use a 'do...while' loop
2036
function repeatStringNumTimesWithDoWhile(str, num) {
2137
// add your code here
22-
return str;
38+
let result = '';
39+
if ( num === 0 || num < 0){
40+
return result;
41+
}
42+
var i = 1 ;
43+
do {
44+
result += str;
45+
i++;
46+
} while ( i <= num)
47+
return result;
2348
}
2449

2550
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));

Week3/homework/4-step3.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
'use strict';
22
// paste your freeCodeCamp solutions in here
3+
4+
function Dog( name , color , numLegs ) {
5+
6+
this.name = "Albert";
7+
8+
this.color = "blue";
9+
10+
this.numLegs = 4;
11+
12+
}
13+
14+
15+
var newDog = new Dog();
16+
17+
console.log(newDog);

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+
4+
function multiplyAll(arr) {
5+
var product = 1;
6+
// Only change code below this line
7+
for (var i=0; i < arr.length; i++) {
8+
for (var j=0; j < arr[i].length; j++) {
9+
product *= arr[i][j];
10+
}
11+
}
12+
// Only change code above this line
13+
return product;
14+
}
15+
16+
17+
// Modify values below to test your code
18+
19+
console.log(multiplyAll([[1,2],[3,4],[5,6,7]]));

Week3/homework/6-step3.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
11
'use strict';
22

3-
const arr2d = [[1, 2], [3, 4], [5, 6]];
3+
let newArray = [ [ [1 , 4 ] , [ 5 , 6 , 7] ] , [ [ 8 , 9 , 10] , [ 2 , 4 ] ] , [ [ 3 , 4] , [ 5 , 6] , [ 8 ] ] ]; // an array with 3 dimensions
4+
5+
6+
7+
8+
function multiplyAll(arr) { // This function if we have an array with 3 dimensions
9+
10+
var result = 1;
11+
12+
for (var i = 0 ; i < arr.length ; i++ ) {
13+
14+
for ( var j = 0 ; j < arr[ i ].length ; j++ ) {
15+
16+
for (var r = 0 ; r < arr[i][j].length ; r++ ) {
17+
18+
result *= arr[i][j][r];
19+
20+
console . log ( arr[i][j][r] );
21+
}
22+
}
23+
}
24+
console . log ( result );
25+
}
26+
27+
multiplyAll ( newArray );
28+
29+
30+
31+
32+
33+
34+
35+
const arr2d = [[1, 2], [3, 4], [5, 6]]; // Nested arrays
436
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];
537

638
// add your solution here, or add a comment on how you would tackle this problem
39+
40+
41+
function nestArray(arr ) { //This is a function for using If we didn't know how deep the array was nested
42+
43+
var newArray = []; // Place the nested array elements in a single-dimension array
44+
45+
var multiplies = 1 ; // it multiplies the product variable by each number in the sub-arrays of arr
46+
47+
function arraysFunction ( arr ) { // This function is called an unspecified number of times depending on which array we have
48+
49+
if ( Array.isArray(arr) ) { // If the 'arr' is a array, go deep into 'arr'
50+
for ( var i in arr ) {
51+
arraysFunction ( arr[i] );
52+
}
53+
54+
}
55+
56+
else { // If the 'arr' is not a array, place it in the newArray
57+
newArray.push( arr );
58+
multiplies *= arr;
59+
}
60+
61+
}
62+
63+
64+
65+
if ( Array.isArray(arr) ) {
66+
arraysFunction (arr);
67+
}
68+
69+
else {
70+
newArray.push( arr );
71+
multiplies *= arr;
72+
}
73+
74+
75+
console.log('The result of multiplies the variables by each number of arr : ' + multiplies );
76+
console.log('Print all elements of The nested array "arr" : ' + newArray );
77+
return newArray;
78+
}
79+
80+
81+
var a1 = nestArray( arr2d );
82+
83+
var a2 = nestArray( arr3d );
84+
85+

Week3/homework/7-step3.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ f1(x);
1010

1111
console.log(x);
1212

13+
/*
14+
Here I tell JavaScript that I want the same variable value (x) and increase it by one value and return it.
15+
'val' here is a local variable. It has the same x value but has nothing to do with it.
16+
I did not tell JavaScript that I wanted to change the value of X .
17+
If I want to do this I must first change the X to let or var because it is const and the code must be like :
18+
19+
20+
let x = 9;
21+
function f1() {
22+
x ++ ;
23+
}
24+
f1();
25+
26+
or
27+
28+
let x = 9;
29+
function f1() {
30+
x = x + 1;
31+
return x;
32+
}
33+
34+
f1(x);
35+
*/
36+
37+
38+
39+
40+
1341

1442
const y = { x: 9 };
1543
function f2(val) {
@@ -23,3 +51,7 @@ console.log(y);
2351

2452
// Add your explanation as a comment here
2553

54+
/*
55+
Here it is different. We tell JavaScript to change the 'x' value in the object, where each of 'y' and 'val' refers to the same object.
56+
If we change the value of val.x or y.x , the original value in y will be changing
57+
*/

Week3/homework/step4-bonus.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
'use strict';
22

3-
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
3+
// Add your code here
4+
// numberValues stringValues
5+
const stringValues = ['a', 'b', 'a', 'c','a', 'd', 'a', 'e','e','e','e','e','e','e','e','e', 'f', 'c' , 'f' , 'f' , 'f' ];
46

5-
// Add your function here. Try and come up with a good name for this function
7+
const numberValues = [1 , 2, 3, 4, 1, 6, 5 ,4,4,4,4,4,2,2,2,2,2,2,2,2,2,2, 0 , 6 ,6 , 6 , 6 , 6];
68

7-
// Replace `yourFunction` with the name of the function you just created
8-
const uniqueValues = yourFunction(values);
9+
function sortUniquerNumberArryAndStringArry( arr ) {
910

10-
console.log(uniqueValues);
11+
if ( typeof arr[0] == "string" ) {
12+
arr.sort();
13+
}
14+
15+
else if ( typeof arr[0] == "number" ) {
16+
arr.sort(function(a, b){return a - b});
17+
}
18+
19+
else {
20+
return console.error('The array is not a "string" or a "number" ');
21+
}
22+
23+
24+
25+
for ( var i = 0 ; i < arr.length -1 ; i ++) {
26+
27+
if ( typeof arr[ i ] === typeof arr[ i + 1] ){
28+
29+
if ( arr[ i ] === arr[ i + 1] ){
30+
31+
arr.splice(i, 1);
32+
33+
sortUniquerNumberArryAndStringArry( arr );
34+
35+
}
36+
}
37+
else{
38+
return ; // The array contains different types of values so it will return undefined
39+
}
40+
41+
}
42+
return arr;
43+
}
44+
45+
46+
const uniqueStringValues = sortUniquerNumberArryAndStringArry( stringValues );
47+
console.log(uniqueStringValues);
48+
49+
50+
const uniqueNumberValues = sortUniquerNumberArryAndStringArry( numberValues );
51+
console.log(uniqueNumberValues);

Week3/homework/step4.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22

33
// Add your code here
44

5-
const addSix = createBase(6);
5+
const createBase = function ( num ) {
6+
return function constructing (value) {
7+
return num + value;
8+
};
9+
};
10+
11+
let addSix = createBase(6);
612

713
addSix(10); // returns 16
814
addSix(21); // returns 27
15+
16+
console.log(addSix(10));
17+
console.log(addSix(21));
18+
19+

0 commit comments

Comments
 (0)