diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..5ca380b14 --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 5ca380b141dfe60780a233a7d7c7a08485af2df9 diff --git a/Sprint-1/errors/0.js b/Sprint-1/errors/0.js index cf6c5039f..e828a21d0 100644 --- a/Sprint-1/errors/0.js +++ b/Sprint-1/errors/0.js @@ -1,2 +1,3 @@ -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? +// comments \ No newline at end of file diff --git a/Sprint-1/errors/1.js b/Sprint-1/errors/1.js index 7a43cbea7..bf4ef85cf 100644 --- a/Sprint-1/errors/1.js +++ b/Sprint-1/errors/1.js @@ -1,4 +1,10 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +// const age = 33; +// age = age + 1; + + + // the problem is the variable age is declared as const which mean we can not change it to fix this we change it from const to let + + let age = 33; + age = age +1; diff --git a/Sprint-1/errors/2.js b/Sprint-1/errors/2.js index e09b89831..dc334bc74 100644 --- a/Sprint-1/errors/2.js +++ b/Sprint-1/errors/2.js @@ -1,5 +1,10 @@ // 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}`); +// console.log(`I was born in ${cityOfBirth}`); +// const cityOfBirth = "Bolton"; + +// the problem is we cannot access city of birth before initialization, here is the correct one + const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); \ No newline at end of file diff --git a/Sprint-1/errors/3.js b/Sprint-1/errors/3.js index ec101884d..45e13f4ff 100644 --- a/Sprint-1/errors/3.js +++ b/Sprint-1/errors/3.js @@ -1,5 +1,5 @@ -const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +// const cardNumber = 4533787178994213; +// const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,11 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +// the code provided will produce an error cause the .slice() will only generate strings and arrays while the cardNumber is a number. +// to correct this i will introduce the .toString() to change it to a string, + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); +console.log(last4Digits); \ No newline at end of file diff --git a/Sprint-1/errors/4.js b/Sprint-1/errors/4.js index 21dad8c5d..3f3f798ba 100644 --- a/Sprint-1/errors/4.js +++ b/Sprint-1/errors/4.js @@ -1,2 +1,12 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// here the problem is a variable name in java script can not start with a number and also the reading in time 20:53 is in the 24 hr clock time + + +const twelveHourClockTime = "08:53"; +const twentyFourHourClockTime = "20:53"; + +console.log(twelveHourClockTime); +console.log(twentyFourHourClockTime); + diff --git a/Sprint-1/exercises/count.js b/Sprint-1/exercises/count.js index 117bcb2b6..58098bf5e 100644 --- a/Sprint-1/exercises/count.js +++ b/Sprint-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 +// in line 3 the code is adding 1 to the variable count, that is after the line 3 is executed the value of count will now be 1. +// the = operator is used to assign a value to a variable. diff --git a/Sprint-1/exercises/decimal.js b/Sprint-1/exercises/decimal.js index cc5947ce2..8136794f9 100644 --- a/Sprint-1/exercises/decimal.js +++ b/Sprint-1/exercises/decimal.js @@ -7,3 +7,7 @@ const num = 56.5678; // 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 = console.log(Math.floor(56.5678)); +const decimalPart = num - wholeNumberPart; +const roundedNum = console.log(Math.round(56.5678)); \ No newline at end of file diff --git a/Sprint-1/exercises/initials.js b/Sprint-1/exercises/initials.js index 6b80cd137..879de2322 100644 --- a/Sprint-1/exercises/initials.js +++ b/Sprint-1/exercises/initials.js @@ -4,3 +4,8 @@ let lastName = "Johnson"; // 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. + +// to select the first character of each variable i will use the charAt() also known as character at method which is used to extract a character at a specified index. +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); +// The console.log function in JavaScript is used to print (or log) messages to the console. +console.log(initials); diff --git a/Sprint-1/exercises/random.js b/Sprint-1/exercises/random.js index 292f83aab..8c369f109 100644 --- a/Sprint-1/exercises/random.js +++ b/Sprint-1/exercises/random.js @@ -7,3 +7,21 @@ 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 + +// answer +//here the num is any integer between the value of 1 to 100, to breakdown the code +// the function Math.random() calls any number between 0 to 0.9999. lets say 0.5575 + + +// and this Math.random() * (maximum - minimum + 1) calculates the random number we got by (maximum - minimum + 1), which can be (100-1+1) which will give us the result of 100. + + +// (0.5575 *100) = 55.75 + + +// the function Math.floor will round the result of the previous step down to the nearest whole number, giving a random integer between 0 and 99 which in this will be 55. + + +// finally add the minimum to the result + +console.log(num); diff --git a/Sprint-1/interpret/percentage-change.js b/Sprint-1/interpret/percentage-change.js index e24ecb8e1..1144df2a7 100644 --- a/Sprint-1/interpret/percentage-change.js +++ b/Sprint-1/interpret/percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -20,3 +20,20 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +//answers + +// a) +//on line 4, carPrice.replaceAll(",", ""), replaceAll is called on carPrice to remove the commas. +// on line 4, Number is called to replace the results of replaceAll to a number, cause they were strings. +// on line 5, priceAfterOneYear.replaceAll("," ""), replaceAll is called on priceAfterOneYear to remove thee comas. +// on line 5, Number is called to replace the results of replaceAll to a number, cause they were strings. +// on line 10, console.log(`The percentage change is ${percentageChange}`);, console.log is called to print the percentage. + +// b) the error is in line 5, priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); a comma is missing between the two strings, the correct one is priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); + +// c) in line 4 and 5 the variables carPrice and priceAfterOneYear are replaced to a new values. first they are both strings with commas now they are numbers. + +// d) in javascript let and const are used to declare variables, so in the above code we have 4 declared variables: carPrice, priceAfterOneYear, priceDifference and percentageChange. + +// e) Number(carPrice.replaceAll(",","")): lets break it down. first the expression replaceAll(",","") is removing the comma (it was "10,000", now it is "10000"). and the second expression Number(carPrice.replaceAll(",","")) is changing the string to number(it was "10000" now it is 10000 ) \ No newline at end of file diff --git a/Sprint-1/interpret/time-format.js b/Sprint-1/interpret/time-format.js index 83232e43a..56e0fe972 100644 --- a/Sprint-1/interpret/time-format.js +++ b/Sprint-1/interpret/time-format.js @@ -22,3 +22,17 @@ console.log(result); // e) What do you think the variable result represents? Can you think of a better name for this variable? // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +//answers + +// a) there are 6 variable declarations + +// b) there is one function, console.log(result); which prints the result. + +// c) the expression movieLength % 60, uses the % modular which returns the remaining of a division. here 8784 is the total movieLength in seconds and 60 is total seconds in one minute, so the total expression is saying how many seconds are remaining after we divide 8784 by 60 which will be 24 seconds. + +// d) line 4 calculating the the total number of whole minutes in the variable movieLength which is in seconds. + +// e) variable result represents time of the movieLength in hours:minutes:seconds. another name can be movieDuration. + +// f) i tried different values and the code works well. \ No newline at end of file diff --git a/Sprint-1/interpret/to-pounds.js b/Sprint-1/interpret/to-pounds.js index 60c9ace69..f99ec10a6 100644 --- a/Sprint-1/interpret/to-pounds.js +++ b/Sprint-1/interpret/to-pounds.js @@ -24,4 +24,16 @@ console.log(`£${pounds}.${pence}`); // Try and describe the purpose / rationale behind each step // To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" +// 1. const penceString = "399p": initializes a string variable with the value "399p" + +// to help me understand first i googled words that are new to me. penceString means the variable is a string, substring means a portion of a string, withOutTrailing mean describing a variable without a certain character, padded or pad means simply adding to a string. + +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);, creates a new variable called penceStringWithoutTrailingP by removing the last character p, resulting in "399". + +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");, pads the string penceStringWithoutTrailingP with a leading "0" if it is less than 3 characters long, making it at least 3 characters. here "399" is three characters so it remains unchanged. + +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);, will create a variable called const pounds y taking the characters from the start of paddedPenceNumberString up to but not including the last two characters which will result in "3". + +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");, will create a variable called const pence this line of code extracts the last two characters from a padded string representation of pence and ensures that it is represented as a two character string. + +// 6. console.log(`£${pounds}.${pence}`);, will print the amount of money in pounds and pence using template literals and then it outputs that formatted string to the console. \ No newline at end of file diff --git a/Sprint-2/debug/0.js b/Sprint-2/debug/0.js index b46d471a8..fcc4447e2 100644 --- a/Sprint-2/debug/0.js +++ b/Sprint-2/debug/0.js @@ -1,7 +1,16 @@ // Predict and explain first... +// function multiply(a, b) { +// console.log(a * b); +// } + +// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + +// // correction +// the multiply code will not work cause the return is not defined. replace return (a*b) instead of console.log(a*b). function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); + diff --git a/Sprint-2/debug/1.js b/Sprint-2/debug/1.js index df4020cae..253427f93 100644 --- a/Sprint-2/debug/1.js +++ b/Sprint-2/debug/1.js @@ -1,8 +1,18 @@ // Predict and explain first... +// function sum(a, b) { + +// return; +// a + b; +// } + +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// correction +// the sum(10,32) in the console.log will give an undefined result. a + b should be on the same line of code with return. + function sum(a, b) { - return; - a + b; + return a + b; } -console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file diff --git a/Sprint-2/debug/2.js b/Sprint-2/debug/2.js index bae9652a8..0d7b6d359 100644 --- a/Sprint-2/debug/2.js +++ b/Sprint-2/debug/2.js @@ -1,14 +1,27 @@ // Predict and explain first... -const num = 103; +// const num = 103; -function getLastDigit() { +// function getLastDigit() { +// 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 + +// correction +// when the num is defined in the above code it uses the const it should use the let, since getLastDigit is not a constant number. +// aan also getLastDigit in the function should have a parameter (num), else it will only give the last digit of the hardcoded num which is 103. + let num = 103; + +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 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); \ No newline at end of file diff --git a/Sprint-2/errors/0.js b/Sprint-2/errors/0.js index 74640e118..5cd61ea94 100644 --- a/Sprint-2/errors/0.js +++ b/Sprint-2/errors/0.js @@ -3,7 +3,17 @@ // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +//correction +// the problem is the capitalise function is that it is re declaring the str parameter inside the function using let. +// we canot do that cause the str is already a defined function. + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; + str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } +console.log(capitalise("emmanuel")) \ No newline at end of file diff --git a/Sprint-2/errors/1.js b/Sprint-2/errors/1.js index 4602ed237..1a28e38a6 100644 --- a/Sprint-2/errors/1.js +++ b/Sprint-2/errors/1.js @@ -3,11 +3,24 @@ // Why will an error occur when this program runs? // Try playing computer with the example to work out what is going on +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; + +// return percentage; +// } + +// console.log(decimalNumber); + +//correction +// the problem in the above code is that it redeclaring the decimalNumber with const. +// and the The variable decimalNumber is only available inside the function calling it outside will result in error + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); \ No newline at end of file diff --git a/Sprint-2/errors/2.js b/Sprint-2/errors/2.js index 814334d9e..c7b5e0fea 100644 --- a/Sprint-2/errors/2.js +++ b/Sprint-2/errors/2.js @@ -3,8 +3,15 @@ // this function should square any number but instead we're going to get an error -function square(3) { +// function square(3) { +// return num * num; +// } + +// correction +// the problem in the above code is it didnt defined the num as a parameter of square. + +function square(num) { return num * num; } - +console.log(square(3)) diff --git a/Sprint-2/extend/format-time.js b/Sprint-2/extend/format-time.js index f3b83062d..3e19c2da6 100644 --- a/Sprint-2/extend/format-time.js +++ b/Sprint-2/extend/format-time.js @@ -22,3 +22,5 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// input 1, \ No newline at end of file diff --git a/Sprint-2/implement/bmi.js b/Sprint-2/implement/bmi.js index 259f62d48..8c7470d21 100644 --- a/Sprint-2/implement/bmi.js +++ b/Sprint-2/implement/bmi.js @@ -13,3 +13,24 @@ // 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 + + +// solution + +function calculateBMI(weight, height) { + + const heightSquared = height * height; + + + const bmi = weight / heightSquared; + + + return parseFloat(bmi.toFixed(1)); + } + + + const weight = 70; + const height = 1.73; + const bmi = calculateBMI(weight, height); + console.log(`Your BMI is: ${bmi}`); + \ No newline at end of file diff --git a/Sprint-2/implement/cases.js b/Sprint-2/implement/cases.js index 9e56a27b6..61dba0ea0 100644 --- a/Sprint-2/implement/cases.js +++ b/Sprint-2/implement/cases.js @@ -13,3 +13,22 @@ // You will need to come up with an appropriate name for the function // Use the string documentation to help you find a solution + + +//solution + +function toUpperSnakeCase(input) { + + const withUnderscores = input.replace(/ /g, "_"); + + + const upperSnakeCase = withUnderscores.toUpperCase(); + + + return upperSnakeCase; + } + + + console.log(toUpperSnakeCase("hello there")); + console.log(toUpperSnakeCase("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..b9e466a60 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 + + +// solution + +function toPounds(amountInPence) { + + const penceStringWithoutTrailingP = amountInPence.substring( 0,amountInPence.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("399p")); + console.log(toPounds("25p")); + \ No newline at end of file diff --git a/Sprint-2/implement/vat.js b/Sprint-2/implement/vat.js index 3fb167226..80554325e 100644 --- a/Sprint-2/implement/vat.js +++ b/Sprint-2/implement/vat.js @@ -8,3 +8,16 @@ // Given a number, // When I call this function with a number // it returns the new price with VAT added on + + +// solution + +function calculatePriceWithVAT(price) { + + const vatInclusivePrice = price * 1.2; + + return parseFloat(vatInclusivePrice.toFixed(2)); + } + + console.log(calculatePriceWithVAT(23)); + \ No newline at end of file diff --git a/Sprint-2/interpret/time-format.js b/Sprint-2/interpret/time-format.js index c5a0c1619..faea28dac 100644 --- a/Sprint-2/interpret/time-format.js +++ b/Sprint-2/interpret/time-format.js @@ -29,3 +29,11 @@ function formatTimeDisplay(seconds) { // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer + +// answers + +// a) three times, for total hours, remaining minuts and remaining seconds +// b) num = totalHours = 0 +// c) ("00") +// d) num = remainingSeconds = 1 +// e) pad(1) to "01", it converts 1 to a string character "01" . \ No newline at end of file