diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..96a1bfee5 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -3,11 +3,18 @@ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring - -function capitalise(str) { +function capitalize(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } // =============> write your explanation here +// the function takes string arg it gets the first character of the string and capitalises it. the .slice method gets the rest of the string and concats the capitalized first char. all this is assigned to var called str and returns the str. +// the error says the var str has already been used in the function, this is because the var str is declared in the function and then again in the return statement. we could fix it by removing the var keyword in the return statement. // =============> write your new code here + +// function capitalize(str) { +// return `${str[0].toUpperCase()}${str.slice(1)}`; +// } + +console.log(capitalise('hello earth')); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..430de5a80 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,34 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// i think it gets a error cause the var decimalNumber is declared inside the function and used as the arg of the function. not sure of the error but it should throw a error // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +function convertToPercentage(decimalNumber) { + const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; - return percentage; + return percentage; } -console.log(decimalNumber); +console.log(decimalNumber); // =============> write your explanation here +//function declared with arg. takes decimal number +// this is the error, the var decimalNumber is declared inside the function and then used as the arg of the function. this is not allowed in js. +// the decimal number is converted to percentage by multiplying it by 100 and concating a % sign +// returns the percentage +// prints to console the decimal number // Finally, correct the code to fix the problem // =============> write your new code here +/* + const decimalNumber = 0.5; + + function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + +console.log(convertToPercentage(decimalNumber)); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..6603eb2f7 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,26 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// I predict that the error will be that the parameter 'num' is not defined because it is not declared before being used in the function. function square(3) { return num * num; } // =============> write the error message here - +// SyntaxError: Unexpected number // =============> explain this error message here +// This error message occurs because the function parameter is not defined correctly. In JavaScript, function parameters must be valid identifiers, and using a number directly (like 3) is not allowed. // Finally, correct the code to fix the problem // =============> write your new code here +/* +let num = 3; + +function square(num) { + return num * num; +} + */ diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..1909a3b4d 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,7 +1,9 @@ // Predict and explain first... - +// function is expected to multiply two numbers and log the result. it starts by taking in args 'a' and 'b', then it logs the product of 'a' and 'b'. +// then it calls the function with 10 and 32 and tries to log the result of the function call. +// However, the function does not return a value, it only logs the product. Therefore, // =============> write your prediction here - +// undefined function multiply(a, b) { console.log(a * b); } @@ -9,6 +11,14 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here - +// no return in the function so its always undefined // Finally, correct the code to fix the problem // =============> write your new code here +/* +function multiply(a, b) { + return a * b; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +*/ diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..25114ce5d 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// it wants to add to args 'a' and 'b' and log the result. in the function body it makes a return and closes then on a new line does the addition of 'a' and 'b'. +// get a error like undefined maybe. function sum(a, b) { return; @@ -9,5 +11,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// the function does not return the sum of 'a' and 'b', it just returns undefined because the return statement gets closed and is not followed by any value. // Finally, correct the code to fix the problem // =============> write your new code here +/* +function sum(a,b) { +return a + b; +} +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +*/ diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..815f87340 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,7 @@ // Predict the output of the following code: // =============> Write your prediction here - +// output: last degit of 42 is undefined. the function is not taking in any arguments. num is has hardcoded. the function currently returns the last digit of num which is 103. const num = 103; function getLastDigit() { @@ -15,10 +15,24 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction // =============> write the output here +/* $ node 2.js +The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3 +*/ // Explain why the output is the way it is // =============> write your explanation here +// output is always 3 cause num is hardcoded to 103. the function does not take in any args, so it always returns the last digit of 103 // Finally, correct the code to fix the problem // =============> write your new code here +/* +function getLastDigit(num) { + return num.toString().slice(-1); +} + console.log(`The last digit of 42 is ${getLastDigit(42)}`); + console.log(`The last digit of 105 is ${getLastDigit(105)}`); + 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 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..c5a2eb300 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -1,5 +1,7 @@ // Below are the steps for how BMI is calculated +const { createTestScheduler } = require("jest"); + // The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. // For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: @@ -16,4 +18,29 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = weight / (height * height); + const value= bmi.toFixed(1); + + let category; + if (value < 18.5) category = "Underweight"; + else if (value < 25) category = "Healthy"; + else if (value < 30) category = "Overweight"; + else catergory = "Please get in touch with your doctor"; + + return { value, category }; +} + +//testing testing 1 2 +// console.log(`My BMI is ${calculateBMI(65.8, 1.69)}`); //DV +// console.log(`My BMI is ${calculateBMI(58.0, 1.73)}`); //TMKC +// console.log(`My BMI is ${calculateBMI(22.5, 1.22)}`); // SV +const tests = [ + { w: 65.8, h: 1.69 }, //DV + { w: 58.0, h: 1.73 }, // TMKC + { w: 22.5, h: 1.22 } // SV according to the calc our kids underweight but the chubbiest of us all +]; + +tests.forEach(({w, h}) => { + const { value, category } = calculateBMI(w, h); + console.log( `BMI ${value}, category: ${category}`); +}); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..56e461520 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // You will need to come up with an appropriate name for the function // Use the MDN string documentation to help you find a solution // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase +function toUpperSnakeCase(input) { + // split the words by spaces + const words = input.split(' '); + // map each of the words to uppercase and join them with underscores + const upperSnakeCase = words.map(word => word.toUpperCase()).join('_'); + return upperSnakeCase; +} + +//test +console.log(toUpperSnakeCase("why do people run")); // "WHY_DO_PEOPLE_RUN" \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..39c557d7e 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-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) { +// This program takes a string representing a price in pence + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + +return`£${pounds}.${pence}`; +} +console.log(toPounds("123p")); // "£1.23" +console.log(toPounds("4567p")); // "£45.67" +console.log(toPounds("89p")); // "£0.89" +console.log(toPounds("5p")); // "£0.05" \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..e58bc8b52 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,19 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here - +// 3 // 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? // =============> write your answer here - +// 0? // c) What is the return value of pad is called for the first time? // =============> write your answer here +// "00" // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here - +// 1 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer // =============> write your answer here +// "01"