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

Skip to content

Commit 393d572

Browse files
committed
Javascript2/Week3 homework : done
1 parent 5362b48 commit 393d572

File tree

9 files changed

+185
-32
lines changed

9 files changed

+185
-32
lines changed

Week3/homework/1-step3.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
function foo(func) {
44
// What to do here?
5+
return func();
56
}
67

78
function bar() {

Week3/homework/2-step3.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
1-
'use strict';
1+
"use strict";
22

33
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
4+
5+
// make array:
6+
47
const values = [];
5-
// Add your code here
8+
9+
// start at beginning of array and check if you should call threeCallback or fiveCallback or go on to next:
10+
11+
for (let i = startIndex; i <= stopIndex; i++) {
12+
values.push(i);
13+
14+
// if the number can be divided to both 3 and 5:
15+
16+
if ((i % 3 == 0) && (i % 5 == 0)) {
17+
console.log(i + " can be divided to both: ");
18+
threeCallback("3");
19+
fiveCallback("5");
20+
}
21+
22+
// if the number can be divided to 3:
23+
24+
else if (i % 3 == 0) {
25+
console.log(i + " can be divided to: ");
26+
threeCallback("3");
27+
}
28+
29+
// if the number can be divided to 5:
30+
31+
else if (i % 5 == 0) {
32+
console.log(i + " can be divided to: ");
33+
fiveCallback("5");
34+
};
35+
36+
}
37+
38+
}
39+
40+
//Passing the callback functions as parameters to the new functions:
41+
42+
function sayThree(threeCallback) {
43+
console.log(threeCallback);
44+
}
45+
46+
function sayFive(fiveCallback) {
47+
console.log(fiveCallback);
648
}
749

8-
threeFive(10, 15, sayThree, sayFive);
50+
threeFive(10, 15, sayThree, sayFive);

Week3/homework/3-step3.js

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1-
'use strict';
1+
"use strict";
22

