Skip to content
Open
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
89 changes: 88 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,91 @@ const photos = [
},
];

console.log('hello');
let gallery = document.getElementById('gallery');
let loadMoreBtn = document.querySelector('.load-more');
let filterBtn = document.querySelectorAll('.filter-buttons button');

let initialPhoto = 6;
let selectFilter = 'initial';

function displayPhoto() {

gallery.innerHTML = '';

let filteredPhoto = [];
if (selectFilter == 'initial' || selectFilter == 'All') {
filteredPhoto = photos;
}
else {
for (let i = 0; i < photos.length; i++) {
let photo = photos[i];

if (selectFilter == 'Nature' && photo.type == 'nature') {
filteredPhoto.push(photo);
}
else if (selectFilter == 'City' && photo.type == 'city') {
filteredPhoto.push(photo);
}
else if (selectFilter == 'Animals' && photo.type == 'animals') {
filteredPhoto.push(photo);
}
}
}

let totalPhoto;

if (selectFilter == 'All') {
totalPhoto = filteredPhoto.length;
}
else {
if (initialPhoto > filteredPhoto.length) {
totalPhoto = filteredPhoto.length;
}
else {
totalPhoto = initialPhoto;
}
}

for (let i = 0; i < totalPhoto; i++) {
let img = document.createElement('img');
img.src = filteredPhoto[i].url;
gallery.appendChild(img);
}

if (selectFilter == 'All' || initialPhoto >= filteredPhoto.length) {
loadMoreBtn.style.display = 'none';
}
else {
loadMoreBtn.style.display = 'block';
}
}

loadMoreBtn.onclick = function() {
initialPhoto += 6;
displayPhoto();
};

filterBtn[0].onclick = function() {

Choose a reason for hiding this comment

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

An improvement you can make: instead of using filterBtn[0] to set the click event listener, you should check the ID of the button element to get the type. Otherwise, if the order of the buttons gets changed, you wouldn't need to updated each of the click event functions. This would also allow you to have cleaner code, because you could create a more generic click event listener for all the buttons using a for loop.

selectFilter = 'All';
displayPhoto();
};

filterBtn[1].onclick = function() {
selectFilter = 'Nature';
initialPhoto = 6;
displayPhoto();
};

filterBtn[2].onclick = function() {
selectFilter = 'City';
initialPhoto = 6;
displayPhoto();
};

filterBtn[3].onclick = function() {
selectFilter = 'Animals';
initialPhoto = 6;
displayPhoto();
};

displayPhoto();