Skip to content
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

updated JS file #12

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
<title>National Parks</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<body>
<header class="banner">
<h1>National Parks</h1>
</header>
<div class="sorter">
Sort by:
<a href="" class="sort-link" id="name-sorter">Name</a> |
<a href="" class="sort-link" id="rating-sorter">Rating</a>
</div>
<main>
<section class="park-display">
<h2>Biscayne National Park</h2>
Expand Down
109 changes: 109 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
console.log(document);

// const header = document.querySelector("h1");
// console.log(header.innerText);

// Find an element with the class value.
const value = document.querySelector(".value");
// console.log(value);
// Find a <button> element.
const btn = document.querySelector("button");
// console.log(btn.outerHTML);
// Find an element with the class area.
const area = document.querySelector(".area-display .value");
// console.log(area);
// Find a <div> that is a descendant of an element with the class stat. (Hint: Look up descendant selectors in the documentation).
const descendant = document.querySelector(".stats div");
// console.log(descendant.innerHTML);
// Find an element with the class hello. Take careful note of what is returned there.
// console.log(document.querySelector(".hello"));

const allBtn = document.querySelectorAll("button");

// for (let el of allBtn.values()) console.log(el);

// Get a list of all the <div> elements containing ratings on the page. Log them to the console using the values() method.
const ratings = document.querySelectorAll(".rating-display");
// for (let rating of ratings.values()) console.log(rating);
// Get a list of all the <div> elements containing areas on the page. Log them to the console using a simple for loop.
const areas = document.querySelectorAll(".area-display");
// for (let area of areas.values()) console.log(area);

const description = document.querySelectorAll(".description-display");

for (let el of description.values()) {
let text = el.innerText;
if (text.length > 250) {
text = text.slice(0, 250);
text += '<a href="#">...</a>';
}
el.innerHTML = text;
}

const ratingsValue = document.querySelectorAll(".rating-display .value");

/* for (let rating of ratingsValue) {
const ratingNum = Number(rating.innerText);

if (ratingNum > 4.7) {
// rating.style.fontWeight = "bold";
// rating.style.color = "#3ba17c";
rating.classList.toggle("high-rating");
rating.classList.toggle("value");
}
} */

const parks = document.querySelectorAll(".park-display");
const parkLength = parks.length;
const newEl = document.createElement("div");

newEl.innerText = `${parkLength} exciting parks to visit!`;
newEl.classList.add(".header-statement");

const header = document.querySelector("header");
header.appendChild(newEl);

const main = document.querySelector("main");
const firstPark = document.querySelector(".park-display");

// main.removeChild(firstPark);

const firstBtn = document.querySelectorAll(".rate-button");
allBtn.forEach((btn) => {
btn.addEventListener("click", (event) => {
const parent = event.target.parentNode;
parent.style.backgroundColor = "#c8e6c9";
});
});

const nameSorter = document.getElementById("name-sorter");
nameSorter.addEventListener("click", (event) => {
event.preventDefault();
main.innerHTML = "";
const parksArr = Array.from(parks);
parksArr.sort((a, b) => {
const parkNameA = a.querySelector("h2");
const parkNameb = b.querySelector("h2");
return parkNameA.innerText.toLowerCase() > parkNameb.innerText.toLowerCase()
? 1
: -1;
});

for (let el of parksArr) main.appendChild(el);
});

const rateSorter = document.getElementById("rating-sorter");
rateSorter.addEventListener("click", (event) => {
event.preventDefault();
main.innerHTML = "";
const parksArr = Array.from(parks);

parksArr.sort((a, b) => {
const parkRateA = a.querySelector(".rating-display .value").innerText;
const parkRateB = b.querySelector(".rating-display .value").innerText;

return Number(parkRateA) > Number(parkRateB) ? -1 : 1;
});

for (let el of parksArr) main.appendChild(el);
});
19 changes: 19 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ main {
color: rgba(255, 255, 255, 0.5);
}

.high-rating {
color: #3ba17c;
font-size: 1.5rem;
font-weight: bold;
text-shadow: 1px 1px rgba(0, 0, 0, 0.2);
text-align: center;
}

.header-statement {
color: #ffffff;
font-size: 1.2rem;
}

.sorter {
width: 90%;
margin: 0 auto;
padding: 6px;
}

@media screen and (min-width: 600px) {
main {
grid-template-columns: repeat(2, 1fr);
Expand Down