Week 3 - Functions and strings

Functions

String functions

JavaScript Modules

Scope

IDE navigation

DRY (Don’t Repeat Yourself)

Function size

Using LLMs for efficient learning

Practice

Assignment

Back to core program

Week 2 Assignment

Task 1

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:

Example 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

Task 2

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:

The 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:

Example 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:

Task 3 - Create a Date Library Module

In 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:

Requirements:

Task 4 - Clean up code to follow DRY principle

In 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!

Submission


The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **

CC BY-NC-SA 4.0 Icons

*https://hackyourfuture.net/*