JavaScript Practice with Ecommerce Book Store Dataset
JS Bytes Issue #7 - Become a better JS engineer
Last week, I shared a ChatGPT prompt that helps React developers turn HTML into reusable React components effortlessly.
In case you missed it, here’s the prompt again. I’m currently working on more prompts to simplify your development process.
This week, I’ve brought another challenge for you. We’ll focus on a book store dataset designed to help you practice JavaScript arrays and objects.
If you missed the previous issue related to e-commerce database challenge, you can find it here.
JavaScript Problem Solving Challenge
You are given a dummy e-commerce book store dataset, which simulates a typical API response. Study the dataset carefully, then answer the 3 questions related to this database.
(Better view at desktop)
const books = [
{
id: 1,
title: 'The Great Gatsby',
author: {
name: 'F. Scott Fitzgerald',
country: 'USA',
},
genre: 'Classic',
price: 1800,
stock: 300,
reviews: [
{ user: 'Alice', rating: 5, comment: 'Loved it!' },
{ user: 'Bob', rating: 4, comment: 'Great read.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 10 },
{ date: '02-06-2024', quantity: 15 },
{ date: '03-06-2024', quantity: 9 },
{ date: '04-06-2024', quantity: 30 },
{ date: '05-06-2024', quantity: 46 },
{ date: '06-06-2024', quantity: 6 },
{ date: '07-06-2024', quantity: 0 },
],
},
{
id: 2,
title: 'To Kill a Mockingbird',
author: {
name: 'Harper Lee',
country: 'USA',
},
genre: 'Classic',
price: 1250,
stock: 700,
reviews: [
{ user: 'Charlie', rating: 5, comment: 'A masterpiece.' },
{ user: 'Dave', rating: 3, comment: 'Average' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 7 },
{ date: '02-06-2024', quantity: 15 },
{ date: '03-06-2024', quantity: 9 },
{ date: '04-06-2024', quantity: 6 },
{ date: '05-06-2024', quantity: 36 },
{ date: '06-06-2024', quantity: 16 },
{ date: '07-06-2024', quantity: 40 },
],
},
{
id: 3,
title: '1984',
author: {
name: 'George Orwell',
country: 'UK',
},
genre: 'Dystopian',
price: 1500,
stock: 1000,
reviews: [
{ user: 'Eve', rating: 4, comment: 'Little boring.' },
{ user: 'Frank', rating: 5, comment: 'A must-read.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 70 },
{ date: '02-06-2024', quantity: 1 },
{ date: '03-06-2024', quantity: 19 },
{ date: '04-06-2024', quantity: 32 },
{ date: '05-06-2024', quantity: 21 },
{ date: '06-06-2024', quantity: 0 },
{ date: '07-06-2024', quantity: 0 },
],
},
{
id: 4,
title: 'The Catcher in the Rye',
author: {
name: 'J.D. Salinger',
country: 'USA',
},
genre: 'Classic',
price: 2900,
stock: 0,
reviews: [
{ user: 'Grace', rating: 4, comment: 'Really enjoyed it.' },
{ user: 'Hank', rating: 3, comment: 'It was okay.' },
],
availability: { online: false, inStore: false },
weeklySales: [
{ date: '01-06-2024', quantity: 25 },
{ date: '02-06-2024', quantity: 11 },
{ date: '03-06-2024', quantity: 44 },
{ date: '04-06-2024', quantity: 3 },
{ date: '05-06-2024', quantity: 1 },
{ date: '06-06-2024', quantity: 0 },
{ date: '07-06-2024', quantity: 11 },
],
},
{
id: 5,
title: 'Moby Dick',
author: {
name: 'Herman Melville',
country: 'USA',
},
genre: 'Adventure',
price: 2320,
stock: 20,
reviews: [
{ user: 'Ivy', rating: 2, comment: 'Did not enjoy reading' },
{ user: 'Jack', rating: 4, comment: 'Long but worth it.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 21 },
{ date: '02-06-2024', quantity: 62 },
{ date: '03-06-2024', quantity: 140 },
{ date: '04-06-2024', quantity: 4 },
{ date: '05-06-2024', quantity: 16 },
{ date: '06-06-2024', quantity: 87 },
{ date: '07-06-2024', quantity: 22 },
],
},
{
id: 6,
title: 'War and Peace',
author: {
name: 'Leo Tolstoy',
country: 'Russia',
},
genre: 'Historical Fiction',
price: 1350,
stock: 80,
reviews: [
{ user: 'Kelly', rating: 5, comment: 'A true classic.' },
{ user: 'Liam', rating: 3, comment: 'Very long but good.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 47 },
{ date: '02-06-2024', quantity: 2 },
{ date: '03-06-2024', quantity: 40 },
{ date: '04-06-2024', quantity: 14 },
{ date: '05-06-2024', quantity: 25 },
{ date: '06-06-2024', quantity: 7 },
{ date: '07-06-2024', quantity: 26 },
],
},
{
id: 7,
title: 'Pride and Prejudice',
author: {
name: 'Jane Austen',
country: 'UK',
},
genre: 'Romance',
price: 1780,
stock: 25,
reviews: [
{ user: 'Mia', rating: 2, comment: 'not good' },
{ user: 'Noah', rating: 3, comment: 'Average.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 11 },
{ date: '02-06-2024', quantity: 8 },
{ date: '03-06-2024', quantity: 0 },
{ date: '04-06-2024', quantity: 7 },
{ date: '05-06-2024', quantity: 18 },
{ date: '06-06-2024', quantity: 1 },
{ date: '07-06-2024', quantity: 3 },
],
},
{
id: 8,
title: 'The Hobbit',
author: {
name: 'J.R.R. Tolkien',
country: 'UK',
},
genre: 'Fantasy',
price: 900,
stock: 30,
reviews: [
{ user: 'Olivia', rating: 5, comment: 'Fantastic adventure.' },
{ user: 'Pete', rating: 4, comment: 'Loved it.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 1 },
{ date: '02-06-2024', quantity: 78 },
{ date: '03-06-2024', quantity: 57 },
{ date: '04-06-2024', quantity: 12 },
{ date: '05-06-2024', quantity: 14 },
{ date: '06-06-2024', quantity: 61 },
{ date: '07-06-2024', quantity: 47 },
],
},
{
id: 9,
title: 'Brave New World',
author: {
name: 'Aldous Huxley',
country: 'UK',
},
genre: 'Dystopian',
price: 3100,
stock: 0,
reviews: [
{ user: 'Quinn', rating: 1, comment: 'not good' },
{ user: 'Rachel', rating: 4, comment: 'Very intriguing.' },
],
availability: { online: false, inStore: false },
weeklySales: [
{ date: '01-06-2024', quantity: 11 },
{ date: '02-06-2024', quantity: 58 },
{ date: '03-06-2024', quantity: 10 },
{ date: '04-06-2024', quantity: 47 },
{ date: '05-06-2024', quantity: 98 },
{ date: '06-06-2024', quantity: 0 },
{ date: '07-06-2024', quantity: 0 },
],
},
{
id: 10,
title: 'The Lord of the Rings',
author: {
name: 'J.R.R. Tolkien',
country: 'UK',
},
genre: 'Fantasy',
price: 1599,
stock: 25,
reviews: [
{ user: 'Steve', rating: 5, comment: 'An epic saga.' },
{ user: 'Tina', rating: 5, comment: 'Absolutely amazing.' },
],
availability: { online: true, inStore: true },
weeklySales: [
{ date: '01-06-2024', quantity: 67 },
{ date: '02-06-2024', quantity: 34 },
{ date: '03-06-2024', quantity: 120 },
{ date: '04-06-2024', quantity: 44 },
{ date: '05-06-2024', quantity: 106 },
{ date: '06-06-2024', quantity: 26 },
{ date: '07-06-2024', quantity: 10 },
],
},
]
1. Get Daily Sales Summary
Write a function to get a summary of daily sales across all books. The function should return an object where each key is a date and the value is the total quantity of books sold on that date.
function getDailySalesSummary(books) {
// Your code here
}
2. Find the Most and Least Sold Book on a Specific Date
Write a function to find the most and least sold book on a specific date. The function should take two parameters: books
and date
. It should return an object with the most and least sold books on that date.
function findMostAndLeastSoldBooksOnDate(books, date) {
// Your code here
}
Output will be like this:
{
mostSoldBook: {},
leastSoldBook: {},
}
3. Find Top 3 Highest Rated Books
Write a function to find the top 3 highest rated books based on average rating. The function should return an array, each element of the array will be a book object with the highest average rating.
function findHighestRatedBook(books) {
// Your code here
}
3 JavaScript Interview Questions
What will be the output of following code?
console.log(!"JS Bytes")
A)
true
B)
false
C)
undefined
D)
null
Given the following code, what will the console output?
let a = "Flexy UI"
let b = new String("Flexy UI")
console.log(a == b)
console.log(a === b)
A)
true, true
B)
false, false
C)
true, false
D)
false, true
What will the following code output?
const set = new Set([1, 1, 2, 3, 4])
console.log(set)
A:
[1, 1, 2, 3, 4]
B:
[1, 2, 3, 4]
C:
{1, 1, 2, 3, 4}
D:
{1, 2, 3, 4}
Thank you for reading this issue!
If you enjoyed the challenge and want more coding exercises, feel free to check out our previous issues.
You can also do two more things to favor my content:
Don’t forget to subscribe for future newsletters.
Share it with others who might find it useful.