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

Skip to content

Ivy js2 wk2 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
Binary file added Week1/.DS_Store
Binary file not shown.
165 changes: 157 additions & 8 deletions Week1/homework/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,160 @@
'use strict';

{
const bookTitles = [
// Replace with your own book titles
'harry_potter_chamber_secrets',
];

// Replace with your own code
console.log(bookTitles);
'use strict';

// array of books containing 10 strings. lowercase
const bookTitles = [
'going_down_river_road',
'the_river_and_the_source',
"salem's_lot",
'americanah',
'things_fall_apart',
'animal_farm',
'the_river_between',
'the_dead_stay_dumb',
'arrow_of_god',
'a_song_of_ice_and_fire',
];

// Replace with your own code
// const showBooks = Object.values(bookNames);
// console.log(bookTitles);

// the part below not showing on console on chrome
// function printBookNames(bookTitles) {
// console.table(bookTitles);
// }
//printing out the string of the above array produces:
// for (let i = 0; i < bookNames.length; i++) {
// console.log(bookNames[i]);
// }
// eslint-disable-next-line no-unused-vars
// 1.3. Make a function (or functions) that generate a ul with li elements for each book ID in the array using a for loop.

// 1.4 create object containing details of each book:
const bookInfo = {
going_down_river_road: {
title: 'Going Down River Road',
author: 'Meja Mwangi',
language: 'English',
published: 1976,
},

the_river_and_the_source: {
title: 'The River And The Source',
author: 'Margaret Ogola',
language: 'English',
published: 1994,
},

homing_in: {
title: 'Salem´s Lot',
author: 'Stephen King',
language: 'English',
published: 1975,
},

americanah: {
title: 'Americanah',
author: 'Chimamanda Ngozi Adichie',
language: 'English',
published: 2013,
},

things_fall_apart: {
title: 'Things Fall Apart',
author: 'Chinua Achebe',
language: 'English',
published: 1958,
},

animal_farm: {
title: 'Animal Farm',
author: 'George Orwell',
language: 'English',
published: 1945,
},

the_river_between: {
title: 'The River Between',
author: 'Ngugi wa Thiong´o',
language: 'English',
published: 1965,
},

the_dead_stay_dumb: {
title: 'The Dead Stay Dumb',
author: 'James Hardley Chase',
language: 'English',
published: 1939,
},

arrow_of_god: {
title: 'Arrow of God',
author: 'Chinua Achebe',
language: 'English',
published: 1964,
},

a_song_of_ice_and_fire: {
title: 'A Song of Ice and Fire',
author: 'George R.R. Martin',
language: 'English',
published: 1996,
},
};
//console.log(Object.keys(bookInfo));

const bookImgs = {
going_down_river_road: './bookImgs/going-down-river-road.jpg',
the_river_between: './bookImgs/the_river_between.jpg',
homing_in: './bookImgs/homing_in.jpg',
americanah: './bookImgs/americanah.jpg',
things_fall_apart: './bookImgs/things_fall_apart.jpg',
animal_farm: './bookImgs/animal_farm.jpeg',
arrow_of_god: './bookImgs/arrow_of_god.jpg',
a_song_of_ice_and_fire: './bookImgs/a_song_of_ice_and_fire.jpg',
the_dead_stay_dumb: './bookImgs/the_dead.jpg',
the_river_and_the_source: './bookImgs/the_river.jpg',
};

function bookList() {
const bookUl = document.createElement('ul');
bookUl.id = 'bookContainer';
document.body.appendChild(bookUl);

for (let key in bookInfo) {
const bookItems = document.createElement('li');
bookItems.id = 'book-items';
const bookTitle = document.createElement('h2');
bookTitle.id = 'book-title';
bookTitle.innerText = bookInfo[key].title;
bookItems.appendChild(bookTitle);

const bookDiv = document.createElement('div');
bookDiv.setAttribute('id', key);
bookItems.appendChild(bookDiv);

const content = document.createAttribute('div');
content.id = 'book-contents';
content.innerText = `<p>Title: ${bookInfo[key].title}</p>
<p>Author: ${bookInfo[key].author}</p>
<p>Language: ${bookInfo[key].language}</p>
<p>Published: ${bookInfo[key].published}</p>`;
//bookDiv.appendChild(content);
bookUl.appendChild(bookItems);
}
//creating a function to add images to books:
for (const bookKey in bookImgs) {
const bookDiv = document.getElementById(bookKey);
const imgContainer = document.createElement('div');
imgContainer.id = 'book-img';
bookDiv.appendChild(imgContainer);

//adding pic from book obj with attr
const pic = document.createElement('img');
pic.setAttribute('src', bookImgs[bookKey]);
imgContainer.appendChild(pic);
}
}
bookList();
Binary file added Week1/homework/bookImgs/.DS_Store
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/americanah.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/animal_farm.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/arrow_of_god.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/going-down-river-road.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/homing_in.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/the_dead.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/the_river.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/the_river_between.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week1/homework/bookImgs/things_fall_apart.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion Week1/homework/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<!-- replace this with your HTML content -->
<!-- replace this with your HTML content -->
<!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>Week-1 Js 2</title>
<link rel="styleSheet" type="text/css" href="./style.css" />
</head>

<body>
<script type="text/javascript" src="./app.js"></script>
</body>
</html>
5 changes: 4 additions & 1 deletion Week1/homework/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/* add your styling here */
/* add your styling here */
body {
font-family: 'Helvetica Neue', Helvetica, sans-serif;
}
Binary file added Week2/.DS_Store
Binary file not shown.
15 changes: 12 additions & 3 deletions Week2/homework/maartjes-work.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,30 @@ const tuesday = [
const maartjesTasks = monday.concat(tuesday);
const maartjesHourlyRate = 20;

/**
* Use of High order functions: map, filter, toFixed
*/

function computeEarnings(tasks, hourlyRate) {
// Replace this comment and the next line with your code
console.log(tasks, hourlyRate);
// mapping duration of tasks from minutes to hours and filtering out duties < 2h
const timeInHours = tasks.map(time => time.duration / 60).filter(duty => duty >= 2);
const salaryPerDay = timeInHours.map(hoursPerDay => hoursPerDay * hourlyRate);
const totalSalary = salaryPerDay.reduce((sum, dailySalary) => sum + dailySalary, 0);
const euroSalary = totalSalary.toFixed(2);
return euroSalary;
Copy link

Choose a reason for hiding this comment

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

Nice and clear code 👍

}

// eslint-disable-next-line no-unused-vars
const earnings = computeEarnings(maartjesTasks, maartjesHourlyRate);

// add code to convert `earnings` to a string rounded to two decimals (euro cents)

console.log(`Maartje has earned €${'replace this string with the earnings rounded to euro cents'}`);
console.log(`Maartje has earned €${earnings}`);

// Do not change or remove anything below this line
module.exports = {
maartjesTasks,
maartjesHourlyRate,
computeEarnings,
};
//completed//
9 changes: 7 additions & 2 deletions Week2/homework/map-filter.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

function doubleOddNumbers(numbers) {
// Replace this comment and the next line with your code
// the following iterates the variables in numbers(with filter) and checking if they´re divisible by 2
// const newNumbers = [];
// for (let i = 0; i =numbers.length)
numbers = numbers.filter(num => num % 2 === 1).map(num => num * 2);
console.log(numbers);
return numbers;
}

const myNumbers = [1, 2, 3, 4];
console.log(doubleOddNumbers(myNumbers));
console.log(doubleOddNumbers(myNumbers)); // output : [2, 6]

// Do not change or remove anything below this line
module.exports = {
myNumbers,
doubleOddNumbers,
};
//completed
Binary file added Week3/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions Week3/homework/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "homework",
"version": "1.0.0",
"description": "",
"main": "step2-1.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
1 change: 1 addition & 0 deletions Week3/homework/step2-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
function foo(func) {
// What to do here?
// Replace this comment and the next line with your code
func();
console.log(func);
}

Expand Down
30 changes: 19 additions & 11 deletions Week3/homework/step2-2.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
'use strict';
function sayThree(number) {
console.log(`your number ${number} is divisible by 3`);
}

function sayFive(number) {
console.log(`your number ${number} is divisible by 5`);
}

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
const numbers = [];
// creating an empty number array and populating it:

// Replace this comment and the next line with your code
console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);
}
const numbers = [];
for (let i = startIndex; i <= stopIndex; i++) {
numbers.push[i];

function sayThree(number) {
// Replace this comment and the next line with your code
console.log(number);
// divisible by 3?
if (i % 3 === 0) {
console.log(threeCallback(i));
} else if (i % 5 === 0) {
console.log(fiveCallback(i));
}
}
}

function sayFive(number) {
// Replace this comment and the next line with your code
console.log(number);
}
// console.log(startIndex, stopIndex, threeCallback, fiveCallback, numbers);

threeFive(10, 15, sayThree, sayFive);

Expand Down
34 changes: 24 additions & 10 deletions Week3/homework/step2-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
function repeatStringNumTimesWithFor(str, num) {
// eslint-disable-next-line prefer-const
let result = '';

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

Expand All @@ -13,30 +19,38 @@ function repeatStringNumTimesWithFor(str, num) {

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

//***********************************

// 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
console.log(str, num, result);

let counter = 0;
while (counter < num) {
result += str;
counter++;
}
return result;
}

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

// 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
console.log(str, num, result);

let i = 0;
if (num > 0) {
do {
result += str;
i++;
} while (i < num);
}
return result;
}

// Replace this comment and the next line with your code
//console.log(str, num, result);

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

// Do not change or remove anything below this line
Expand Down
3 changes: 3 additions & 0 deletions Week3/homework/step2-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

function Dog() {
// add your code here
this.name = 'Roger';
this.color = 'black';
this.numLegs = 4;
}

const hound = new Dog();
Expand Down
Loading