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
10 changes: 9 additions & 1 deletion task-1/book.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function isBookApplicable(searchString) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bonus: what if someone callsisBookApplicable("") or isBookApplicable(" ")?

In JavaScript, "anything".includes("") evaluates to true, so this can be a nice real word edge case

// Your code here
const title = "The fundamentals of JavaScript";
const cleanSearch = searchString.trim().toLowerCase();
return title.toLowerCase().includes(cleanSearch);
}

console.log(isBookApplicable("javascript"));
console.log(isBookApplicable("python"));
console.log(isBookApplicable("JavaScript"));
console.log(isBookApplicable("JAVASCRIPT"));
console.log(isBookApplicable(" javascript "));
30 changes: 29 additions & 1 deletion task-2/parse-date.js
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
// Your code here
function parseDateString(dateString) {
const format = dateString.substring(0, 3);
const first = dateString.substring(4, 6);
const second = dateString.substring(7, 9);
const year = dateString.substring(10, 14);
Comment on lines +2 to +5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Because this splices the string based on specific positions, this works as long as the input string follows the expected pattern.

For instance: MDY 3-5-2025. The lack of a trailing 0 in the day will break the substring splice and lead to incorrect results. A possible way to make this more resilient would be to use .split(" ") to separate format from date, and then `.split("-") to separate the individual date components.



if (format === "MDY") {
return {
day: Number(second),
month: Number(first),
year: Number(year),
};
}
if (format === "DMY") {
return {
day: Number(first),
month: Number(second),
year: Number(year),
};
}
}
console.log(
parseDateString("MDY 03-15-2025")
);

console.log(
parseDateString("DMY 15-03-2025")
);
23 changes: 23 additions & 0 deletions task-3/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;
}
116 changes: 73 additions & 43 deletions task-4/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,110 @@
// 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) {
function convertCelsiusToFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}

function convertCelsiusToKelvin(celsius) {
return celsius + 273.15;
}

function calculateWindChill(temperature, windSpeed) {
return 13.12 + 0.6215 * temperature - 11.37 * Math.pow(windSpeed, 0.16) + 0.3965 * temperature * Math.pow(windSpeed, 0.16);
}

function printWeatherStatus(celsius) {
if (celsius < 0) {
console.log("Status: Freezing");
} else if (tempCelsius1 >= 0 && tempCelsius1 < 10) {
} else if (celsius >= 0 && celsius < 10) {
console.log("Status: Cold");
} else if (tempCelsius1 >= 10 && tempCelsius1 < 20) {
} else if (celsius >= 10 && celsius < 20) {
console.log("Status: Mild");
} else if (tempCelsius1 >= 20 && tempCelsius1 < 30) {
} else if (celsius >= 20 && celsius < 30) {
console.log("Status: Warm");
} else {
console.log("Status: Hot");
}

}
// Temperature conversion and weather report for City 1
let cityName1 = "Amsterdam";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bonus: in Javscript, you can declare variables that are mutable via let or immutable via const. In general, if you don't need/expect it to mutate something (like the city name here) it is considered best practice to use const. That way you've that guarantee and it is clearer at a glance that it is a value that shouldn't mutate.

let tempCelsius1 = 22;
let tempFahrenheit1 = convertCelsiusToFahrenheit(tempCelsius1);
let tempKelvin1 = convertCelsiusToKelvin(tempCelsius1);
console.log("Weather Report for " + cityName1);
console.log("Temperature: " + tempCelsius1 + "°C");
console.log("Temperature: " + tempFahrenheit1 + "°F");
console.log("Temperature: " + tempKelvin1 + "K");
Comment on lines +32 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bonus: this print block is repeated for 5 times, once per city/location. This is a situation where we could apply also apply the DRY principle by wrapping this into a reusable function!

printWeatherStatus(tempCelsius1);
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;
let tempFahrenheit2 = convertCelsiusToFahrenheit(tempCelsius2);
let tempKelvin2 = convertCelsiusToKelvin(tempCelsius2);
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");
}
printWeatherStatus(tempCelsius2);
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;
let tempFahrenheit3 = convertCelsiusToFahrenheit(tempCelsius3);
let tempKelvin3 = convertCelsiusToKelvin(tempCelsius3);
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");
}
printWeatherStatus(tempCelsius3);
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);
let windChill1 = calculateWindChill(tempCelsius1, windSpeed1);
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);
let windChill2 = calculateWindChill(tempCelsius2, windSpeed2);
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");
let windChill3 = calculateWindChill(tempCelsius3, windSpeed3);
console.log("Wind chill in " + cityName3 + ": " + windChill3.toFixed(2) + "°C");

let cityName4 = "Zurich";
let tempCelsius4 = 4;
let tempFahrenheit4 = convertCelsiusToFahrenheit(tempCelsius4);
let tempKelvin4 = convertCelsiusToKelvin(tempCelsius4);

console.log("Weather Report for " + cityName4);
console.log("Temperature: " + tempCelsius4 + "°C");
console.log("Temperature: " + tempFahrenheit4 + "°F");
console.log("Temperature: " + tempKelvin4 + "K");
printWeatherStatus(tempCelsius4);
console.log("---");

// Wind chill calculation for City 4
let windSpeed4 = 12;
let windChill4 = calculateWindChill(tempCelsius4, windSpeed4);
console.log("Wind chill in " + cityName4 + ": " + windChill4.toFixed(2) + "°C");

let cityName5 = "Bali";
let tempCelsius5 = 31;
let tempFahrenheit5 = convertCelsiusToFahrenheit(tempCelsius5);
let tempKelvin5 = convertCelsiusToKelvin(tempCelsius5);

console.log("Weather Report for " + cityName5);
console.log("Temperature: " + tempCelsius5 + "°C");
console.log("Temperature: " + tempFahrenheit5 + "°F");
console.log("Temperature: " + tempKelvin5 + "K");
printWeatherStatus(tempCelsius5);
console.log("---");

// Wind chill calculation for City 5
let windSpeed5 = 10;
let windChill5 = calculateWindChill(tempCelsius5, windSpeed5);
console.log("Wind chill in " + cityName5 + ": " + windChill5.toFixed(2) + "°C");