10000 done by annagabain · Pull Request #2 · annagabain/JavaScript2 · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Week3/homework/MY-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="MY-step2-1.js"></script>
<script src="MY-step2-2-Func_with_4_Arg.js"></script>
<script src="MY-step2-3-Repeat_a_String.js"></script>
<script src="MY-step2-4-Some_practice_with_objects.js"></script>
<script src="MY-step2-5-Nest_for_loops.js"></script>
<script src="MY-step2-6-Flatten_the_Array.js"></script>
<script src="MY-step2-7-Different_Results.js"></script>
<script src="MY-step3-Base_Scope_Closures.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

function foo(func) {
// What to do here?
// Replace this comment and the next line with your code
func()
console.log(func);
}

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

foo(bar);

// Do not change or remove anything below this line
Expand Down
23 changes: 23 additions & 0 deletions Week3/homework/MY-step2-1-Hello_I_am_bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
/*sample*/
// function doIt() {
// console.log('I am done');
// }
// setTimeout(doIt, 5000);

//Test this code by copy-pasting the whole foo and bar functions to the browser console
function foo(func) {
// What to do here?
// Replace this comment and the next line with your code
return ('Hi, I am AaaaT, who are you?');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem again. This function should take a function and invoke/call it. Not return anything.

//console.log(func);
}

function bar() {
console.log('Hello, I am bar!');
}
setTimeout(bar, 3000);
foo(bar);

// Do not change or remove anything below this line
module.exports = foo;
49 changes: 49 additions & 0 deletions Week3/homework/MY-step2-2-Func_with_4_Arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

/*2.2 You must write a function that takes 4 arguments.

A start value
An end value
A callback to call if the number is divisible by 3
A callback to use if the number is divisible by 5
The function should first generate an array containing values from start value to end value (inclusive).

Then the function should take the newly created array and iterate over it, and calling the first callback if the array value is divisible by 3.

The function should call the second callback if the array value is divisible by 5.

Both functions should be called if the array value is divisible by both 3 and 5. */

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
const numbers = [];
// Replace this comment and the next line with your code
for (let i = startIndex; i <= stopIndex; i++) {
numbers.push(i);
//A callback to call if the number is divisible by 3
if (i % 3 === 0) {
threeCallback(i);
}
//A callback to use if the number is divisible by 5
if (i % 5 === 0) {
fiveCallback(i);
}
}
}

threeFive()


function sayThree(number) {
// Replace this comment and the next line with your code
console.log(number, 'value is divisible by 3');
}

function sayFive(number) {
// Replace this comment and the next line with your code
console.log(number, 'value is divisible by 5');
}

threeFive(10, 15, sayThree, sayFive);

// Do not change or remove anything below this line
module.exports = threeFive;
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
'use strict';

/*-----------------------------------------------------------------------------------*/
// Use a 'for' loop
function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
for (let i = 0; i < num; i++) {
result += str;
}
console.log(str, num, result);

return result;
}

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

/*-----------------------------------------------------------------------------------*/
// Use a 'while' loop
function repeatStringNumTimesWithWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
while (num > 0) {
result += str;
num--;
}
console.log(str, num, result);

return result;
}

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

/*-----------------------------------------------------------------------------------*/

// Use a 'do...while' loop
function repeatStringNumTimesWithDoWhile(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

// Replace this comment and the next line with your code
do {
result += str;
} while (result.length < str.length * num);
console.log(str, num, result);

return result;
}

console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 3));
console.log('do-while', repeatStringNumTimesWithDoWhile('abc', 7));

/*-----------------------------------------------------------------------------------*/
// Do not change or remove anything below this line
module.exports = {
repeatStringNumTimesWithFor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

function Dog() {
// add your code here
this.name = "Woofy Boy",
this.color = "Brown",
this.numLegs = 4;

}

const hound = new Dog();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
'use strict';
//'use strict';

function multiplyAll(arr) {
// eslint-disable-next-line
let product = 1;

// Replace this comment and the next line with your code
for(let i=0; i < arr.length; i++){
for (let j=0; j < arr[i].length; j++){
product = product * arr[i][j];
}
}
console.log(arr, product);

return product;
}

const result = multiplyAll([[1, 2], [3, 4], [5, 6]]);
console.log(result); // 720
const result = multiplyAll([[3, 4], [5, 6], [7, 8]]);
console.log(result); // 20160

// Do not change or remove anything below this line
module.exports = multiplyAll;

40 changes: 40 additions & 0 deletions Week3/homework/MY-step2-6-Flatten_the_Array.js
AE18
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const arr2d = [[1, 2], [3, 4], [5, 6]];
const arr3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]];

// const flattenArray2d = (arr) => arr.flat()
// const flattenArray3d = (arr) => arr.flat(2) //depth of 2

// const flattenArray2d = flatten
// const flattenArray3d = flatten


function flattenArray2d(arr) {
let flattenArr2 = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
flattenArr2.push(arr[i][j])
}
}
return flattenArr2;
}

function flattenArray3d(arr) {
const flattenArr3 = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
for (let l = 0; l < arr[i][j].length; l++) {
flattenArr3.push(arr[i][j][l]);
}
}
}
return flattenArr3;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but also take a look at .flat() later. May have made things even easier ;)

}


//Do not change or remove anything below this line
module.exports = {
flattenArray2d,
flattenArray3d,
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ f2(y);
console.log(y);

// Add your explanation as a comment here

// in function f1 x is passed by value, therefore does not change
//in function f2 x is passed by reference, y is returned after going through the f2 function where the value of y(that is an object containing x) is changed by adding 1 to x. 9+1=10
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

function createBase(base) {
// Replace this comment and the next line with your code

return function(number) {
return base + number

console.log(base);
}
}


const addSix = createBase(6);

Expand Down
File renamed without changes.
23 changes: 0 additions & 23 deletions Week3/homework/step2-2.js

This file was deleted.

23 changes: 0 additions & 23 deletions Week3/homework/step2-6.js

This file was deleted.

0