Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NW6| FIDAA BASHIR | Module-JS1 | WEEK3 #190

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions week-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
/*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?*/
3 changes: 2 additions & 1 deletion week-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion week-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -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}`);
3 changes: 2 additions & 1 deletion week-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions week-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
console.log(twelveHourClockTime);
console.log(twentyFourHourClockTime);
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
// ans ; it will reassign the count variable
console.log(count);
8 changes: 7 additions & 1 deletion week-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
2 changes: 2 additions & 0 deletions week-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 4 additions & 0 deletions week-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions week-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 4 additions & 0 deletions week-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
4 changes: 1 addition & 3 deletions week-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
4 changes: 4 additions & 0 deletions week-2/debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions week-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 5 additions & 7 deletions week-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion week-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
6 changes: 4 additions & 2 deletions week-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
9 changes: 5 additions & 4 deletions week-2/errors/2.js
Original file line number Diff line number Diff line change
@@ -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));
11 changes: 11 additions & 0 deletions week-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
15 changes: 12 additions & 3 deletions week-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
11 changes: 11 additions & 0 deletions week-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 calculatePriceWithVAT(price) {
price = price * 1.2;

return price;
}

console.log(calculatePriceWithVAT(50));
11 changes: 11 additions & 0 deletions week-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
//}
33 changes: 25 additions & 8 deletions week-3/debug/format-as-12-hours.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
// formatAs12HourClock currently has a 🐛

// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42"
// b) Check the assertion output and explain what the bug is
// c) Now fix the bug and re-run all your assertions

function formatAs12HourClock(time) {
if (Number(time.slice(0, 2)) > 12) {
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
let a = `${Number(time.slice(0, 2)) - 12}`;
if (a < 9) {
a = ("0" + a).slice(-2);
}
let b = `${Number(time.slice(-2))}`;
if (b < 9) {
b = "0" + b;
}
return a + ":" + b + " " + "pm";
}
let a = `${time.slice(0, 2)}`;
if (a < 9) {
a = ("0" + a).slice(-2);
}
return `${time} am`;
let b = `${Number(time.slice(-2))}`;
if (b < 9) {
b = "0" + b;
}
return a + ":" + b + " " + "am";
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
Expand All @@ -23,8 +44,4 @@ console.assert(
targetOutput2
);

// formatAs12HourClock currently has a 🐛

// a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42"
// b) Check the assertion output and explain what the bug is
// c) Now fix the bug and re-run all your assertions
console.log(formatAs12HourClock("11:00"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! We can also try different cases using console.log to make sure that the code works properly for any kind of input.

22 changes: 22 additions & 0 deletions week-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"

function getAngleType(angle) {
if (angle < 90) {
return "Acute angle";
} else if (angle == 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle == 180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
return "invalid angle";
}

console.log(getAngleType(80));
console.log(getAngleType(90));
console.log(getAngleType(120));
console.log(getAngleType(180));
console.log(getAngleType(200));
console.log(getAngleType(540));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! 👍 That's great you considered the invalid inputs as well, to return invalid angles!

25 changes: 25 additions & 0 deletions week-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."

function getCardValue(cardString) {
const [rank, suit] = cardString.split("♠");
switch (rank) {
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "10":
return parseInt(rank);
case "J":
case "Q":
case "K":
return 10;
case "A":
return 11;
default:
throw new Error("Invalid card rank.");
}
}
console.log(getCardValue(10));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great attempt to implement the function! Try to run your code again and find out how to fix the error if it gives you any error! We also need to write assertions using console.assert as the exercise asks us to do, in line 5!

Loading