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

WM5 | Karam Ali | Module-JS1 | Week 2 #88

Open
wants to merge 6 commits 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
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?

Choose a reason for hiding this comment

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

Welldone

16 changes: 15 additions & 1 deletion week-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// const age = 33;

let age = 33;
age = age + 1;

Choose a reason for hiding this comment

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

well done!


// The issue in this code is that you are attempting to reassign a new value to a
// constant variable (age) after it has been initially assigned a value, and then reassign the value by 1
// In JavaScript, const is used to declare a variable whose value should not be reassigned after it's
// initially set. Therefore, attempting to modify or reassign a const variable will result in an error.

// solutions
/*
To resolve this issue, you should use let instead of const
if you want to declare a variable that can be reassigned.
*/
4 changes: 3 additions & 1 deletion week-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// 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}`);

Choose a reason for hiding this comment

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

Welldone


8 changes: 7 additions & 1 deletion 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 last4Digits = cardNumber.slice(-4);
const last4Digits = Math.floor(cardNumber % 10000);
console.log(last4Digits);



// 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 cannot slice numbers, it can only be used for strings.
7 changes: 5 additions & 2 deletions week-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";

Choose a reason for hiding this comment

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

Weldone

console.log(twelveHourClockTime);
console.log(twentyFourHourClockTime);
6 changes: 6 additions & 0 deletions week-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ 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

// count is a variable with an initial value of 0. It represents a numeric value.

// count + 1 is an expression on the right side of the assignment operator (=). function is to count

//
8 changes: 8 additions & 0 deletions week-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

const num = 56.5467;

const wholeNumberPart = Math.round(num) - 2;
const decimalPart = num - Math.round(num);
const roundedNum = Math.round(num) - 1;

console.log(wholeNumberPart);
console.log(decimalPart);
console.log(roundedNum);

// 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 )
Expand Down
10 changes: 10 additions & 0 deletions week-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@ let firstName = "Creola";
let middleName = "Katherine";
let lastName = "Johnson";

<<<<<<< HEAD
const initials = `${firstName.substring(0, 1)}${middleName.substring(
0,
1
)}${lastName.substring(0, 1)}`;
console.log(initials);

// Declare a variable called initials that stores the first 3 characters of each string in upper case
=======
// Declare a variable called initials that stores the first character of each string in upper case to form the user's initials
>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c
// Log the variable in each case
9 changes: 9 additions & 0 deletions week-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);

const dir = filePath.slice(0, lastSlashIndex);
const dotIndex = base.lastIndexOf(".");
const ext = dotIndex !== -1 ? base.slice(dotIndex + 1) : "";

console.log(`The dir part of ${filePath} is: \n ${dir}`);
console.log(`The ext part of ${filePath} is: \n ${ext}`);

// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable


16 changes: 16 additions & 0 deletions week-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;


// - minimum and maximum are constants that define the range of numbers to generate.
// minimum is set to 1 and maximum is set to 100.
// - Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive).
// - (maximum - minimum + 1) calculates the number of integers between the min and max values, inclusive.
// In this case it's 100 - 1 + 1 = 100.
// - Multiplying Math.random() by (maximum - minimum + 1) gives us a random decimal between 0 and 100.
// - Math.floor() rounds the random decimal down to the nearest whole number.
// - Adding minimum (which is 1) to the rounded number offsets the range to be between 1 and 100 instead
// of 0 and 99.
// - So Math.floor(Math.random() * (maximum - minimum + 1)) + minimum ultimately generates a random
// integer between the min and max values of 1 and 100.
// - The result is logged to the num and tested.
// So in summary, it uses Math.random(), Math.floor(), min/max ranges, multiplication, and addition
// to generate a random number between two set values.

// In this exercise, you will need to work out what num represents?
// 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
Expand Down
5 changes: 5 additions & 0 deletions week-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

The Alert function pops out message whenever the user logs on the website.

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
The prompt function pops out the message whenever the user logs on the web page.

What is the return value of `prompt`?
The return vale of Prompt is the user's name.
6 changes: 6 additions & 0 deletions week-1/explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}



Try also entering `typeof console`

Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?

The console.assert() method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. The dot. notation in console.log or console.assert() is a method used to access the specific debugging method on the global console object provided by web browsers.
20 changes: 20 additions & 0 deletions week-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@ const percentageChange = (priceDifference / carPrice) * 100;

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

<<<<<<< HEAD
// There are 3. Line 4, Line 7 and Line 10.

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
=======
// b) Identify all the lines that are variable reassignment statements
>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c

// c) Identify all the lines that are variable declarations

<<<<<<< HEAD
// There are 2. On line 4 and line 5

// d) Identify all the lines that are variable declarations

// There are 2. On line 7 and line 8

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

// This expression is saying that I want to replace the data type of carPrice from a string to a number

=======
// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c
22 changes: 22 additions & 0 deletions week-1/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,38 @@ 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.

