diff --git a/.gitignore b/.gitignore index bde36e530..16460d61c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules +.idea/ .DS_Store .vscode **/.DS_Store \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..004994ac7 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,29 @@ // Predict and explain first... // =============> write your prediction here +// +// I predict we will make mistake because str has been already declared in body of function // call the function capitalise with a string input + +//Uncaught SyntaxError: Identifier 'str' has already been declared + // interpret the error message and figure out why an error is occurring +//'str' has already been declared.Also we call function,defind it,but we don't use it. + function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } // =============> write your explanation here + +//So I change : I just return str directly ,add a function call and console.log(): + // =============> write your new code here + +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} + +console.log(capitalise("Anuar")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..dc588c140 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,10 @@ // Why will an error occur when this program runs? // =============> write your prediction here +/*SyntaxError: Identifier 'decimalNumber' has already been declared*/ +//Because same name is used again with const decimalNumber = 0.5; +// inside the function. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +20,16 @@ console.log(decimalNumber); // =============> write your explanation here +// console.log(decimalNumber); line is outside the function, +// so the variable decimalNumber is not defined in that scope. +// This will cause a ReferenceError. + // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(500)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..1346fd024 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,6 +1,6 @@ // Predict and explain first BEFORE you run any code... - +//We need to call function to use it, here will be an error! // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here @@ -10,11 +10,18 @@ function square(3) { } // =============> write the error message here +//Uncaught SyntaxError: Unexpected number // =============> explain this error message here +/*We get a syntax error because `3` is a number, + and function parameters must be valid variable names(x, num etc)*/ + // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} +console.log(square(3)); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..cccf3e8ee 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// -So,I guess the code won't work, because we print the result of multiplication twice function multiply(a, b) { console.log(a * b); @@ -9,6 +10,15 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +//-We need to remove console log in function and use instead of it return // 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..f0ff62db5 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +//I believe code won't work because return's value isn't assigned function sum(a, b) { return; a + b; @@ -9,5 +9,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +/* +The function returns undefined because return is followed by a line break. + JavaScript ends the return statement immediately, so the line a + b; is never executed. +To fix this, the expression must be placed on the same line as return. +*/ // Finally, correct the code to fix the problem // =============> write your new code here + +function sum(a, b) { + return a + b; +} diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..f7bed703a 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,8 +1,9 @@ // Predict and explain first... - // Predict the output of the following code: // =============> Write your prediction here +//-I think here is error in function ,in getLastDigit(.......) should be num in parentheses + const num = 103; function getLastDigit() { @@ -15,10 +16,36 @@ 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 +/* +So, I was almost right , +the num is stick to this number "103", and actually it it should be in parenthesis. +*/ + // Explain why the output is the way it is // =============> write your explanation here +/* +// =============> write your explanation here +The function `getLastDigit()` does not accept any parameters, + and always returns the last digit of the constant `num = 103`. +It completely ignores the numbers passed in (42, 105, 806) because it's not using them. +That's why the result is always the same — "3". + + The last digit of 42 is 3 +VM757:8 The last digit of 105 is 3 +VM757:9 The last digit of 806 is 3 +*/ + // 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..990895f8d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,14 @@ // It should return their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file + let bmi = weight / (height * height); + if (bmi <= 18.5) { + return "Underweight"; + } else if (bmi <= 25.0) { + return "Normal"; + } else if (bmi <= 30.0) { + return "Overweight"; + } else { + return "Obese"; + } +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..20d3458b5 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,8 @@ // 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 makeAllCaps(text) { + return text.toUpperCase().split(" ").join("_"); +} +console.log(makeAllCaps("Anuar")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..f9bc23a2f 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,22 @@ // 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(amount) { + const penceStringWithoutTrailingP = amount.substring(0, amount.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"); + console.log(`£${pounds}.${pence}`); +} + +toPounds("500p"); +toPounds("300p"); +toPounds("1000p"); diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..6785ddef8 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -19,16 +19,26 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +//3 times — once for hours, once for minutes, once for seconds. + // 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? +// b) What is the value assigned to num when pad is called for the first time? // =============> write your answer here +// 0 'totalHours'; + // 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 — this is the value of remainingSeconds, because 61 % 60 = 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" — because 1 is converted to "1", and padStart adds one leading 0 → "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..10f897312 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,11 +1,21 @@ // This is the latest solution to the problem from the prep. // Make sure to do the prep before you do the coursework -// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. +// Your task is to write tests for as many different groups of input data or edge cases as you can, +// and fix any bugs you find. function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + if (hours === 0) { + return `12:${minutes} am`; + } + + if (hours === 12) { + return `12:${minutes} pm`; + } + if (hours > 12) { - return `${hours - 12}:00 pm`; + const newHours = String(hours - 12).padStart(2, "0"); + return `${newHours}:${minutes} pm`; } return `${time} am`; }