Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion task-1/book.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function isBookApplicable(searchString) {
// Your code here
const bookTitle = 'The fundamentals of JavaScript';

// remove extra whitespace from searchString and turn into lowercase
const cleanSearchString = searchString.replace(/\s+/g,' ').trim().toLowerCase();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that you also handle potential extra space in between words.

But what if I provide an empty string? Or what if I call the isBookApplicable function without a parameter?

console.log(isBookApplicable(""));
console.log(isBookApplicable());

That's why, in software engineering, it's very important to first consider all the requirements and error cases that need to be handled ("Error Handling").

// turn bookTitle into lowercase
const cleanBookTitle = bookTitle.toLowerCase();

// return true or false based on availability of cleanSearchString in cleanBookTitle
return cleanBookTitle.includes(cleanSearchString);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You named variables very clearly. Well done!

}

console.log(isBookApplicable("javascript"));
console.log(isBookApplicable("javascript "));
console.log(isBookApplicable("python"))
console.log(isBookApplicable("JavaScript"));
console.log(isBookApplicable("JAVASCRIPT"));
38 changes: 37 additions & 1 deletion task-2/parse-date.js
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
// Your code here
function parseDateString(dateString) {
const dateObject = {}; // empty date object to return at the end

const dateStringSplit = dateString.split(" "); // split the dateString by space
const dateFormat = dateStringSplit[0]; // this is first part of splitted dateString: MDY or DMY
const dateFullString = dateStringSplit[1]; // this second part of splitted dateString: e.g. 10-21-1983
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice logic that you created clear variables. There is another way to do this using Destructuring. Sometimes, using destructuring yields cleaner, easier-to-understand code.

    const dateStringSplit = dateString.split(" ");
    const [dateFormat, dateFullString] = dateStringSplit;

const dateFullStringSplit = dateFullString.split("-"); // split dateFullString by dash

// add key:value pairs of day and month to the empty dateObject created earlier in this function
if(dateFormat === "MDY") {
dateObject.day = Number(dateFullStringSplit[1]);
dateObject.month = Number(dateFullStringSplit[0]);
} else if(dateFormat === "DMY") {
dateObject.day = Number(dateFullStringSplit[0]);
dateObject.month = Number(dateFullStringSplit[1]);
}


// add key:value pair of year to the dateObject created earlier in this function
dateObject.year = Number(dateFullStringSplit[2]);

return dateObject; // return the date object with key:value pairs of day, month, and year
}



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 }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You handled all the requirements, well done!

18 changes: 18 additions & 0 deletions task-3/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function convertHoursToMinutes(hours) {
return hours * 60;
}
export function convertMinutesToHours(minutes) {
return minutes / 60;
}
export function convertDaysToHours(days){
return days * 24;
}
export function convertHoursToDays(hours) {
return hours / 24;
}
export function convertMinutesToSeconds(minutes) {
return minutes * 60;
}
export function convertSecondsToMinutes(seconds) {
return seconds / 60;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

103 changes: 29 additions & 74 deletions task-4/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,35 @@
// 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("---");
function cityWeather(cityName, tempCelsius) {
const tempFahrenheit = (tempCelsius * 9 / 5) + 32;
const tempKelvin = tempCelsius + 273.15;
console.log("Weather Report for " + cityName);
console.log("Temperature: " + tempCelsius + "°C");
console.log("Temperature: " + tempFahrenheit + "°F");
console.log("Temperature: " + tempKelvin + "K");
if (tempCelsius < 0) {
console.log("Status: Freezing");
} else if (tempCelsius >= 0 && tempCelsius < 10) {
console.log("Status: Cold");
} else if (tempCelsius >= 10 && tempCelsius < 20) {
console.log("Status: Mild");
} else if (tempCelsius >= 20 && tempCelsius < 30) {
console.log("Status: Warm");
} else {
console.log("Status: Hot");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your logic is already good. But you could further improve it and make it easier to read. All these statements could be assigned to a variable. For example:

    const isFreezing = tempCelsius < 0;
    const isCold = tempCelsius >= 0 && tempCelsius < 10;
    const isMild = tempCelsius >= 10 && tempCelsius < 20;
    const isWarm = tempCelsius >= 20 && tempCelsius < 30;

    if (isFreezing) {
        console.log("Status: Freezing");
    } else if (isCold) {
        console.log("Status: Cold");
    } else if (isMild) {
        console.log("Status: Mild");
    } else if (isWarm) {
        console.log("Status: Warm");
    } else {
        console.log("Status: Hot");
    }


// 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("---");
}
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");
function windChillCalculation(cityName, tempCelsius, windSpeed) {
// Wind chill calculation
const windChill = 13.12 + 0.6215 * tempCelsius - 11.37 * Math.pow(windSpeed, 0.16) + 0.3965 * tempCelsius * Math.pow(windSpeed, 0.16);
console.log("Wind chill in " + cityName + ": " + windChill.toFixed(2) + "°C");
}
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");
cityWeather("Amsterdam", 22);
cityWeather("Berlin", 15);
cityWeather("Copenhagen", -5);

// 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");
windChillCalculation("Amsterdam", 22, 15);
windChillCalculation("Berlin", 15, 20);
windChillCalculation("Copenhagen", -5, 25);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You used clear variable names and clear function names. And you implemented reusable functions in accordance with the separation of concerns principle; each function is responsible for one thing. Well done!