Skip to content
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
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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

// Line 3 is changing the value of the count variable by increasing 1 to its current value.
// The = operator is used to assign the new value back to the count variable.
8 changes: 6 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ 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.

let initials = ``;
function getInitial(name) {
return name[0];
}

// https://www.google.com/search?q=get+first+character+of+string+mdn
let initials =
getInitial(firstName) + getInitial(middleName) + getInitial(lastName);
console.log(initials);

8 changes: 4 additions & 4 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ console.log(`The base part of ${filePath} is ${base}`);
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;

// https://www.google.com/search?q=slice+mdn
const dir = filePath.slice(0, lastSlashIndex);
console.log(`The dir part of ${filePath} is ${dir}`);
const ext = filePath.slice(filePath.lastIndexOf("."));
console.log(`The ext part of ${filePath} is ${ext}`);
6 changes: 6 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ 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
console.log(num);
// num is a random integer between 1 and 100.
// Math.random() generates a random floating-point number between 0 and 1.
// Multiplying this by (maximum - minimum + 1) scales it to a range of 0 to 100.
// Math.floor() rounds down the result to the nearest integer number.
// Finally, adding minimum shifts the range to be between 1 and 100.
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -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?
/* 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?
*/
5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-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;
age = age + 1;
let age = 30; // Change const to let to allow reassignment
age += 1; // Increment age by 1
console.log(age);
6 changes: 6 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";

// The variable cityOfBirth should be declared before it is used in the console.log statement.
/*
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
*/
15 changes: 15 additions & 0 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/* The old code:
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
// Before running the code, make and explain a prediction about why the code won't work
// 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

/* My prediction: The code will give an error because the minus sign(-4) and slice works only with strings, not numbers.

Consideration: The error is a TypeError: cardNumber.slice is not a function. mostly it was because I predicted that numbers do not have a slice method. About the minus sign, I have learned about positive and negative indexing in strings, now I can see the difference.


To fix this, we need to convert cardNumber to a string before calling the slice method.
*/

// Updated code:
const cardNumber = 4533787178994213;
const last4DigitsCorrected = cardNumber.toString().slice(-4);
console.log(last4DigitsCorrected);
13 changes: 12 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
/*
The old code:
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";
console.log(24hourClockTime);*/

// It is giving a syntax error
// The reason is that variable names cannot start with a number

// Updated code:
const hourClockTime12 = "20:53";
const hourClockTime24 = "08:53";
console.log(hourClockTime24, hourClockTime12);
42 changes: 42 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,52 @@ console.log(`The percentage change is ${percentageChange}`);

// a) How many function calls are there in this file? Write down all the lines where a function call is made

// There are 5 function calls in this file:
// line 4: carPrice.replaceAll(",", "")
// line 4: Number(carPrice.replaceAll(",", ""))
//line 5: priceAfterOneYear.replaceAll("," "")
// line 5: Number(priceAfterOneYear.replaceAll("," ""))
// line 9: console.log(`The percentage change is ${percentageChange}`)



// 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?
/*
It's a SyntaxError in line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));.
The error is occurring because there is a missing comma between the two arguments of the replaceAll function here ("," "").
To fix this problem, we need to add a comma between the two arguments: replaceAll(",", "").
*/



// c) Identify all the lines that are variable reassignment statements
/*reusing the same variable carPrice but updating its value:
carPrice = Number(carPrice.replaceAll(",", ""));

reusing the same variable priceAfterOneYear but updating its value:
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/



// d) Identify all the lines that are variable declarations
/*declaring a new variable carPrice:
let carPrice = "10,000";

declaring a new variable priceAfterOneYear:
let priceAfterOneYear = "8,543";

declaring a new variable priceDifference:
const priceDifference = carPrice - priceAfterOneYear;

declaring a new variable percentageChange:
const percentageChange = (priceDifference / carPrice) * 100;
*/

Choose a reason for hiding this comment

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

These are correct - are they any other variables being declared?

Copy link
Author

Choose a reason for hiding this comment

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

@LonMcGregor , priceDifference and precentageChange are also variables.



// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*
The expression Number(carPrice.replaceAll(",", "")) is converting the carPrice string into a number.
The purpose of this expression is to get a numeric value for carPrice that can be used for calculations.
*/
38 changes: 38 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,52 @@ 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 6 variable declarations:
1.Line 1 : const movieLength.
2.Line 3 : const remainingSeconds.
3.Line 4 : const totalMinutes.
4.Line 6 : const remainingMinutes.
5.Line 7 : const totalHours.
6.Line 9 : const result.
*/

// b) How many function calls are there?

/* There is one function call:
Line 10 : console.log().
*/

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

/* The movieLength represents the length of the movie in seconds, the operand % is remaining (Modulo) returns the remaining of the movieLength divided by 60(to convert sec into minutes) but it only gives the remaining of this operation.
*/

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

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

/*
The expression assigned to completed total minutes of the movie, by subtract the movie length in sec from the remaining seconds and divide the result by 60 to convert it to minutes
*/

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

/*
The variable result display the total length of the movie in these form (Hours:Minutes:seconds) in string format.

I can think of "MovieDuration" as a better name because it gives more description to the output.

Choose a reason for hiding this comment

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

Is it clear what the difference is between movieDuration and movieLength? Could I understand the difference by quickly reading these two variable names?

Copy link
Author

Choose a reason for hiding this comment

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

@LonMcGregor
I think movieDuration it emphasizes the meaning of time span than movieLength.(I guess)

Copy link
Author

Choose a reason for hiding this comment

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

@LonMcGregor what about (totalscreentime) ? is this more obvious ?

Choose a reason for hiding this comment

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

@Alaa-Tagi They both show the total screen time. Think of what the difference is between the movie length variable and this new variable at the end of the code (what type of data is used, what content the data holds). What different type of data is stored there? How could you make it obvious what the difference is?

Copy link
Author

Choose a reason for hiding this comment

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

Oh, I got what you meant now. Are you saying that the difference in datatype and representation for movie length is numeric data, while screen time is considered structured data? Is that correct? What about total_runtime_minutes?

Choose a reason for hiding this comment

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

Yes, that's what I was thinking. Because your final argument is formatted to show hours, minutes, seconds, rather than total seconds as an integer, picking a name that makes that clear is better. It shows the runtime with more details than just minutes, so is there an even better name?

Copy link
Author

Choose a reason for hiding this comment

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

I guess it can be ( runtime_hms) Is it still not better name?

Choose a reason for hiding this comment

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

runtime_hms sounds good to me - this makes it clear that it contains all of hours, minutes and seconds.

*/

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

/*
I did experimenting the code by putting different value to the movieLength and it works just fine.

test numbers and its outcomes:
1. 36000sec>> 10:0:0
2. 120 sec >> 0:2:0
3. 0 sec >> 0:0:0
4. 555555.55sec >> 154:19:15.550000000046566

As the results show the code is working fine even with unexpected large numbers.
*/
7 changes: 6 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ 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": initialises a string variable with the value "399p".
// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1): represent to takes a substring from index 0 up to (but not including) penceString.length - 1.
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): assign to ensures the numeric string is at least 3 characters long by adding leading zeros if needed.
// 4. const pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2): Takes the substring from the start up to (but not including) the last two characters.
// 5. substring(length - 2) : use to returns the last two characters (the pence digits). padEnd(2, "0") would append trailing zeros if the result were shorter than 2.
// 6. console.log(`£${pounds}.${pence}`): assign to display the price in a human-readable pounds-and-pence format (£x.yy)
5 changes: 5 additions & 0 deletions Sprint-1/4-stretch-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?

Answer: A pop up window displaying a text (Hello World!) will appear on the top and an OK button. It's a temporarily effect, but it required a user interaction to press OK before continuing.

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?
What is the return value of `prompt`?

Answer: A pop up window appears with a title and a question (What is your name?), also an answering space area. At the right down of the window there are two buttons to the user, Ok and cancel.
After typing myName and press OK the console display the answer(myName).
19 changes: 18 additions & 1 deletion Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

-The output : ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?

-The output : console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} + a lot of functions under it.

Try also entering `typeof console`

Answer the following questions:
-The output : object

-Answer the following questions:

What does `console` store?

-Answer : console is an object with debugging methods.
It does not store variables or result. It provide methods for outputting , data , object and more.

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

Answer:
-console is a built-in object.
-log and assert are properties of that object, specifically functions (methods).
-The console.assert() static method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
-The console.log() static method outputs a message to the console.
-'.' means use this method from this object.