Using LLMs for efficient learning
Write a function that returns true if a book ‘The fundamentals of JavaScript’ is applicable for a specific search string:
Function Signature:
function isBookApplicable(searchString) {
// Your code here
}
Requirements:
true if the book title "The fundamentals of JavaScript" is applicable for the given search stringfalse otherwiseExample Usage:
console.log(isBookApplicable("javascript"));
// Output: true
console.log(isBookApplicable("javascript "));
// Output: true
console.log(isBookApplicable("python"));
// Output: false
console.log(isBookApplicable("JavaScript"));
// Output: true
console.log(isBookApplicable("JAVASCRIPT"));
// Output: true
Write a function called parseDateString that takes a date string as input and returns an object containing the parsed date components.
Function Signature:
function parseDateString(dateString) {
// Your code here
}
Input Format:
The input string will be in one of two formats:
MDY 10-21-1983 - Month-Day-Year formatDMY 21-10-1983 - Day-Month-Year formatThe format indicator (MDY or DMY) is always followed by a space, then the date values separated by hyphens.
Expected Output:
Your function should return an object with three properties:
day - the day as a numbermonth - the month as a numberyear - the year as a numberExample Usage:
console.log(parseDateString("MDY 10-21-1983"));
// Output: { day: 21, month: 10, year: 1983 }
console.log(parseDateString("DMY 21-10-1983"));
// Output: { day: 21, month: 10, year: 1983 }
console.log(parseDateString("MDY 03-15-2024"));
// Output: { day: 15, month: 3, year: 2024 }
console.log(parseDateString("DMY 15-03-2024"));
// Output: { day: 15, month: 3, year: 2024 }
Requirements:
day, month, and year propertiesIn this task, you will create a reusable JavaScript module that provides date conversion functions. You'll practice creating modules, exporting functions, and using them in other files.
Scenario:
You have been given an example.js file that already uses various date conversion functions. Your job is to create a date.js module that implements these functions so that example.js works correctly.
The example.js file (DO NOT MODIFY):
import {
convertHoursToMinutes,
convertMinutesToHours,
convertDaysToHours,
convertHoursToDays,
convertMinutesToSeconds,
convertSecondsToMinutes
} from './date.js';
console.log("=== Time Conversion Examples ===");
console.log("5 hours =", convertHoursToMinutes(5), "minutes");
console.log("120 minutes =", convertMinutesToHours(120), "hours");
console.log("3 days =", convertDaysToHours(3), "hours");
console.log("48 hours =", convertHoursToDays(48), "days");
console.log("10 minutes =", convertMinutesToSeconds(10), "seconds");
console.log("300 seconds =", convertSecondsToMinutes(300), "minutes");
Your Task:
Create a file called date.js that exports the following functions:
convertHoursToMinutes(hours) - converts hours to minutesconvertMinutesToHours(minutes) - converts minutes to hoursconvertDaysToHours(days) - converts days to hoursconvertHoursToDays(hours) - converts hours to daysconvertMinutesToSeconds(minutes) - converts minutes to secondsconvertSecondsToMinutes(seconds) - converts seconds to minutesRequirements:
date.js in the same directory as example.jsexample.js file - it should work as-is once your date.js is completeIn this task, you will be given code that works correctly but violates the DRY (Don't Repeat Yourself) principle. Your job is to refactor the code by extracting repeated logic into separate functions.
Starting Code:
// Temperature conversion and weather report for City 1
let cityName1 = "Amsterdam";
let tempCelsius1 = 22;
let tempFahrenheit1 = (tempCelsius1 * 9 / 5) + 32;
let tempKelvin1 = tempCelsius1 + 273.15;
console.log("Weather Report for " + cityName1);
console.log("Temperature: " + tempCelsius1 + "°C");
console.log("Temperature: " + tempFahrenheit1 + "°F");
console.log("Temperature: " + tempKelvin1 + "K");
if (tempCelsius1 < 0) {
console.log("Status: Freezing");
} else if (tempCelsius1 >= 0 && tempCelsius1 < 10) {
console.log("Status: Cold");
} else if (tempCelsius1 >= 10 && tempCelsius1 < 20) {
console.log("Status: Mild");
} else if (tempCelsius1 >= 20 && tempCelsius1 < 30) {
console.log("Status: Warm");
} else {
console.log("Status: Hot");
}
console.log("---");
// Temperature conversion and weather report for City 2
let cityName2 = "Berlin";
let tempCelsius2 = 15;
let tempFahrenheit2 = (tempCelsius2 * 9 / 5) + 32;
let tempKelvin2 = tempCelsius2 + 273.15;
console.log("Weather Report for " + cityName2);
console.log("Temperature: " + tempCelsius2 + "°C");
console.log("Temperature: " + tempFahrenheit2 + "°F");
console.log("Temperature: " + tempKelvin2 + "K");
if (tempCelsius2 < 0) {
console.log("Status: Freezing");
} else if (tempCelsius2 >= 0 && tempCelsius2 < 10) {
console.log("Status: Cold");
} else if (tempCelsius2 >= 10 && tempCelsius2 < 20) {
console.log("Status: Mild");
} else if (tempCelsius2 >= 20 && tempCelsius2 < 30) {
console.log("Status: Warm");
} else {
console.log("Status: Hot");
}
console.log("---");
// Temperature conversion and weather report for City 3
let cityName3 = "Copenhagen";
let tempCelsius3 = -5;
let tempFahrenheit3 = (tempCelsius3 * 9 / 5) + 32;
let tempKelvin3 = tempCelsius3 + 273.15;
console.log("Weather Report for " + cityName3);
console.log("Temperature: " + tempCelsius3 + "°C");
console.log("Temperature: " + tempFahrenheit3 + "°F");
console.log("Temperature: " + tempKelvin3 + "K");
if (tempCelsius3 < 0) {
console.log("Status: Freezing");
} else if (tempCelsius3 >= 0 && tempCelsius3 < 10) {
console.log("Status: Cold");
} else if (tempCelsius3 >= 10 && tempCelsius3 < 20) {
console.log("Status: Mild");
} else if (tempCelsius3 >= 20 && tempCelsius3 < 30) {
console.log("Status: Warm");
} else {
console.log("Status: Hot");
}
console.log("---");
// Wind chill calculation for City 1
let windSpeed1 = 15;
let windChill1 = 13.12 + 0.6215 * tempCelsius1 - 11.37 * Math.pow(windSpeed1, 0.16) + 0.3965 * tempCelsius1 * Math.pow(windSpeed1, 0.16);
console.log("Wind chill in " + cityName1 + ": " + windChill1.toFixed(2) + "°C");
// Wind chill calculation for City 2
let windSpeed2 = 20;
let windChill2 = 13.12 + 0.6215 * tempCelsius2 - 11.37 * Math.pow(windSpeed2, 0.16) + 0.3965 * tempCelsius2 * Math.pow(windSpeed2, 0.16);
console.log("Wind chill in " + cityName2 + ": " + windChill2.toFixed(2) + "°C");
// Wind chill calculation for City 3
let windSpeed3 = 25;
let windChill3 = 13.12 + 0.6215 * tempCelsius3 - 11.37 * Math.pow(windSpeed3, 0.16) + 0.3965 * tempCelsius3 * Math.pow(windSpeed3, 0.16);
console.log("Wind chill in " + cityName3 + ": " + windChill3.toFixed(2) + "°C");
Your Task:
Refactor this code by creating separate functions to eliminate repetition.
Requirements:
Bonus Challenge:
After refactoring, add weather reports for two more cities with different temperatures and wind speeds. Notice how much easier it is to generate new reports when following the DRY principle!
…
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **
