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: we are re-assigning count using the assignment operator (=). The " = " is used in JavaScript to assing a value
// to a variable and in this case we are re-assinging count to equal count + 1.
Comment on lines +8 to +9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Operation like count = count + 1 is very common in programming, and there is a programming term describing such operation.

Could you find out what one-word programming term describes the operation on line 3?

4 changes: 2 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,7 @@ 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 = ``;

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

7 changes: 5 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
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 = ;
const dir = filePath.slice(1 , 44);
console.log(dir)
const ext = filePath.slice(lastSlashIndex + 5 );
console.log(ext)
Comment on lines +21 to +24

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you explore an approach that could work for any valid file path? For examples,
/tmp/interpret/file.json and /Users/mitch/cyf/Module-JS1/mycode.js.


// https://www.google.com/search?q=slice+mdn
26 changes: 22 additions & 4 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ 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?
//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

/*
Answer

num is a variable that'll hold the output of the expression in line 4, I have run the code several times and I get a
different number each run.
Math.random returns a random decimal number between 0 (inclusive) and 1 (exclusive).
Math.floor rounds the number to the nearest integer.
This expression is evaluated according to parenthesis and operator precedence, so for this expression it will be as follows:
1. (maximum - minimum + 1)
2. Math.random() is called
3. result from Math.random * result from > maximum - minimum + 1
4. Math.floor is then called with (Math.random * result from > maximum - minimum + 1)
5. result from Math.floor + minimum
*/

// 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
// Try logging the value of num and running the program several times to build an idea of what the program is doing
7 changes: 5 additions & 2 deletions 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?
/*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?*/

//Answer
//We will comment it out.
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/* we cannot reassign a variable that was declared using the const keyword so we will change it to the let keyword
which allows us to reassign variables*/

let age = 33;
age = age + 1;
console.log(age)
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// 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}`);


/* The variable is declared after it's been used and when we run the code we get this > ReferenceError: Cannot access
'cityOfBirth' before initialization */
17 changes: 15 additions & 2 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
//const cardNumber = 4533787178994213;
//const last4Digits = cardNumber.slice(-4);


// to fix the code, we'll convert the number to a string because .slice does not work on number data type
let cardNumber = 4533787178994213;
cardNumber = cardNumber.toString()
let 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

//Answer
// We are not logging to the console
// The slice method should be a positive number to represent the index position.

console.log(last4Digits)
//TypeError: cardNumber.slice is not a function
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
const _12HourClockTime = "8:53pm";
const $24hourClockTime = "20:53";
Comment on lines +1 to +2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Identifiers that begin with _ or $ are valid variable, function, class, and property names. However, they are usually used by convention to signal special meaning.

Could you explore names that start with alphabets instead? Feel free to ask AI for suggestion.


//when starting a variable name with a number we have to precede with either of _ or $
11 changes: 9 additions & 2 deletions 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,18 @@ 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 3 function calls. line 4 Number() and .replaceAll()
// line 12 console,log()

// 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?
//A syntaxError is occurring on line 5, we are missing a "," and a closing ")"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can more precisely describe "A comma is missing between "," and "" in the function call" as:
A comma is missing between the ___________s.

What is the programming term that belongs in the blank?

Note: The original code does not have a missing closing ")".


// c) Identify all the lines that are variable reassignment statements
// line 4 and 5

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

// line 1, 2, 7, 8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/* We are removing the comma, the replaceAll method takes in two arguements, 1 for what to replace and 2 for what to
replace by, and in this case we are replaceing the comma with an empty string. we wrap the carPrice variable which is a string
with the Number() function to convert it into a number*/
15 changes: 13 additions & 2 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const movieLength = 8784; // length of movie in seconds
// const movieLength = 8784; // length of movie in seconds
const movieLength = 879094; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -12,14 +13,24 @@ 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?
// 6

// b) How many function calls are there?
// 1

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// % is the modulos operator, movieLength % 60 gives us the remainder, essentially we divide 60 by movieLenght and return the remainder

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/* we are subtracting remainingSeconds from movieLength, remainingSeconds is a variable that holds the result from totalMinutes % 60,
the expression in brackets will evaluate first and then the result divided by 60.
*/

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

//runTime
Comment on lines -24 to +31

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

runTime reads like "runtime", which has a different meaning in IT. Could you suggest a different (and more descriptive) name?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
/* I have experimented with different values by changing the movieLength variable, and they all work, it works because we have
didn't hardcode our expressions and used variables.
*/

14 changes: 14 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const penceStringWithoutTrailingP = penceString.substring(
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
Expand All @@ -25,3 +26,16 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 3. we are declaring a variable and assigning it the variable penceString which holds the value "399p" but without the p - the method .subString is used
// here to remove the p.
// 8. we are declaring a variable and assigning it the variable penceStringWithoutTrailingP which holds the value "399
// we are then using the .padStart method, however the method will not make any change because we have passed in the argument 3 which i
// is the total length the string should be after padding - our string is already 3 lengths long.
// 10. we are using a substring to slice our string from the first index to paddedPenceNumberString.length - 2, paddedPenceNumberString.length
// will give us the number 3, so paddedPenceNumberString.length - 2 also means 3 - 2 which gives us 1, so from index 0 which is 3 to index 1 which is exclusive.
// 15. we are declaring a variable pence and assigning it the variable paddedPenceNumberString which holds the result of a couple
// method chains, firstly a .substring method with the argument "paddedPenceNumberString.length - 2" which also means 3 - 2, so it will
// slice from index 1 to the end as we have not given a second argument, then we follow with a padEnd, this time we are adding
// characters to the end of the string - we want a total of two characters
//19. we use a string template to console log

Loading