Skip to content

Commit 6fe588d

Browse files
committed
upload homework week3 javascript 02
1 parent 1d7ea1f commit 6fe588d

File tree

11 files changed

+173
-18
lines changed

11 files changed

+173
-18
lines changed

Week2/homework/maartjes_work.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ console.log("SEK " + kronor);
7070

7171
//if currency was in EURO:
7272
let euro = Number(12250).toLocaleString("en-EUR", {minimumFractionDigits: 2});
73-
console.log("" + euro);
73+
console.log("Maartjes should earn €" + euro);
7474

7575

Week2/homework/shop_list.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*Assignment: Your friend Gudrun owns a "livs" kiosk. She needs help avoiding food waste and financial loss.
2+
Write a Javascript function which, given a list of items sorts them by 1) best before date (ascending) and 2) price (descending).
3+
In other words, the item with the earliest best before date first, and in the case of same date, higher price first.
4+
If items have bot equal best before date and price,their order does not matter.
5+
The input list is given as a text string, in a JSON format demonstrated by this example:*/
6+
7+
8+
let livs_kiosk = '[ {"product": "Milk, 1L carton", "best_before": "2018-10-04", "price": 15.5 }, {"product": "Milk, 1L carton", "best_before": "2018-10-04", "price": 15.5},{ "product": "Eggs 12pcs","best_before": "2018-10-04","price": 35.0}, {"product": "Swedish meatballs", "best_before": "2018-10-25", "price": 22.0 }]'
9+
10+
/*The function should return a text string containing the sorted list, in the same format as the input.
11+
You can assume that the input is always valid and in the same format, and its length isn't more than a few hundreds items.
12+
Only "vanilla" Javascript is allowed, ie. no external libraries.*/
13+
14+
15+
let Obj = JSON.parse(livs_kiosk);
16+
/*console.log(Obj);
17+
const bestBefore = Obj.filter(Obj.best_before);
18+
console.log(bestBefore);*/
19+
20+
let sortedObj = Obj.sort(function(pr1, pr2) {
21+
let a = new Date (pr1.best_before)
22+
let b = new Date (pr1.best_before)
23+
if (a>b){return -1};
24+
if (a<b){return 1};
25+
let c = new Date (pr2.price)
26+
let d = new Date (pr2.price)
27+
if (c>d){return -1};
28+
if (c<d){return 1};
29+
});
30+
console.log(sortedObj);
31+
32+

Week3/homework/1-step3.js

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

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

78
function bar() {
89
console.log('Hello, I am bar!');
910
}
1011

1112
foo(bar);
13+

Week3/homework/2-step3.js

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

3+
34
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
45
const values = [];
5-
// Add your code here
6+
for (var i = startIndex; i <= stopIndex; i++){
7+
values.push(i)
8+
}
9+
console.log(values);
10+
11+
for (let j = 0; j < values.length; j++){
12+
if (j % 3 === 0) {
13+
threeCallback("3");
14+
}
15+
else if (j % 5 === 0) {
16+
fiveCallback("5");
17+
}
18+
else if ((j % 3=== 0) && (j % 5 === 0)) {
19+
fiveCallback("5") && threeCallback("3");
20+
}
21+
}
622
}
23+
function sayThree(threeCallback) {
24+
console.log(threeCallback);
25+
}
26+
27+
function sayFive(fiveCallback) {
28+
console.log(fiveCallback);
29+
}
730

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

Week3/homework/3-step3.js

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

33
// use a 'for' loop
4-
function repeatStringNumTimesWithFor(str, num) {
5-
// add your code here
6-
return str;
4+
function repeatStringNumTimes(str, num) {
5+
// repeat after me
6+
let repeatString = "";
7+
8+
for (; num > 0;) {
9+
repeatString += str;
10+
num--;
11+
}
12+
return repeatString;
713
}
814

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

1117
// use a 'while' loop
12-
function repeatStringNumTimesWithWhile(str, num) {
13-
// add your code here
14-
return str;
15-
}
18+
function repeatStringNumTimes(str, num) {
19+
// repeat after me
20+
let repeatString = "";
21+
22+
while (num > 0){
23+
repeatString += str;
24+
num--;
25+
}
26+
return repeatString;
27+
}
1628

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

1931
// use a 'do...while' loop
20-
function repeatStringNumTimesWithDoWhile(str, num) {
21-
// add your code here
22-
return str;
32+
function repeatStringNumTimes(str, num) {
33+
// repeat after me
34+
let repeatString = "";
35+
do {
36+
repeatString += str;
37+
num--;
38+
} while (num > 0);
39+
if (num < 0){
40+
return "";
41+
}
42+
return repeatString;
2343
}
24-
2544
console.log('while', repeatStringNumTimesWithDoWhile('abc', 3));

Week3/homework/4-step3.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
'use strict';
22
// paste your freeCodeCamp solutions in here
3+
//Object Oriented Programming: Define a Constructor Function
4+
function Dog(){
5+
this.name = "Bob";
6+
this.color = "red";
7+
this.numLegs = 4;
8+
}
9+
10+
//Object Oriented Programming: Use a Constructor to Create Objects
11+
function Dog() {
12+
this.name = "Rupert";
13+
this.color = "brown";
14+
this.numLegs = 4;
15+
}
16+
// Add your code below this line
17+
18+
let hound = new Dog();

Week3/homework/5-step3.js

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

Week3/homework/6-step3.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@ 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+
for (let i = 0; i < arr2d.length; i++) {
8+
for (let z = 0; z < arr2d[i].length; z++) {
9+
console.log(arr2d[i][z]);
10+
}
11+
}
12+
13+
for (let i = 0; i < arr3d.length; i++) {
14+
for (let z = 0; z < arr3d[i].length; z++){
15+
for (let j = 0; j < arr3d[i].length; j++){
16+
17+
console.log(arr3d[i][z][j]);
18+
}
19+
}
20+
}

Week3/homework/7-step3.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const x = 9;
44
function f1(val) {
55
val = val + 1;
66
return val;
7+
78
}
89

910
f1(x);
@@ -22,4 +23,15 @@ f2(y);
2223
console.log(y);
2324

2425
// Add your explanation as a comment here
25-
26+
/*
27+
**by value**
28+
The first function (f1) passes the variable by value, as all Javascript functions pass primitive data functions this way.
29+
however this does not explain why the value is not 10, as the function is to add 1 to the value of x.
30+
31+
if you were to ask the value of x within the function, it will give you 10, however outside the function it will give you only the original value (9);
32+
This is because they are now two difference variables rather than being the same variable. so if you change one, it will not affect the other.
33+
34+
**by reference**
35+
the second passes the variable by reference, as Javascript objects are always passed by reference. This means that they variables are one and the same,
36+
so if you affect the variable within the function it will affect the variable outside the function (as they are the same)
37+
*/

Week3/homework/step4-bonus.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@
33
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
6-
6+
function noDupe() {
7+
values.filter(function(item, pos) {
8+
return values.indexOf(item) == pos;
9+
})
10+
};
11+
12+
function noDupe(values) {
13+
var seen = {};
14+
return values.filter(function(item) {
15+
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
16+
});
17+
}
718
// Replace `yourFunction` with the name of the function you just created
8-
const uniqueValues = yourFunction(values);
19+
const uniqueValues = noDupe(values);
920

1021
console.log(uniqueValues);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy