Skip to content
Open
4 changes: 3 additions & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ let count = 0;
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
// The = sign means 'assignment' operator
// In line 3, at first the calculation will be done at R.H.S,
// with the initial count value then the calculated value will be stored at the L.H.S count
9 changes: 2 additions & 7 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@ let firstName = "Creola";
let middleName = "Katherine";
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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn

let initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`;
console.log(initials);
11 changes: 3 additions & 8 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
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
// 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);
const dotIndex = base.lastIndexOf(".");
const ext = base.slice(dotIndex);
16 changes: 14 additions & 2 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ const minimum = 1;
const maximum = 100;

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

// In this exercise, you will need to work out what num represents?
// num stores the result of the expression after calculating it.
// 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
// There is a range of numbers from 1 to 100. In the expression,
// (maximum - minimum + 1) calculates the size of the possible integers, which is 100.
// The Math.random() function generates a random decimal number in the range 0 ≤ x < 1.
// This random decimal is then multiplied by the range size to scale it to a
// value between 0 and 100. The result is a floating-point number in this interval.
// Next, Math.floor() is used to round the number down to the largest integer less than
// or equal to the calculated value, producing an integer between 0 and 99.
// Finally, by adding minimum, the range is shifted from 0–99 to 1–100, giving a random
// integer within the required range.
// I have run 5 times the program and this produces different values each time because
// Math.random() generates a new random decimal number every time.
// Since the final value of num depends on this function, the result changes on each execution.
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?
We don't want the computer to run these 2 lines - how can we solve this problem?

// We can solve this problem by writing those two lines as comments using //,
// because JavaScript ignores commented lines and they will not be executed by the computer.
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;

// There is a constant variable age, which is fixed and it's value cannot be reassigned.
// If we want to change the value of age, we have to use let instead of const.
3 changes: 3 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,6 @@

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

// The variable const cityOfBirth = "Bolton"; needs to be declared at first.
// As it is not declared, JavaScript cannot find out the variable cityofBirth in console.log.
14 changes: 12 additions & 2 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
console.log(cardNumber.toString());
const cardNumber1 = cardNumber.toString()
const last4Digits = cardNumber1.slice(-4);
console.log(last4Digits);

// 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
// I think slice () works for strings or arrays not for numbers. That's why the code won't work.
// Then run the code and see what error it gives.
// After running code TypeError: cardNumber.slice is not a function this error shows.
// 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
// It gives this error because cardNumber doesn't have slice method. My prediction was beacuse
// cardNumber is number not string and the difference is that slice method called but it
// doesn't exist on that type.
// Then try updating the expression last4Digits is assigned to, in order to get the correct
// value
// Updated the expression in order to get in to the correct value.
4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const TwelveHourClockTime = "8:53pm";
const TwentyfourhourClockTime = "20:53";
18 changes: 17 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +12,27 @@ 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 5 function calls. carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
// In the above 2 lines, 2 replaceAll() functions for carPrice and priceAfterOneyear and after executing them again Number()
// functions for the 2 variables.
// At last, console.log() function for printing the executed value.

// 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?
// after running the code, the error is coming from this line priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));and
// the error is SyntaxError: missing ) after argument list. There is a comma missing in replaceAll() syntax. It should be
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// c) Identify all the lines that are variable reassignment statements
// There are 2 variable reassignment statements.
// const priceDifference = carPrice - priceAfterOneYear;
// const percentageChange = (priceDifference / carPrice) * 100;

// d) Identify all the lines that are variable declarations
// There are 4 variable declarations lines.
// let carPrice = "10,000";
// let priceAfterOneYear = "8,543";

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Firstly this expression replace all the commas from the variable carPrice = "10,000" to "10000" and then it converts
// string "10,000" to number 10000.
9 changes: 9 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,23 @@ 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.

// b) How many function calls are there?
// There is one function call.

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// The expression movieLength % 60 devides the movieLength by 60 seconds and return the reminder
// which will be stroed in the const remainingSeconds.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// Firstly, Removes the leftover seconds from movieLength and then converts the remaining seconds
// into whole minutes which will be stored into totalMinutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// The variable result reprents the movieLength in hours:minutes:seconds. The variable should be renamed by totalDuration.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// I have tried with 3 variables, 1515, 9999 & 70052. I observed that this code works succesfully with all the values
// and gave me results for all the different movieLength in hours:minutes:seconds.
15 changes: 15 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); : a variable declared and a function called
// in the variable with substring method. In the substring method (penceString.length - 1) = 4 -1 = 3 then the function remains
// penceString.substring(0, 3); from which we find const penceStringWithoutTrailingP = 399p.penceString.substring(0, 3); to
// const penceStringWithoutTrailingP = 399
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); : another variable const paddedPenceNumberString
// declared and a function called in the variable with padStart method. penceStringWithoutTrailingP.padStart(3, "0"); will be 399.padStart(3, "0");
// and finally const paddedPenceNumberString = "399". Secondly, another variable const pounds declared in which calculates the value in
// pounds. For this, (0, paddedPenceNumberString.length - 2); will be (0, 1) and the const pounds = 399.substring(0, 1); calculates
// const pounds = 3
// 4. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); : a variable pence declared
// to calculate the value in pence. so, if we put the values it will be const pence = 399.substring(1).padEnd(2, "0"); which at first works for
// substring and then for padEnd and will be const pence = 99;
// 5. console.log(`£${pounds}.${pence}`); : this console function printed the prince in pounds and pence.And finally the printed output
// will be £3.99
3 changes: 3 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
// `alert` function deisplays a popup dialogue box with a message and the user have to click 'ok' button.

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?
// `prompt` function displays a dialogue box with a message and asks the the user to give input.
What is the return value of `prompt`?
//It returns the value which user inputs. In the question it is myName.
7 changes: 7 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ 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] } . This is the output. The output indicates it is a function whose name is log() and which is built in
//Javascript.

Now enter just `console` in the Console, what output do you get back?
//console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} . this is the output. In which console shows many functions it delt with such as debugging, error, info, log warn etc.

Try also entering `typeof console`
//The output is 'object'. Which means it is an object which perform many functions.

Answer the following questions:

What does `console` store?
`console` stores different methods (functions).
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
//Both are the function reference. If we further call `console.log` then the function runs and it will print output.
console.assert() function writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. `.` used to access the property of the object. In both the functions, `.` accesses `log` or `assert` property of the console object and get the values.
Loading