From 76f8ae3408b931a6c71e1a66e8f9f80cb538a314 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 14 Nov 2023 21:39:49 +0000 Subject: [PATCH 01/10] answer the questions and fixed the errors --- week-1/errors/0.js | 4 ++-- week-1/errors/1.js | 3 ++- week-1/errors/2.js | 2 +- week-1/errors/3.js | 3 ++- week-1/errors/4.js | 6 ++++-- week-1/exercises/count.js | 2 ++ week-1/exercises/decimal.js | 8 +++++++- week-1/exercises/initials.js | 2 ++ week-1/exercises/paths.js | 4 ++++ week-1/exercises/random.js | 1 + week-1/interpret/percentage-change.js | 4 ++++ 11 files changed, 31 insertions(+), 8 deletions(-) diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..e9c7b03f 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,2 +1,2 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +/*This is just an instruction for the first activity - but it is just for human consumption +We don't want the computer to run these 2 lines - how can we solve this problem?*/ diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..e57b2e97 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,5 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..2865eb8a 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,5 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..4c5c3271 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,5 +1,6 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..65bbbdd9 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,4 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..c16718b2 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -4,3 +4,5 @@ count = count + 1; // 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 +// ans ; it will reassign the count variable +console.log(count); diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..31ffae57 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -1,4 +1,3 @@ - const num = 56.5467; // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math @@ -8,3 +7,10 @@ const num = 56.5467; // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) // Log your variables to the console to check your answers +const wholeNumberPart = Math.floor(num); +const decimalPart = num - wholeNumberPart; +const roundedNum = Math.round(num); + +console.log(wholeNumberPart); +console.log(decimalPart); +console.log(roundedNum); diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..3791bc5d 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -4,3 +4,5 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials // Log the variable in each case +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); +console.log(initials); diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..bf238eb0 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -15,4 +15,8 @@ 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 +const dir = filePath.slice(filePath.indexOf("/"), filePath.lastIndexOf("/")); +console.log(dir); // Create a variable to store the ext part of the variable +const ext = filePath.slice(filePath.lastIndexOf(".")); +console.log(ext); diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..1d89c685 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -7,3 +7,4 @@ 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 several times to build an idea of what the program is doing +console.log(num); diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..9a5c55fb 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -12,9 +12,13 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +There are 2 function calls + Number + replaceAll // b) Identify all the lines that are variable reassignment statements + // c) Identify all the lines that are variable declarations // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? From 29d74dff8e015110f32db375dd7509c4804cea91 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 21 Nov 2023 20:23:45 +0000 Subject: [PATCH 02/10] debug --- week-2/debug/0.js | 4 ++++ week-2/debug/1.js | 5 +++-- week-2/debug/2.js | 12 +++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..c337ca34 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -5,3 +5,7 @@ function multiply(a, b) { } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +The first line of the function logs the product of the two arguments to the console. The second line of the code calls the multiply() function with the arguments 10 and 32, and then logs the result to the console. The expected output is: + +320 \ No newline at end of file diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..880516bf 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,9 @@ // Predict and explain first... function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +the error here that we should place the a + b expression directly after the return keyword. \ No newline at end of file diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..9f31b8fe 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,14 +1,12 @@ -// Predict and explain first... - +// Predict and explain first.. +// This program should tell the user the last digit of each number. +// Explain why getLastDigit is not working properly - correct the problem const num = 103; -function getLastDigit() { - return num.toString().slice(-1); +function getLastDigit(num) { + return num % 10; } 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 From f92e05c3b444784390c61fad2818326b7f6ebd1d Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 30 Nov 2023 18:54:14 +0000 Subject: [PATCH 03/10] 1.js errors --- week-2/errors/0.js | 3 ++- week-2/errors/1.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/week-2/errors/0.js b/week-2/errors/0.js index 58b1349d..076a6a00 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -4,6 +4,7 @@ // interpret the error message and figure out why it's happening, if your prediction was wrong function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("hello")); diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..1731afad 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -2,12 +2,14 @@ // Write down the error you predict will be raised // Why will an error occur when this program runs? // Play computer with the example to work out what is going on +//In this code, there is an error that the variable decimalNumber, declared twice. once as a parameter of the convertToPercentage function and again outside the function, because in JS variables can't be declared twice within the same scope +// to fix this error, we need to delete the parameter in line 8, so we can call the function without any argument. -function convertToPercentage(decimalNumber) { +function convertToPercentage() { const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage()); From 67e4d82d99f33d4219969ea8d1e68419d9591aa9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 30 Nov 2023 19:06:39 +0000 Subject: [PATCH 04/10] errors 2.js --- week-2/errors/2.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..efbeeb1f 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -1,10 +1,11 @@ - // Predict and explain first... // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? +// the error here is the (num) variable is not defined within the square function. +// to fix this error, we need to define the (num) variable within the square function, like this: -function square(3) { - return num * num; +function square(num) { + return num * num; } - +console.log(square(3)); From 4d46444d7657c156f63a09516afb38b53144b7f3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 30 Nov 2023 21:44:42 +0000 Subject: [PATCH 05/10] BMI.js --- week-2/implement/bmi.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..ecac9983 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -13,3 +13,14 @@ // Given someone's weight in kg and height in metres // When we call this function with the weight and height // Then it returns their Body Mass Index to 1 decimal place + +function calculateBMI(weight, height) { + const BMI = weight / (height * height); + return BMI.toFixed(1); + +} +const weight = 70; //weight in Kg +const height = 1.73; // height in m + +const BMIResult = calculateBMI(weight, height); +console.log("BMI:", BMIResult); \ No newline at end of file From f5c89748767c4950a1641d1c67997d1b957f4530 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Dec 2023 13:59:59 +0000 Subject: [PATCH 06/10] cases.js --- week-2/implement/cases.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index 56b0d8d0..c6931fa7 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -10,8 +10,17 @@ // Then it returns the string in UPPER_CAMEL_CASE, so "HELLO_THERE" // Test case: we expect "lord of the rings" to be "LORD_OF_THE_RINGS" -// Test case: we expect "the great gatsby" to be "THE_GREAT_GATSBY" -// Test case: we expect "the da vinci code" to be "THE_DA_VINCI_CODE" - // Come up with a clear, simple name for the function // Use the string documentation to help you plan your solution + +function camelCaseToWords(text) { + const result = text.replaceAll(/\s/g, "_"); + return result.toUpperCase(); +} +const text1 = "lord of the rings"; +const text2 = "the great gatsby"; +const text3 = "the great gatsby"; + +console.log(camelCaseToWords(text1)); +console.log(camelCaseToWords(text2)); +console.log(camelCaseToWords(text3)); From d2886022fbfbfb481ead192ccd2b84cb4a1663a2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Dec 2023 14:10:27 +0000 Subject: [PATCH 07/10] to-pounds.js --- week-1/interpret/to-pounds.js | 4 +--- week-2/implement/to-pounds.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..928cc3d1 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -11,9 +11,7 @@ const pounds = paddedPenceNumberString.substring( paddedPenceNumberString.length - 2 ); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); +const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2) .padEnd(2, "0"); console.log(`£${pounds}.${pence}`); diff --git a/week-2/implement/to-pounds.js b/week-2/implement/to-pounds.js index 7add3d05..1679109d 100644 --- a/week-2/implement/to-pounds.js +++ b/week-2/implement/to-pounds.js @@ -3,3 +3,14 @@ // Take this code and turn it into a reusable block of code. // Declare a function called toPounds with an appropriately named parameter. // Call this function a number of times to check it works for different inputs + +function toPounds(penceString) { + 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("499p")); // we expect the output: £4.99 +console.log(toPounds("123p")); // we expect the output: £1.23 +console.log(toPounds("5p")); // we expect the output: £0.05 \ No newline at end of file From 3c74cfe406fe47429a30be0ac5990e21a74b6e1a Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Dec 2023 14:39:11 +0000 Subject: [PATCH 08/10] vat.js --- week-2/implement/vat.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..0dc2ce5f 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -8,3 +8,11 @@ // Given a number, // When I call this function with a number // Then it returns the new price with VAT added on + +function calculatePriceWithVAT(price) { + price = price * 1.2; + + return price; +} + +console.log(calculatePriceWithVAT(50)); From 4790e5f25fa5241619373ff58561b5fb79197c4b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Dec 2023 15:09:42 +0000 Subject: [PATCH 09/10] time-format.js --- week-2/interpret/time-format.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..9187e783 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -26,18 +26,29 @@ console.log(formatTimeDisplay(143)); // Questions // a) When formatTimeDisplay is called how many times will pad be called? +// Here The pad function is called three times. // Call formatTimeDisplay with an input of 143, now answer the following: // b) What value is assigned to the parameter num when pad is called for the first time? +// When formatTimeDisplay is called with an input of 143, the value will be assigned to the parameter num when pad is called for the first time is 2. // c) What is the return value of pad when it is called for the first time? +//The return value of pad when it is called for the first time is 02 // d) What is the value assigned to the parameter num when pad // is called for the last time in this program? Explain your answer +//The value assigned to the parameter num when pad is called for the last time in this program is 00. +// because the remaining seconds is the remainder of the seconds after dividing by 60 // e) What is the return value when pad is called // for the last time in this program? Explain your answer +//The return value when pad is called for the last time in this program is 23. +//because the assigned value will return the num value itself // f) Research an alternative way of padding the numbers in this code. // Look up the string functions on mdn +//we can use padStart(2, '0') to pad the numbers to a length of two characters with the padding character '0'. like this: +//function pad(num) { + //return num.toString().padStart(2, '0'); +//} \ No newline at end of file From 150892bdb2b8c221e8ae0faec776d3c44ec4b549 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 1 Dec 2023 15:19:29 +0000 Subject: [PATCH 10/10] changes on the debug file --- week-2/debug/0.js | 4 ++-- week-2/debug/1.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/week-2/debug/0.js b/week-2/debug/0.js index c337ca34..28ccc51f 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -6,6 +6,6 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -The first line of the function logs the product of the two arguments to the console. The second line of the code calls the multiply() function with the arguments 10 and 32, and then logs the result to the console. The expected output is: +//The first line of the function logs the product of the two arguments to the console. The second line of the code calls the multiply() function with the arguments 10 and 32, and then logs the result to the console. The expected output is: -320 \ No newline at end of file +//320 \ No newline at end of file diff --git a/week-2/debug/1.js b/week-2/debug/1.js index 880516bf..9c08a61f 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -6,4 +6,4 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -the error here that we should place the a + b expression directly after the return keyword. \ No newline at end of file +//the error here that we should place the a + b expression directly after the return keyword. \ No newline at end of file