scrimba
Helen Chong's JavaScriptmas 2023 Solutions
Helen Chong's JavaScriptmas 2023 Day 1 solution
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Helen Chong's JavaScriptmas 2023 Day 1 solution
AboutCommentsNotes
Helen Chong's JavaScriptmas 2023 Day 1 solution
Expand for more info
index.js
run
preview
console
const christmasCountdownDisplay = document.getElementById("christmas-countdown-display");
const christmasHoursDisplay = document.getElementById("christmas-hours-display");
const christmasMinutesDisplay = document.getElementById("christmas-minutes-display");
const christmasSecondsDisplay = document.getElementById("christmas-seconds-display");

const newYearCountdownDisplay = document.getElementById("newyear-countdown-display");
const newYearHoursDisplay = document.getElementById("newyear-hours-display");
const newYearMinutesDisplay = document.getElementById("newyear-minutes-display");
const newYearSecondsDisplay = document.getElementById("newyear-seconds-display");
const nextYearDisplay = document.getElementById("next-year");

function renderCountdown(){
// Task:
// - Get today's date (you only need the day).
// - Calculate remaining days.
// - Display remaining days in countdownDisplay.
const currentYear = new Date().getFullYear();
const currentTime = new Date();

const christmas = new Date(`December 25 ${currentYear} 00:00:00`);
const newYear = new Date(`January 1 ${currentYear + 1} 00:00:00`);
const nextYear = currentYear + 1;

function getRemainingTIme(event) {
const totalTime = event - currentTime;
const day = Math.floor(totalTime / 1000 / 60 / 60 / 24);
const hour = Math.floor(totalTime / 1000 / 60 / 60) % 24;
const minute = Math.floor(totalTime / 1000 / 60) % 60;
const second = Math.floor(totalTime / 1000) % 60;

return { totalTime, day, hour, minute, second };
}

let christmasDays = getRemainingTIme(christmas).day;
let christmasHours = getRemainingTIme(christmas).hour;
let christmasMinutes = getRemainingTIme(christmas).minute;
let christmasSeconds = getRemainingTIme(christmas).second;

let newYearDays = getRemainingTIme(newYear).day;
let newYearHours = getRemainingTIme(newYear).hour;
let newYearMinutes = getRemainingTIme(newYear).minute;
let newYearSeconds = getRemainingTIme(newYear).second;

christmasCountdownDisplay.textContent = christmasDays;
christmasHoursDisplay.textContent = christmasHours;
christmasMinutesDisplay.textContent = christmasMinutes < 10 ? '0' + christmasMinutes : christmasMinutes;
christmasSecondsDisplay.textContent = christmasSeconds < 10 ? '0' + christmasSeconds : christmasSeconds;

newYearCountdownDisplay.textContent = newYearDays;
newYearHoursDisplay.textContent = newYearHours;
newYearMinutesDisplay.textContent = newYearMinutes < 10 ? '0' + newYearMinutes : newYearMinutes;
newYearSecondsDisplay.textContent = newYearSeconds < 10 ? '0' + newYearSeconds : newYearSeconds;

nextYearDisplay.textContent = nextYear;
}

// renderCountdown()

setInterval(renderCountdown, 1000);

// Stretch goals:
// - Display hours, minutes, seconds.
// - Add a countdown for another festival, your birthday, or Christmas 2022.
Console
/index.html
LIVE