// b) How many function calls are there?
// There isn't any function calls.

// c) Using documentation on MDN, explain what the expression movieLength % 60 represents

// This represents how many seconds are in a minute.
// This ensures that all the minutes are accounted for, and any remainders will be formatted in seconds.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// It means that I want you to collect the movieLength which is (8784), I want you to minus the remainingSeconds from this,
// and because remainingSeconds seconds is stored in a variable, it works out what the sum should be for remainingSeconds,
// which is (24 seconds). This leaves us with a sum from the expression movieLength - remainingSeconds = (8760),
// which if then divided by 60 = (146).

// e) What do you think the variable result represents? Can you think of a better name for this variable?

<<<<<<< HEAD
// The amount of time left of the film
// const filmTimeLeft = `${remainingHours}:${remainingMinutes}:${remainingSeconds}`;

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

// The code will work with all values for movieLength. When the result is executed with a value of 1,
// the timer will display 0(Hours):0(Minutes):1(Seconds).
// I did notice though that if I changed this number to a ridiculous number for example: 100000000000000 or 1000000000,
// it would return the same value, which it should not
=======
// f) Think about whether this program will work for all values of movieLength.
// Think of what values may cause problems for the code.
// Decide the result should be for those values, then test it out.
// Can you find any values of movieLength which don't give you what you'd expect?
>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c
16 changes: 16 additions & 0 deletions week-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ console.log(`£${pounds}.${pence}`);
// You should use MDN to look up substring, padStart and padEnd to interpret this program

// To begin, we can start with
<<<<<<< HEAD
// const penceString = "399p": initialises a string variable with the value "399p"

// It stores actual number value of the string.From index of 0 until second last index of the string.so it can exclude letter p.

//This line is ensuring that if its length is less than 3 characters, it adds "0" characters at the beginning of the string
//to make it a total of 3 characters long. If the original string is already 3 characters or longer, no padding is added.

//It is storing first index of the string which is 3 in variable pounds and represent the number of pounds in that string, while excluding remaining number.

// It is storing last two indexes of the string which are 99 in variable pence and represent the number of pence in that string,
//while excluding remaining number. After extracting the last two digits,
//the padEnd method is used to ensure that the string is always two characters long. If the extracted substring is less than two characters, it pads the string with zeros on the right side until it reaches a length of 2.
//It is displaying total number of pounds and pence.
=======

// 1. const penceString = "399p": initialises a string variable with the value "399p"
>>>>>>> 03fce56db903fb986ff36f73e4d57cebfa18707c
5 changes: 4 additions & 1 deletion week-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

// The console.log will not return the value of what action you want for a and b to execute and it should be changed to return 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)}`);

Choose a reason for hiding this comment

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

nice work

6 changes: 4 additions & 2 deletions week-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Predict and explain first...
// Line 4 is returning nothing because of semicolon and command is not confirmed.


function sum(a, b) {
return;
a + b;
return a + b;

}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
5 changes: 3 additions & 2 deletions week-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Predict and explain first...

const num = 103;

function getLastDigit() {

function getLastDigit(num) {
return num.toString().slice(-1);
}

Expand All @@ -12,3 +12,4 @@ 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
//It was not taking num as an argument and have fixed value of num which is 103, each time when it is called it take declared value of num.
24 changes: 19 additions & 5 deletions week-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
// Predict and explain first...
// write down the error you predict will be raised
// then call the function capitalise with a string input
// interpret the error message and figure out why it's happening, if your prediction was wrong

// str has been stored as a value inside the function capitalize, meaning that it will not work,
// once a name has been used it cannot be reused as this will cause conflict.


// 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;
// }

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
return `${str[0].toUpperCase()}${str.slice(1)}`;
}

const capitaliseTest = capitalise("hi");
const myNameCapTest = capitalise("karam");

console.log(capitaliseTest);
console.log(myNameCapTest);

Choose a reason for hiding this comment

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

LGTM

19 changes: 17 additions & 2 deletions week-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@
// Why will an error occur when this program runs?
// Play computer with the example to work out what is going on

//This will not work because in line 7 the variable decimal number declaration came after the function. It should come first then the function declaration.
//Also we need to call the function in the console log for it to work.

// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

// return percentage;
// }

// console.log(decimalNumber);

//I have corrected the code below

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;

const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);

console.log(convertToPercentage(0.7));

Choose a reason for hiding this comment

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

What you did here is correct, however, you need to declare decimalNumber as a var and assign 0.7 to it, and then pass the "decimalNumber" into the function convertToPercentage instead of passing 0.7 straight.

9 changes: 7 additions & 2 deletions week-2/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

// Predict and explain first...
// The number 3 should have been declared as an argument not as a declaration.
// 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(num) {
return num * num;
}


console.log(square(3));
console.log(square(2));
console.log(square(4));

Choose a reason for hiding this comment

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

Well done!

Loading