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
25 changes: 24 additions & 1 deletion task-1/book.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
function isBookApplicable(searchString) {
// Your code here
const title = "The fundamentals of JavaScript";
const inputText = normalizeText(searchString);
if (inputText.length === 0) return false;
const normalizedTitle = normalizeText(title);
return normalizedTitle.includes(inputText);
}

function normalizeText(s) {
return String(s).trim().toLowerCase().replace(/\s+/g, " ");
}

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
29 changes: 28 additions & 1 deletion task-2/parse-date.js
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
// Your code here
function parseDateString(dateString) {
const [format, datePart] = String(dateString).trim().split(/\s+/);
const numbers = datePart.split("-").map(Number);

let day, month, year;

if (format === "MDY") {
[month, day, year] = numbers; // Month-Day-Year
} else if (format === "DMY") {
[day, month, year] = numbers; // Day-Month-Year
} else {
throw new Error("Unknown date format. Expected 'MDY' or 'DMY'.");

Choose a reason for hiding this comment

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

When you deliberately throw an error you should also handle the error somewhere else in your code. Otherwise the error is passed on to the host environment, in this case Node.js, which terminates your program with a stack trace, like this:

Error: Unknown date format. Expected 'MDY' or 'DMY'.
    at parseDateString (/Volumes/Crucial2TB/xdev/hackyourfuture/c55-core-week-3/task-2/parse-date.js:12:11)
    at Object.<anonymous> (/Volumes/Crucial2TB/xdev/hackyourfuture/c55-core-week-3/task-2/parse-date.js:17:13)
    at Module._compile (node:internal/modules/cjs/loader:1761:14)
    at Object..js (node:internal/modules/cjs/loader:1893:10)
    at Module.load (node:internal/modules/cjs/loader:1481:32)
    at Module._load (node:internal/modules/cjs/loader:1300:12)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47

Error handling is covered in week 6, so don't worry about it now.

}

return { day, month, 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 }

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;
}

Choose a reason for hiding this comment

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

👍

126 changes: 53 additions & 73 deletions task-4/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,60 @@
// 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");
// Converts a temperature from Celsius (°C) to Fahrenheit (°F).
function celsiusToFahrenheit(c) {
return (c * 9) / 5 + 32;
}
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");
// Converts a temperature from Celsius (°C) to Kelvin (K).
function celsiusToKelvin(c) {
return c + 273.15;
}
// Returns a human-readable weather status string based on the Celsius temperature.
function getTemperatureStatus(c) {
if (c < 0) return "Freezing";
if (c >= 0 && c < 10) return "Cold";
if (c >= 10 && c < 20) return "Mild";
if (c >= 20 && c < 30) return "Warm";
return "Hot";
}
// Calculates the wind chill (feels-like temperature) in Celsius using the given air temperature (tempCelsius) and wind speed (windSpeed).
function calculateWindChill(tempCelsius, windSpeed) {
return (
13.12 +
0.6215 * tempCelsius -
11.37 * Math.pow(windSpeed, 0.16) +
0.3965 * tempCelsius * Math.pow(windSpeed, 0.16)
);
}
console.log("---");
// Prints a weather report for a single city:
function printWeatherReport(cityName, tempCelsius) {
const tempF = celsiusToFahrenheit(tempCelsius);
const tempK = celsiusToKelvin(tempCelsius);

// 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("Weather Report for " + cityName);
console.log("Temperature: " + tempCelsius + "°C");
console.log("Temperature: " + tempF + "°F");
console.log("Temperature: " + tempK + "K");
console.log("Status: " + getTemperatureStatus(tempCelsius));
console.log("---");
}
// Prints the wind chill for a single city.
function printWindChill(cityName, tempCelsius, windSpeed) {
const windChill = calculateWindChill(tempCelsius, windSpeed);
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");
const cities = [
{ name: "Amsterdam", tempC: 22, wind: 15 },
{ name: "Berlin", tempC: 15, wind: 20 },
{ name: "Copenhagen", tempC: -5, wind: 25 },
{ name: "Lviv", tempC: 2, wind: 10 },
{ name: "Strasbourg", tempC: 6, wind: 10 },
];

// 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");
for (const c of cities) {
printWeatherReport(c.name, c.tempC);
}

// 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");
for (const c of cities) {
printWindChill(c.name, c.tempC, c.wind);
}

Choose a reason for hiding this comment

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

This looks like a perfect cleanup of the original code. If this is completely your own work, well done. However, if some or all of it is done through genAI then please take note of the Code of Conduct that Peter posted in slack.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks! Just to clarify: I used a bit of AI only to translate parts of the assignment and for learning/explanations, not to generate the code. I wrote the solution myself, and I understand what each function and line is doing.