Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Millena mesfin/Sprint-2-exercices /structuring and testing data #221

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ function multiply(a, b) {
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// Line#3: A function named multiply is defined,takes two parameters a and b.
// Line#4: calling the multiply function.
// Line#7:The template literal (${multiply(10, 32)}) tries to include the result of multiply(10, 32) in the string.
// Result: Undefined
function multiply(a, b) {return a * b;
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// The multiply function returns the result.


6 changes: 6 additions & 0 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ function sum(a, b) {
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// Return a + b; // Put return expression on one line, otherwise the value will be undefined.

function sum(a, b) {
return a + b;
}
// Fixed the code
8 changes: 8 additions & 0 deletions Sprint-2/debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

// Line#3; it always uses number variable.
// Line#5; we need to make getLastDigit accept a parameter.

function getLastDigit(number) {
return num.toString().slice(-1);
Copy link

@cjyuan cjyuan Dec 11, 2024

Choose a reason for hiding this comment

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

Have you executed your script to verify your function can correctly return the last digit?

I don't think console.log( getLastDigit(806) ) will output 6 with your current implementation.

}
//Fixed the code
13 changes: 13 additions & 0 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

//The function capitalise accepts a parameter named str.

//In JavaScript, we cannot use let to declare a new variable with the same name as an existing parameter within the same scope.
//This will throw an error.

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
// I ommited let
return str;
}

// Fixed the code.
102 changes: 102 additions & 0 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,45 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5); // 〰️ Get the minute part of the time

// 〰️ Validate the input: check if hours and minutes are numbers and within valid ranges
if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
return null; // 〰️ Return null for invalid input
}

// 〰️ Handle midnight (00:00)
if (hours === 0) {
return `12:${minutes} am`;
}

// 〰️ Handle noon (12:00)
if (hours === 12) {
return `12:${minutes} pm`;
}

// 〰️ Handle PM times (hours greater than 12)
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${hours - 12}:${minutes} pm`;
}
return `${time} am`;
// 〰️ Handle AM times (hours less than 12)
return `${hours}:${minutes} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
//Test cases

// 〰️ Test a standard morning time (8:00 AM)
const currentOutput1 = formatAs12HourClock("08:00");
const targetOutput1 = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);

const currentOutput2 = formatAs12HourClock("23:00");
Expand All @@ -22,3 +50,77 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// 〰️ Additional test cases

// 〰️ Test midnight (12:00 AM)
const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

// 〰️ Test noon (12:00 PM)
const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

// 〰️ Test just after noon (12:30 PM)
const currentOutput5 = formatAs12HourClock("12:30");
const targetOutput5 = "12:30 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);

// 〰️ Test just after midnight (12:30 AM)
const currentOutput6 = formatAs12HourClock("00:30");
const targetOutput6 = "12:30 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);

// 〰️ Invalid input cases

// 〰️ Test an invalid time (hours out of range)
const currentOutput7 = formatAs12HourClock("24:00");
console.assert(
currentOutput7 === null,
`expected null but got: ${currentOutput7}`
);

// 〰️ Test an invalid time (minutes out of range)
const currentOutput8 = formatAs12HourClock("12:60");
console.assert(
currentOutput8 === null,
`expected null but got: ${currentOutput8}`
);

// 〰️ Test completely invalid input (non-numeric string)
const currentOutput9 = formatAs12HourClock("invalid");
console.assert(
currentOutput9 === null,
`expected null but got: ${currentOutput9}`
);

// 〰️ Test missing hour digit (invalid format)
const currentOutput10 = formatAs12HourClock("8:00");
console.assert(
currentOutput10 === null,
`expected null but got: ${currentOutput10}`
);

console.log(formatAs12HourClock("07:00"));
console.log(formatAs12HourClock("19:00"));
console.log(formatAs12HourClock("00:00"));
console.log(formatAs12HourClock("12:00"));
console.log(formatAs12HourClock("24:00"));
console.log(formatAs12HourClock("23:59"));
console.log(formatAs12HourClock("invalid"));
console.log(formatAs12HourClock("22:63"));

20 changes: 20 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place



function calculatedBMI(weight, height) {
// Square the height
const heightSquared = height * height;

// Divide weight by squared height
const bmi = weight / heightSquared;

// Return the BMI rounded to 1 decimal place
return parseFloat(bmi.toFixed(1));
}

console.log(calculatedBMI(70, 1.73)); // Outputs: 23.4

// First, we’re declaring a function named calculateBMI, which takes two parameters: weight (in kilograms) and height (in meters).
// Then we create a variable heightSquared and set it equal to the height multiplied by itself. This step squares the height, which is part of the BMI formula.
// We calculate the BMI by dividing the weight by heightSquared.
// The function will return 23.4 as the BMI, which is rounded to one decimal place, as required.
17 changes: 17 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution



function toUpperSnakeCase(inputString) {
// Convert the string to uppercase and replace spaces with underscores
return inputString.toUpperCase().replace(/ /gi, "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));


// inputString.toUpperCase() converts all letters to uppercase.
// .replace(/ /g, "_") uses a regular expression to replace all spaces in the string with underscores (_), where the /g flag ensures that all spaces are replaced (not just the first one).
// Using .replace(/ /g, "_") with the g flag tells JavaScript to replace all spaces in the string, not just the first one.
// /g: Replaces all occurrences of the pattern, but is case-sensitive.
// /gi: Replaces all occurrences of the pattern and is case-insensitive.
Copy link

Choose a reason for hiding this comment

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

You are using / /gi (instead of / /g) in your code. How does the i flag affect the result?

26 changes: 26 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,29 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs


// We need to encapsulate this logic into a reusable function called toPounds, which should take a penceString as a parameter and return a formatted pounds-and-pence string. Then, we can test this function with different inputs to ensure it works correctly.

function toPounds(penceString) {
// Step 1: Remove the trailing "p" from the string
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);

// Step 2: Pad the numeric part to ensure at least 3 digits
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Step 3: Extract the pounds and pence parts
const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");

// Step 4: Return the formatted pounds and pence string
return `£${pounds}.${pence}`;
}


// Testing the function with different inputs
console.log(toPounds("399p")); // Expected output: "£3.99"
console.log(toPounds("50p")); // Expected output: "£0.50"
console.log(toPounds("7p")); // Expected output: "£0.07"
console.log(toPounds("1000p")); // Expected output: "£10.00"
console.log(toPounds("12345p")); // Expected output: "£123.45"
16 changes: 16 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@
// Given a number,
// When I call this function with a number
// it returns the new price with VAT added on



// Define a function named addVAT that takes one parameter: price
function addVAT(price) {

// Step 1: Calculate the price with VAT by multiplying the price by 1.2
// (1.2 represents the original price + 20% VAT)
const priceWithVAT = price * 1.2;

// 〰️ Step 2: Return the new price with VAT included
return priceWithVAT;
}

console.log(addVAT(50)); // Outputs "60"
console.log(addVAT(80)); // Outputs "96"
7 changes: 5 additions & 2 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// pad will be called three times—once for each of totalHours, remainingMinutes, and remainingSeconds.

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?

// The first call to pad is for totalHours, which is 0. So, num is assigned the value 0 during the first call.
// c) What is the return value of pad is called for the first time?
// When pad(0) is called, it returns "00" because padStart(2, "0") ensures the output has two characters.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer

// The last call to pad is for remainingSeconds, which is 1. So, num is assigned the value 1 during the last call.
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// When pad(1) is called, it returns "01" because padStart(2, "0") pads the single digit 1 to make it two characters ("01").