diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b6..e95e5234e 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-1/exercises/count.js @@ -1,6 +1,6 @@ let count = 0; -count = count + 1; +count = count + 1; //This line will increase count variable by 1 and "=" sign is used to assign the new value to the variable // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce2..cccd8d157 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -2,8 +2,10 @@ const num = 56.5678; // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math -// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) -// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num ) -// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) +wholeNumberPart = Math.floor(num); // Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) +decimalPart = +(num % 1).toFixed(10);// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num ) +roundedNum = Math.round(num);// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) +answers = [wholeNumberPart, decimalPart, roundedNum]; // Log your variables to the console to check your answers +console.log(answers) \ No newline at end of file diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd137..d21a35296 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -1,6 +1,7 @@ let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; - +initials = [firstName[0], middleName[0], lastName[0]]; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. +console.log(initials); \ No newline at end of file diff --git a/Sprint-1/exercises/paths.js b/Sprint-1/exercises/paths.js index c91cd2ab3..aab3d8982 100644 --- a/Sprint-1/exercises/paths.js +++ b/Sprint-1/exercises/paths.js @@ -15,4 +15,12 @@ const base = filePath.slice(lastSlashIndex + 1); console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable -// Create a variable to store the ext part of the variable +// Extract the `dir` part (everything before the last slash) +const dir = filePath.slice(0, lastSlashIndex); + +// Find the last dot index in the `base` to extract the `ext` part +const lastDotIndex = base.lastIndexOf("."); +const ext = base.slice(lastDotIndex); // Includes the dot +// Create a variable to store the ext part of the variables +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); \ No newline at end of file diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aab..517cf7176 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -7,3 +7,22 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing +console.log(num); + +//Math.random(): +// Produces a random floating-point number between 0 (inclusive) and 1 (exclusive). +// Example output: 0.234, 0.879, etc. +// Math.random() * (maximum - minimum + 1): + +// Scales the random number to a range from 0 to (maximum - minimum + 1). +// For example, if minimum = 1 and maximum = 100, the range becomes 0 to 100. +// Math.floor(): + +// Removes the decimal part of the scaled random number, effectively flooring it to the nearest integer. +// This ensures the result is an integer. +// + minimum: + +// Shifts the range from 0 to (maximum - minimum + 1) to minimum to maximum. +// This adds the minimum value to the result, ensuring the random number is within the desired range. +// Why (maximum - minimum + 1)? +// Adding 1 ensures that the upper bound (maximum) is included in the range. Without the +1, the random number would only go up to maximum - 1. \ No newline at end of file diff --git a/Sprint-1/interpret/to-pounds.js b/Sprint-1/interpret/to-pounds.js index 60c9ace69..4e5908018 100644 --- a/Sprint-1/interpret/to-pounds.js +++ b/Sprint-1/interpret/to-pounds.js @@ -25,3 +25,45 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + + +// const penceString = "399p";: + +// Initializes a string variable with the value "399p". +// This represents a monetary amount in pence, with the trailing p indicating pence. +// const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);: + +// Removes the trailing "p" from the string. +// Uses the substring() method to extract all characters from the start (0) to the second-to-last character (penceString.length - 1). +// Result: "399". +// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");: + +// Ensures the pence string has at least three characters by padding the start with 0 if necessary. +// Example: +// If penceStringWithoutTrailingP were "5", this would make it "005". +// If it's "399", it remains "399" as it already has three characters. +// Result: "399". +// const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);: + +// Extracts the pound portion from the padded pence string. +// Takes all characters from the start (0) up to the third-to-last character (paddedPenceNumberString.length - 2). +// Example: +// If paddedPenceNumberString is "399", this extracts "3" (the pound portion). +// Result: "3". +// const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: + +// Extracts the pence portion from the padded pence string. +// Takes the last two characters (paddedPenceNumberString.length - 2 onward). +// If the extracted string is less than two characters, padEnd(2, "0") ensures it has exactly two characters by appending 0 if needed. +// Example: +// If paddedPenceNumberString is "399", this extracts "99". +// If it were "005", this extracts "05". +// Result: "99". +// console.log(£${pounds}.${pence});: + +// Constructs a formatted string representing the amount in pounds and pence. +// Uses template literals to combine the pounds and pence values into the format £X.XX. +// Example: +// For pounds = "3" and pence = "99", the output is "£3.99". +// Purpose of the Program: +// The program converts a price given as a string in pence (e.g., "399p") into a formatted string representing pounds and pence (e.g., "£3.99"). This is useful for presenting prices in a human-readable currency format. diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d48..d6177abd7 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -13,3 +13,17 @@ // 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 calculateBMI(weight, height) { + // Calculate BMI + const bmi = weight / (height ** 2); + + // Round to 1 decimal place + return +bmi.toFixed(1); + } + + // Example usage + const weight = 70; // in kilograms + const height = 1.73; // in metres + const bmi = calculateBMI(weight, height); + + console.log(`Your BMI is ${bmi}`); // Outputs: Your BMI is 23.4 \ No newline at end of file diff --git a/Sprint-2/implement/cases.js b/Sprint-2/implement/cases.js index 9e56a27b6..00b80b18b 100644 --- a/Sprint-2/implement/cases.js +++ b/Sprint-2/implement/cases.js @@ -13,3 +13,11 @@ // 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(input) { + // Replace spaces with underscores and convert to uppercase + return input.replace(/ /g, "_").toUpperCase(); + } + + // Example usage + console.log(toUpperSnakeCase("hello there")); // Outputs: HELLO_THERE + console.log(toUpperSnakeCase("lord of the rings")); // Outputs: LORD_OF_THE_RINGS \ No newline at end of file diff --git a/Sprint-2/implement/to-pounds.js b/Sprint-2/implement/to-pounds.js index 6265a1a70..ab88190d3 100644 --- a/Sprint-2/implement/to-pounds.js +++ b/Sprint-2/implement/to-pounds.js @@ -4,3 +4,28 @@ // 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 + +function toPounds(penceString) { + // Remove the trailing "p" from the input + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + + // Ensure the pence string has at least three digits by padding + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Extract the pounds part (everything except the last two digits) + const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); + + // Extract the pence part (the last two digits) and ensure it's two characters + const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); + + // Return the formatted string + return `£${pounds}.${pence}`; + } + + // Test cases + console.log(toPounds("399p")); // Expected: £3.99 + console.log(toPounds("5p")); // Expected: £0.05 + console.log(toPounds("105p")); // Expected: £1.05 + console.log(toPounds("1200p")); // Expected: £12.00 + console.log(toPounds("0p")); // Expected: £0.00 + \ No newline at end of file diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb167226..8a93e6cff 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,18 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on + +function calculatePriceWithVAT(price) { + // Multiply the price by 1.2 to include VAT + const priceWithVAT = price * 1.2; + + // Return the result rounded to 2 decimal places + return +priceWithVAT.toFixed(2); + } + + // Example usage + console.log(calculatePriceWithVAT(50)); // Expected: 60.00 + console.log(calculatePriceWithVAT(100)); // Expected: 120.00 + console.log(calculatePriceWithVAT(0)); // Expected: 0.00 + console.log(calculatePriceWithVAT(19.99)); // Expected: 23.99 + \ No newline at end of file