From aebc18349564175044455daec3172105b4abd595 Mon Sep 17 00:00:00 2001 From: anuthapaliy <113211321+anuthapaliy@users.noreply.github.com> Date: Wed, 27 Dec 2023 08:30:20 +0000 Subject: [PATCH] started time format js --- week-1/errors/1.js | 8 +++++++- week-1/errors/2.js | 8 +++++++- week-1/errors/3.js | 10 ++++++++-- week-1/errors/4.js | 9 +++++++-- week-1/exercises/count.js | 2 ++ week-1/exercises/decimal.js | 6 ++++++ week-1/exercises/initials.js | 15 +++++++++++++++ week-1/interpret/time-format.js | 7 +++++-- week-2/debug/0.js | 14 ++++++++++++-- week-2/debug/1.js | 19 ++++++++++++++----- week-2/debug/2.js | 26 +++++++++++++++++++------- week-2/errors/0.js | 19 ++++++++++++++++--- week-2/errors/1.js | 19 +++++++++++++++---- week-2/errors/2.js | 8 ++++++-- week-2/implement/bmi.js | 12 ++++++++++++ week-2/implement/cases.js | 17 +++++++++++++++++ week-2/implement/vat.js | 8 ++++++++ week-2/interpret/time-format.js | 1 + 18 files changed, 177 insertions(+), 31 deletions(-) diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..95fb5fde 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,10 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +// const age = 33; +// age = age + 1; + +// In js if we use const to declare a variable its value cannot be reassigned. If we want to modify the value, we should use let instead of const. + +let age = 33; age = age + 1; +// This will work because it will alow us to reassign the values. \ No newline at end of file diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..b80e12f9 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,11 @@ // 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"; + const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + + +// The problem is here that variable cityOfBirth has been declared after using in template literal. First variable needs to be declared. \ No newline at end of file diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..0e32c1dc 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,7 +1,13 @@ -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 // Make and explain a prediction about why the code won't work // Then try updating the expression last4Digits is assigned to, in order to get the correct value + + +// => slice method is a string method but card Number is a numeric Value. First we have to convert this cardNumber to string. + +const cardNumber = 4533787178994213; +const last4Digits = cardNumber.toString().slice(-4); diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..39db7656 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,7 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +// const 12HourClockTime = "20:53"; +// const 24hourClockTime = "08:53"; + +// I think the issue is with the variable name. In JS variable name start with letter not with Numbers. + +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..2ca3a836 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 + +// => = is assignment operator. so the new value of count is 0 + 1, 1. so this line 3 shows the increment the value of count by 1. \ No newline at end of file diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..196f3fb5 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -4,7 +4,13 @@ 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 // Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) +let wholeNumberPart = Math.floor(num); +console.log(wholeNumberPart); // Create a variable called decimalPart and assign to it an expression that evaluates to 0.5467 ( the decimal part of num ) +let decimalPart = num - wholeNumberPart; +console.log(decimalPart); // Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) +let roundedNum = Math.round(num); +console.log(roundedNum); // Log your variables to the console to check your answers diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..647ad69a 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -4,3 +4,18 @@ 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 firstInitial = firstName[0].toUpperCase(); +// let middleInitial = middleName[0].toUpperCase(); +// let lastInitial = lastName[0].toUpperCase(); + +// let initials = firstInitial + middleInitial + lastInitial; + +// console.log(initials); + +let initials = firstName[0].toUpperCase() + middleName[0].toUpperCase() + lastName[0].toUpperCase(); + +console.log(initials); + + + diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 7961fe0d..9621de37 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -14,12 +14,15 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? - +// =>There are 7 variable declarations in this Program. // b) How many function calls are there? -// c) Using documentation on MDN, explain what the expression movieLength % 60 represents +// =>There are no function calls. +// c) Using documentation on MDN, explain what the expression movieLength % 60 represents +// =>In JavaScript module Operatoor(%) means remainder. So movieLength % 60 means it will give remaining seconds when movieLength is divided by 60. // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// => This line 4 calculate the total minute in the movie. It subtracts the remaining seconds from movieLength and divide the result by 60 to convert seconds to minute. // e) What do you think the variable result represents? Can you think of a better name for this variable? diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..72f68b4d 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -1,7 +1,17 @@ // 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)}`); + + +// Here the multiply function didnt return only console.log. So without return if it uses console.log, it wont give the number instead it will output as undefined. +// so, the corrected code would be-: + function multiply(a, b) { - console.log(a * b); + return a * b; } -console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); \ No newline at end of file diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..cf5500bf 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,17 @@ // Predict and explain first... -function sum(a, b) { - return; - a + b; -} +// function sum(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)}`); +// Again its undefined because there is nothing after return statement. If we want value we have to replace it with return a + b; + +function sum(a, b) { + return a + b; + } + + console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); \ No newline at end of file diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..913153f5 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,14 +1,26 @@ // Predict and explain first... -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// 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)}`); +// 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 + // => getLastDigit function doesnt have arguement but in console log it uses the number 42, 105, 806. function should have a parameter number, then it will work as expected. + + + const num = 103; + + function getLastDigit(number) { + return number.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)}`); \ No newline at end of file diff --git a/week-2/errors/0.js b/week-2/errors/0.js index 58b1349d..fa7f2d5d 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -1,9 +1,22 @@ // Predict and explain first... // write down the error you predict will be raised -// then call the function capitalise with a string input + // => why to use let statement here? Also parameter name and declaring variable also with the same name str. + // 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)}`; +// return str; +// } + + +// corrected code-: function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } + +// then call the function capitalise with a string input +let result = capitalise ("nepal"); +console.log(result); + + diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..9debb556 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -1,13 +1,24 @@ // Predict and explain first... // Write down the error you predict will be raised +// => parameter and variableName are same. // Why will an error occur when this program runs? // Play 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) + + +// corrected code-: function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; + return `${decimalNumber * 100}%`; - return percentage; } -console.log(decimalNumber); +console.log(convertToPercentage(0.5)); \ No newline at end of file diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..09788eaa 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -3,8 +3,12 @@ // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? -function square(3) { +// function square(3) { +// return num * num; +// } +// (why function parameter is 3, it should be variable name.) +// corrected code-: +function square(num){ return num * num; } - diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..5379542d 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -13,3 +13,15 @@ // 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 calculationOfBMI(height, weight){ + // calculate bmi +let bmi = weight / (height * height); + +// rounded BMI to 1 decimal place +let roundedBMI = parseFloat(bmi.toFixed(1)); + return roundedBMI; +} \ No newline at end of file diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index f66bf7e3..fffc87df 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -15,3 +15,20 @@ // Come up with a clear, simple name for the function // Use the string documentation to help you plan your solution + +function upperSnakeCase(inputString){ + let stringWords = inputString.split(" "); + let upperCaseWords = stringWords.map(letter => letter.toUpperCase()); + let underScoreWords = upperCaseWords.join("_"); + return underScoreWords; + +} +console.log(upperSnakeCase("lord of the rings")); + + + +// function upperSnakeCase(inputString) { +// // Replace spaces with underscores, then convert to uppercase +// return inputString.replace(/\s+/g, '_').toUpperCase(); +// } +// console.log(upperSnakeCase("lord of the rings")); \ No newline at end of file diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..ea0e3dba 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 priceWithVAT(price) { + let tax = 1.2; + newPrice = price * tax + return parseFloat(newPrice.toFixed(1)); +} + +// console.log(priceWithVAT(50)); diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..fe4405be 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -26,6 +26,7 @@ console.log(formatTimeDisplay(143)); // Questions // a) When formatTimeDisplay is called how many times will pad be called? +// =>The pad function will be called three times within the formatTimeDisplay function: once for hours, once for minutes, and once for seconds. // Call formatTimeDisplay with an input of 143, now answer the following: