Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement of JavaScript2 Week1 HomeWork from Ann-Grabetski
  • Loading branch information
anngrrr committed Sep 3, 2019
commit 60d625fd49f0319c27f27d15f8b8534398b80cfe
206 changes: 88 additions & 118 deletions Week1/homework/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

{
// **1.1** declaring an array

const bookTitles = [
const booksArray = [
`The_Goldfinch`,
`A_Garden_of_Earthly_Delights`,
`Beloved`,
Expand All @@ -16,130 +15,101 @@
`The_Year_of_Magical_Thinking`
];

/* **1.3** function that generate a `ul` with `li` elements for each book title

function createListOfBookTitles() {

const emptyList = document.createElement("ul");

function placeList() {
const placeForList = document.body;
placeForList.insertBefore(emptyList, placeForList.childNodes[2]);
};
placeList();

function addTitlesToList() {
let x;
for (x in bookTitles) {
const newListItem = document.createElement("li");
const titleItem = document.createTextNode(bookTitles[x]);
newListItem.appendChild(titleItem);
emptyList.appendChild(newListItem);
};
};
addTitlesToList();
const booksObject = {
The_Goldfinch: {
title: `The Goldfinch`,
author: `Donna Tartt`,
original_language: `English`
},
A_Garden_of_Earthly_Delights: {
title: `A Garden of Earthly Delights`,
author: `Carol Oates`,
original_language: `English`
},
Beloved: {
title: `Beloved`,
author: `Toni Morrison`,
original_language: `English`
},
There_Once_Lived_a_Woman_Who_Tried_to_Kill_Her_Neighbors_Baby: {
title: `There Once Lived a Woman Who Tried to Kill Her Neighbo\'s Baby`,
author: `Lyudmila Petrushevskaya`,
original_language: `Russian`
},
The_House_of_the_Spirits: {
title: `The House of the Spirits`,
author: `Isabel Allende`,
original_language: `Spanish`
},
The_Left_Hand_of_Darkness: {
title: `The Left Hand of Darkness`,
author: `Ursula K. Le Guin`,
original_language: `English`
},
The_Unwomanly_Face_of_War: {
title: `The Unwomanly Face of War`,
author: `Svetlana Alexievich`,
original_language: `Russian`
},
Chernobyl_Prayer: {
title: `Chernobyl Prayer`,
author: `Svetlana Alexievich`,
original_language: `Russian`
},
Runaway: {
title: `Runaway`,
author: `Alice Munro`,
original_language: `English`
},
The_Year_of_Magical_Thinking: {
title: `The Year of Magical Thinking`,
author: `Joan Didion`,
original_language: `English`
}
};
createListOfBookTitles();
*/

// **1.4** Making an object

const Books = bookTitles.reduce((acc, elem) => {
acc[elem] = {title:``, author:``, original_language:``};
return acc;
}, {});

// Adding content

Books.The_Goldfinch.title = `The Goldfinch`;
Books.The_Goldfinch.author =`Donna Tartt`;
Books.The_Goldfinch.original_language = `English`;

Books.A_Garden_of_Earthly_Delights.title = `A Garden of Earthly Delights`;
Books.A_Garden_of_Earthly_Delights.author = `Carol Oates`;
Books.A_Garden_of_Earthly_Delights.original_language = `English`;

Books.Beloved.title = `Beloved`;
Books.Beloved.author =`Toni Morrison`;
Books.Beloved.original_language = `English`;

Books.There_Once_Lived_a_Woman_Who_Tried_to_Kill_Her_Neighbors_Baby.title = `There Once Lived a Woman Who Tried to Kill Her Neighbo\`s Baby`;
Books.There_Once_Lived_a_Woman_Who_Tried_to_Kill_Her_Neighbors_Baby.author = `Lyudmila Petrushevskaya`;
Books.There_Once_Lived_a_Woman_Who_Tried_to_Kill_Her_Neighbors_Baby.original_language = `Russian`;

Books.The_House_of_the_Spirits.title = `The House of the Spirits`;
Books.The_House_of_the_Spirits.author = `Isabel Allende`;
Books.The_House_of_the_Spirits.original_language = `Spanish`;

Books.The_Left_Hand_of_Darkness.title = `The Left Hand of Darkness`;
Books.The_Left_Hand_of_Darkness.author = `Ursula K. Le Guin`;
Books.The_Left_Hand_of_Darkness.original_language = `English`;

Books.The_Unwomanly_Face_of_War.title = `The Unwomanly Face of War`;
Books.The_Unwomanly_Face_of_War.author = `Svetlana Alexievich`;
Books.The_Unwomanly_Face_of_War.original_language = `Russian`;
for (const book of booksArray) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't forget to remove debugging code like this loop before submitting your homework.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

const title = booksObject[book].title;
const author = booksObject[book].author;
const original_language = booksObject[book].original_language;

Books.Chernobyl_Prayer.title = `Chernobyl Prayer`;
Books.Chernobyl_Prayer.author = `Svetlana Alexievich`;
Books.Chernobyl_Prayer.original_language = `Russian`;

Books.Runaway.title = `Runaway`;
Books.Runaway.author = `Alice Munro`;
Books.Runaway.original_language = `English`;

Books.The_Year_of_Magical_Thinking.title = `The Year of Magical Thinking`;
Books.The_Year_of_Magical_Thinking.author = `Joan Didion`;
Books.The_Year_of_Magical_Thinking.original_language = `English`;

console.log(Books);

// **1.5** function that take the actual information about the book from the object
console.log(title);
console.log(author);
console.log(original_language);
};

const booksList = document.createElement(`ul`);
document.body.insertBefore(booksList, document.body.childNodes[2]);

function placeContent() {
let x;
for (x in Books) {
const listItem = document.createElement(`li`);
booksList.appendChild(listItem);
listItem.setAttribute(`id`, x);

const book = Books[`${x}`];

const title = document.createElement(`h3`);
listItem.appendChild(title);
const titleContent = document.createTextNode(book[`title`]);
title.appendChild(titleContent);

const author = document.createElement(`p`);
listItem.appendChild(author);
author.setAttribute(`class`, `author`);
const authorContent = document.createTextNode(`By ${book[`author`]}`);
author.appendChild(authorContent);

const originalLanguage = document.createElement(`p`);
listItem.appendChild(originalLanguage);
const originalLanguageContent = document.createTextNode(`Original language: ${book[`original_language`]}`);
originalLanguage.appendChild(originalLanguageContent);
};
};
placeContent();

// **1.8** function that place an image at the corresponding `li` element

function placeImages() {
let x;
for (x in Books) {
const listItem = document.getElementById(`${x}`);
for (const book of booksArray) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good!

const listItem = document.createElement(`li`);
booksList.appendChild(listItem);
listItem.setAttribute(`id`, book);

const title = document.createElement(`h3`);
listItem.appendChild(title);
const titleContent = document.createTextNode(booksObject[book].title);
title.appendChild(titleContent);

const author = document.createElement(`p`);
listItem.appendChild(author);
author.setAttribute(`class`, `author`);
const authorContent = document.createTextNode(`By ${booksObject[book].author}`);
author.appendChild(authorContent);

const original_language = document.createElement(`p`);
listItem.appendChild(original_language);
const original_languageContent = document.createTextNode(`Original language: ${booksObject[book].original_language}`);
original_language.appendChild(original_languageContent);
};

const image = document.createElement(`img`);
listItem.appendChild(image);
listItem.insertBefore(image, listItem.childNodes[0]);
image.setAttribute(`src`, `images/${x}.jpg`);
};
};
placeImages();
for (const book of booksArray) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Although this solution to add the images works it is not what was asked for in the instructions. Please read 1.7 and 1.8 again. And implement it like it is written there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

const listItem = document.getElementById(book);

const image = document.createElement(`img`);
listItem.appendChild(image);
listItem.insertBefore(image, listItem.childNodes[0]);
image.setAttribute(`src`, `images/${book}.jpg`);
};

};