Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

London_10/Anu Thapaliya/Module-JS1/week2 #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion week-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion week-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 8 additions & 2 deletions week-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 7 additions & 2 deletions week-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// 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";
2 changes: 2 additions & 0 deletions week-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions week-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions week-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);



7 changes: 5 additions & 2 deletions week-1/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
14 changes: 12 additions & 2 deletions week-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -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)}`);
19 changes: 14 additions & 5 deletions week-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -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)}`);
26 changes: 19 additions & 7 deletions week-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -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)}`);
19 changes: 16 additions & 3 deletions week-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -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);


19 changes: 15 additions & 4 deletions week-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -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));
8 changes: 6 additions & 2 deletions week-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


12 changes: 12 additions & 0 deletions week-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions week-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
8 changes: 8 additions & 0 deletions week-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
1 change: 1 addition & 0 deletions week-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down