From 4b9434d44f0462be0e23d663493832945f68efe3 Mon Sep 17 00:00:00 2001 From: Abdulmajeed Jaafer Date: Mon, 26 Apr 2021 19:29:30 +0300 Subject: [PATCH] challenge 1 done! --- index.html | 13 +++- script/script.js | 187 ++++++++++++++++++++++++++++------------------- 2 files changed, 123 insertions(+), 77 deletions(-) diff --git a/index.html b/index.html index 8a79e88..53a0a86 100644 --- a/index.html +++ b/index.html @@ -16,9 +16,18 @@ id="name" placeholder="enter a character name" /> - - + + + + +

total result :

+
diff --git a/script/script.js b/script/script.js index c9750ea..3ac76e2 100644 --- a/script/script.js +++ b/script/script.js @@ -1,91 +1,128 @@ // all the magic will happen now ! +document.addEventListener("DOMContentLoaded", (event) => { + //the event occurred -class Character { - constructor(id, name, align, power, year) { - this.id = id; - this.name = name; - this.align = align; - this.power = power; - this.year = year; - } - checkCharacter() { - if (this.align.includes("good")) { - return "good"; - } else if (this.align.includes("bad")) { - return "bad"; - } else { - return "natural"; + class Character { + constructor(id, name, align, power, year) { + this.id = id; + this.name = name; + this.align = align; + this.power = power; + this.year = year; + } + checkCharacter() { + if (this.align.includes("good")) { + return "good"; + } else if (this.align.includes("bad")) { + return "bad"; + } else { + return "natural"; + } } } -} -class Characters { - constructor() { - this.arrayOfCharacters = []; - } + class Characters { + constructor() { + this.arrayOfCharacters = []; + } - loadData(id, name, align, power, year) { - const newCharacter = new Character(id, name, align, power, year); - this.arrayOfCharacters.push(newCharacter); - } + loadData(id, name, align, power, year) { + const newCharacter = new Character(id, name, align, power, year); + // console.log(newCharacter); + this.arrayOfCharacters.push(newCharacter); + } - getData() { - return this.arrayOfCharacters; - } - searchByName(name) { - return this.arrayOfCharacters.find((item) => { - if (item.name.includes(name)) return item; - }); + getData() { + return this.arrayOfCharacters; + } + + searchByName(name) { + return this.getData().filter((person) => + person.name.toLowerCase().includes(name.toLowerCase()) + ); + } + searchByStatus(status) { + return this.getData().filter((person) => person.align == status); + } + searchByBoth(name, status) { + return this.getData().filter((person) => { + if ( + person.align == status && + person.name.toLowerCase().includes(name.toLowerCase()) + ) { + return person; + } + }); + } } -} -const characters = new Characters(); -fetch("../marvel.json") - .then((response) => response.json()) - .then((json) => loadDataToCharacters(json)) - .catch((err) => console.log(err)); -function loadDataToCharacters(json) { - json.forEach((element) => { - characters.loadData( - element.id, - element.name, - element.align, - element.power, - element.year - ); - }); -} + const characters = new Characters(); -const btn1 = document.getElementById("search"); -const btn2 = document.getElementById("check-align"); -const nameInput = document.getElementById("name"); -const body = document.querySelector("body"); -const data = characters.getData(); + function loadDataToCharacters(json) { + json.forEach((element) => { + characters.loadData( + element.id, + element.name, + element.align, + element.power, + element.year + ); + }); + } -btn1.addEventListener("click", (e) => { - e.preventDefault(); - const nameTxt = nameInput.value; + fetch("../marvel.json") + .then((response) => response.json()) + .then((json) => loadDataToCharacters(json)) + .catch((err) => console.log(err)); - if (nameTxt === "") return; - const foundObject = searchByName(data, nameTxt); - console.log(foundObject); - body.innerHTML += `

${foundObject.name}

`; -}); + const form = document.getElementById("form"); + let filter = document.querySelector("#filter"); + let totalResult = document.querySelector("#totalResult"); + let searchText = document.getElementById("name"); + const resultDiv = document.querySelector("#result"); -btn2.addEventListener("click", (e) => { - e.preventDefault(); - if (nameInput.value === "") { - alert("please enter a character name!"); - return; + function renderHtml(data) { + resultDiv.innerHTML = ""; + totalResult.parentElement.style.display = "block"; + totalResult.innerHTML = data.length; + data.forEach((person, index) => { + resultDiv.innerHTML += `
+

${index + 1}

+
${person.name}
+
${person.power}
+
${person.align}
+
${person.year}
+
+
`; + }); } - const foundObject = searchByName(data, nameInput.value); - body.innerHTML += `

${ - foundObject.name - } ${foundObject.checkCharacter()}

`; -}); -function searchByName(data, name) { - return data.find((element) => { - if (element.name.toLowerCase().includes(name.toLowerCase())) return element; + //search event + form.addEventListener("submit", (e) => { + e.preventDefault(); + + //filter by status + if (searchText.value == "" && filter.value != "") { + let filterRes = characters.searchByStatus(filter.value); + renderHtml(filterRes); + filter.value = ""; + return; + } + //filter by search text + else if (searchText.value !== "" && filter.value == "") { + let searchRes = characters.searchByName(searchText.value); + renderHtml(searchRes); + searchText.value = ""; + return; + } + //search by both + else if (searchText.value !== "" && filter.value !== "") { + let searchRes = characters.searchByBoth(searchText.value, filter.value); + renderHtml(searchRes); + searchText.value = ""; + filter.value = ""; + return; + } + alert("try again!"); }); -} +});