3-
// use a 'for' loop
4-
function repeatStringNumTimesWithFor(str, num) {
5-
// add your code here
6-
return str;
3+
//3.1 - Solution with a "for loop":
4+
5+
function forLoopSolution(str, num) {
6+
7+
let emptyString = " ";
8+
let filledString = str;
9+
10+
if (num < 0) {
11+
return emptyString;
12+
}
13+
14+
else if (num > 0) {
15+
for (let i = 1; i < num; i++) {
16+
filledString += str
17+
}
18+
}
19+
return filledString;
720
}
821

9-
console.log('for', repeatStringNumTimesWithFor('abc', 3));
22+
console.log("Using for loop: \n" + forLoopSolution("abc \n", 4))
23+
24+
//3.2 - Solution with a "while loop":
25+
26+
function whileLoopSolution(str, num) {
1027

11-
// use a 'while' loop
12-
function repeatStringNumTimesWithWhile(str, num) {
13-
// add your code here
14-
return str;
28+
let strString = "";
29+
30+
while (num > 0) {
31+
strString += str;
32+
num--;
33+
}
34+
35+
return strString;
1536
}
1637

17-
console.log('while', repeatStringNumTimesWithWhile('abc', 3));
38+
console.log("Using while loop: \n" + whileLoopSolution("abc \n", 5))
39+
40+
//3.3 - Solution with a "do...while loop":
41+
42+
function doWhileLoopSolution(str, num) {
43+
44+
let strString = "";
45+
46+
do {
47+
strString += str;
48+
num--;
49+
}
50+
while (num > 0);
1851

19-
// use a 'do...while' loop
20-
function repeatStringNumTimesWithDoWhile(str, num) {
21-
// add your code here
22-
return str;
52+
return strString;
2353
}
2454

25-
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));
55+
console.log("Using do...while loop: \n" + doWhileLoopSolution("abc \n", 6))

Week3/homework/4-step3.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
'use strict';
2-
// paste your freeCodeCamp solutions in here
1+
"use strict";
2+
3+
//Defining a Constructor Function:
4+
5+
//Create a constructor, Dog, with properties name, color, and numLegs that are set to a string, a string, and a number, respectively:
6+
7+
function Dog() {
8+
this.name = "Marissa";
9+
this.color = "Golden";
10+
this.numLegs = 4;
11+
}
12+
13+
let hound = new Dog();
14+
console.log(hound);

Week3/homework/5-step3.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
'use strict';
2-
// paste your freeCodeCamp solutions in here
1+
"use strict";
2+
3+
// Modify function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr
4+
5+
function multiplyAll(arr) {
6+
var product = 1;
7+
8+
//This outputs each sub-element in arr one at a time.
9+
//Note that for the inner loop, we are checking the .length of arr[i],
10+
//since arr[i] is itself an array.
11+
12+
for (var i = 0; i < arr.length; i++) {
13+
for (var j = 0; j < arr[i].length; j++) {
14+
product = product * arr[i][j];
15+
}
16+
17+
}
18+
return product;
19+
}
20+
21+
console.log(multiplyAll([[1, 2], [3, 4], [5, 6, 7]]));

Week3/homework/6-step3.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ 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+
8+
for (let i = 0; i < arr3d.length; i++) {
9+
for (let j = 0; j < arr3d[i].length; j++) {
10+
console.log(arr3d[i][j]);
11+
}
12+
}
13+
14+
//Comment on K Dimensional Arrays:
15+
//Seems like we will need one loop for each array dimension.

Week3/homework/7-step3.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ f1(x);
1010

1111
console.log(x);
1212

13-
1413
const y = { x: 9 };
1514
function f2(val) {
1615
val.x = val.x + 1;
@@ -21,5 +20,15 @@ f2(y);
2120

2221
console.log(y);
2322

24-
// Add your explanation as a comment here
23+
/*
24+
25+
01 - The variable here "Passed by Value", because it is a "number" data type.
26+
Primitive data types like number, string and boolean..etc. are always pass by value.
27+
That's why the value "x" didn't got effected by what is happening inside the function.
28+
29+
02 - The variable here "Passed by Reference", because it is a "object" data type.
30+
Non-primitive data types like object, array and date are always pass by reference.
31+
That's why the value "y" got effected by what is happening inside the function.
32+
33+
*/
2534

Week3/homework/step4-bonus.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
'use strict';
22

33
const values = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
4+
const numberArray = [1, 1, 2, 5, 5, 5, 6, 3, 9];
5+
const mixedArray = [1, true, "coffee", { 2: 1, 4: 2 }, false, true, "water"];
46

5-
// Add your function here. Try and come up with a good name for this function
7+
function smartSorting(anyArray) {
68

7-
// Replace `yourFunction` with the name of the function you just created
8-
const uniqueValues = yourFunction(values);
9+
anyArray.sort();
10+
let sortedArray = [];
911

10-
console.log(uniqueValues);
12+
for (let i = anyArray.length - 1; i >= 0; i--) {
13+
if (anyArray[i] != anyArray[i - 1]) {
14+
sortedArray.push(anyArray[i]);
15+
};
16+
if (typeof anyArray[i] != "string" && typeof anyArray[i] != "number") {
17+
return "Sorry, I have to stop. I only want numbers or strings. Bye!";
18+
}
19+
20+
}
21+
return sortedArray.sort();
22+
}
23+
24+
const uniqueValues = smartSorting(values);
25+
const uniqueNumbers = smartSorting(numberArray);
26+
const mixedTest = smartSorting(mixedArray);
27+
28+
29+
console.log("Array from assignment: " + values);
30+
console.log("Sorted version: " + uniqueValues);
31+
32+
console.log("Array with numbers: " + numberArray);
33+
console.log("Sorted version: " + uniqueNumbers);
34+
35+
console.log("Mixed array with different data types: " + mixedArray);
36+
console.log("It doesn't accept any non-number & string values:\n" + mixedTest);

Week3/homework/step4.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
'use strict';
22

3-
// Add your code here
3+
function createBase(number1) {
4+
return function (number2) {
5+
return number1 + number2;
6+
};
7+
}
48

59
const addSix = createBase(6);
610

7-
addSix(10); // returns 16
8-
addSix(21); // returns 27
11+
console.log(addSix(10)); // returns 16
12+
console.log(addSix(21)); // returns 27
13+
console.log(addSix(30)); // returns 36

0 commit comments

Comments
 (0)