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 3 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.

<aside> ❗

Do not modify example.js

</aside>

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.

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 assignment repository can be found in https://github.com/HackYourAssignment/ with the name cXX-core-week-## (Replace XX with your cohort number and ## with the week number)

Follow the Assignment submission guide to learn how to submit the assignment