Skip to content

Commit ae003e0

Browse files
committed
JS2 week2 mandatory
1 parent 8f921a6 commit ae003e0

File tree

3 files changed

+123
-7
lines changed

3 files changed

+123
-7
lines changed
6 KB
Binary file not shown.

Week-2/Homework/mandatory/2-exercises/exercises.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@
1313
* .....
1414
* </div>
1515
*/
16+
1617
function exerciseOne(arrayOfPeople) {
1718
let content = document.querySelector("#content");
19+
20+
for (let i = 0; i < arrayOfPeople.length; i++) {
21+
let h = document.createElement("H1");
22+
let t = document.createElement("H2");
23+
h.innerText = arrayOfPeople[i].name;
24+
t.innerText = arrayOfPeople[i].job;
25+
content.appendChild(h);
26+
content.appendChild(t);
27+
}
1828
}
1929

2030
/**
@@ -25,7 +35,12 @@ function exerciseOne(arrayOfPeople) {
2535
*
2636
*/
2737
function exerciseTwo(shopping) {
28-
//Write your code in here
38+
document.createElement("ul");
39+
for (let i = 0; i < shopping.length; i++) {
40+
let li = document.createElement("li");
41+
li.innerText = shopping[i];
42+
content.appendChild(li);
43+
}
2944
}
3045

3146
/**
@@ -57,8 +72,32 @@ function exerciseTwo(shopping) {
5772
5873
The end result should look something like this: https://hyf-js2-week1-makeme-ex1-demo.herokuapp.com/
5974
**/
75+
let bookpics = [
76+
"https://mitpress.mit.edu/sites/default/files/9780262640374.jpg",
77+
"https://images.gr-assets.com/books/1295465264l/8884400.jpg",
78+
"https://upload.wikimedia.org/wikipedia/en/8/8f/The_pragmatic_programmer.jpg",
79+
];
80+
6081
function exerciseThree(books) {
61-
//Write your code in here
82+
83+
let unorderedList = document.createElement("ul");
84+
content.appendChild(unorderedList);
85+
for (let i = 0; i < bookpics.length; i++) {
86+
let p = document.createElement("p");
87+
let list = document.createElement("li");
88+
let images = document.createElement("img");
89+
p.innerText = books[i].title + " " + books[i].author;
90+
images.src = bookpics[i];
91+
unorderedList.appendChild(list);
92+
list.appendChild(p);
93+
list.appendChild(images);
94+
95+
if (books[i].alreadyRead === true) {
96+
list.style.backgroundColor = "green";
97+
} else {
98+
list.style.backgroundColor = "red";
99+
}
100+
}
62101
}
63102

64103
//
@@ -74,7 +113,7 @@ function exerciseThree(books) {
74113
let people = [
75114
{ name: "Chris", job: "Teacher" },
76115
{ name: "Joanna", job: "Student" },
77-
{ name: "Boris", job: "Prime Minister" }
116+
{ name: "Boris", job: "Prime Minister" },
78117
];
79118

80119
exerciseOne(people);
@@ -87,18 +126,18 @@ const books = [
87126
{
88127
title: "The Design of Everyday Things",
89128
author: "Don Norman",
90-
alreadyRead: false
129+
alreadyRead: false,
91130
},
92131
{
93132
title: "The Most Human Human",
94133
author: "Brian Christian",
95-
alreadyRead: true
134+
alreadyRead: true,
96135
},
97136
{
98137
title: "The Pragmatic Programmer",
99138
author: "Andrew Hunt",
100-
alreadyRead: true
101-
}
139+
alreadyRead: true,
140+
},
102141
];
103142

104143
exerciseThree(books);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* clicking blue */
2+
3+
let blueButton = document.getElementById("blueBtn");
4+
let jumbotron = document.querySelector(".jumbotron");
5+
let donateBikeButton = document.querySelector(".buttons a:first-of-type");
6+
let volunteerbtn = document.querySelector(".buttons a:last-of-type");
7+
let onBlueButtonClick = function () {
8+
jumbotron.style.backgroundColor = `#588fbd`;
9+
donateBikeButton.style.backgroundColor = "#ffa500";
10+
volunteerbtn.style.backgroundColor = "black";
11+
volunteerbtn.style.color = "white";
12+
};
13+
blueButton.addEventListener("click", onBlueButtonClick);
14+
15+
/* Clicking orange */
16+
17+
let orangeButton = document.getElementById("orangeBtn");
18+
19+
let onOrangeButtonClick = function () {
20+
jumbotron.style.backgroundColor = `#f0ad4e`;
21+
donateBikeButton.style.backgroundColor = "#5751fd";
22+
volunteerbtn.style.backgroundColor = "#31b0d5";
23+
volunteerbtn.style.color = "white";
24+
};
25+
orangeButton.addEventListener("click", onOrangeButtonClick);
26+
27+
/* Clicking Green */
28+
29+
let greenButton = document.getElementById("greenBtn");
30+
31+
let onGreenButtonClick = function () {
32+
jumbotron.style.backgroundColor = `#87ca8a`;
33+
donateBikeButton.style.backgroundColor = "black";
34+
volunteerbtn.style.backgroundColor = "#8c9c08";
35+
};
36+
greenButton.addEventListener("click", onGreenButtonClick);
37+
38+
/* Validate User */
39+
40+
let inputEmail = document.getElementById("exampleInputEmail1");
41+
let inputName = document.getElementById("example-text-input");
42+
let description = document.getElementById("exampleTextarea");
43+
let submitButton = document.querySelector("form button");
44+
45+
let validateUser = function (e) {
46+
e.preventDefault();
47+
let emailValue = inputEmail.value;
48+
let nameValue = inputName.value;
49+
let descriptionValue = description.value;
50+
if (emailValue.trim() === "" || emailValue.includes("@") === false) {
51+
inputEmail.style.backgroundColor = "red";
52+
} else {
53+
inputEmail.style.backgroundColor = "white";
54+
}
55+
56+
if (nameValue.trim() === "") {
57+
inputName.style.backgroundColor = "red";
58+
} else {
59+
inputName.style.backgroundColor = "white";
60+
}
61+
62+
if (descriptionValue.trim() === "") {
63+
description.style.backgroundColor = "red";
64+
} else {
65+
description.style.backgroundColor = "white";
66+
}
67+
68+
if (
69+
descriptionValue.trim() !== "" &&
70+
nameValue.trim() !== "" &&
71+
emailValue.trim() !== "" &&
72+
emailValue.includes("@") === true
73+
) {
74+
alert("Thanks mate");
75+
}
76+
};
77+
submitButton.addEventListener("click", validateUser);

0 commit comments

Comments
 (0)