Skip to content

Glasgow | ITP May 25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Sprint 1 #597

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

Open
wants to merge 5 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
2 changes: 2 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,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

// count is the name of the variable. count is not a constant variable and can be changed and updated because of the way it is declared using let. The initial value of count is set to 0. From line 3, the = operator is used to assign a new value to the count variable. The expression count + 1 calculates the current value of count (which is 0) and adds 1 to it, resulting in a new value of 1. This new value is then assigned back to the count variable, effectively updating its value from 0 to 1.
2 changes: 2 additions & 0 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ let initials = ``;

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

initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
console.log(initials); // Expected output: "CKJ"
8 changes: 6 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ 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(0, lastSlashIndex);
const dotIndex = base.lastIndexOf(".");
const ext = base.slice(dotIndex);

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

// https://www.google.com/search?q=slice+mdn
17 changes: 15 additions & 2 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const minimum = 1;
const maximum = 100;
const minimum = 1; // This variable shows the smallest number value possible is 1
const maximum = 100; // This variable shows the highest or max value for a random number is 100

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

console.log(`The random number is ${num}`);

// 1. Math.floor is a JavaScript function that will round down a number to the nearest whole number. Math.random() is similar but chooses any random number between 1 and 100 (as declared in the variables minimum and maximum).

// 2. What const num does is it stores the answer of of the expression. The expressions goal is to generate a random number between 1 and 100, rounds it down to the nearest whole number, then multiplies it by 100 (maximum 100 - minimum 1 + 1). Then another layer of complexitity is added by adding the result to 1.

// 3. When the code is run, it will generate a random number between 1 and 100, round it down to the nearest whole number, and then add 1 to it. This means that the final result will always be between 1 and 100, inclusive.


/* Instructions for the exercise:

// 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: 6 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
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?

// Oh, I see! We can use a comment to prevent the computer from running these lines. To do that in JavaScript you can use double forward slashes `//` to comment out a single line or `/* ... */` for multiple lines. It would look something like this:

/* 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? */
7 changes: 7 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@

const age = 33;
age = age + 1;

/* node shows an "Uncaught SyntaxError: Invalid or unexpected token" this is because using const means the value can’t be reassigned. For this to work, the correct version would be to use 'let'.

let age = 33;
age = age + 1;
console.log(age); // should print 34
*/
12 changes: 10 additions & 2 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// 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}`);
//const cityOfBirth = "Bolton";

/*The above shows shows 'ReferenceError: Cannot access 'cityOfBirth' before initialization
at Object.' This happens, according to Mozilla "when a lexical variable was accessed before it was initialized."

To access it, it has to be flipped like this: */

const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
20 changes: 18 additions & 2 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
//const cardNumber = 4533787178994213;
//const last4Digits = cardNumber.slice(-4);

/*Instructions */

// 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 answers:

1. My prediction on why the code isn't working is that it isn't being called or declared. Right now const stores the numbers but there is no console.log() to show the output

2. After running it shows "TypeError: cardNumber.slice is not a function"

3. 💔 Ah I see, my prediction was partially correct that it would not work but my reason and logic were wrong. What's different is that slice() only works on strings or arrays, not numbers. So to fix this, I. have to convert the number to a string first so I can slice it. See below a solution below:

*/

const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);
console.log(last4Digits); // expected to output "4213"
7 changes: 6 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";

//variable names need to start with a letter, underscore (_), or dollar sign ($). So the solution for the abobe will be

//const hourClock12 = "08:53";
//const hourClock24 = "20:53";
44 changes: 44 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,54 @@ 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

/* Answers 👀
There are 5 function calls

1. carPrice.replaceAll(",", "")
2. Number(carPrice.replaceAll(",", ""))
3. priceAfterOneYear.replaceAll("," "")
4. Number(priceAfterOneYear.replaceAll("," ""))
5. 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?

/* Answer 👀

Line 5

"SyntaxError: missing ) after argument list"
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));

correct bit should be to add a comma inbetween such as below:

replaceAll(",", "")
*/

// c) Identify all the lines that are variable reassignment statements

/* Answer 👀

Lines 4 and 5

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

2. priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
*/


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

/* Answer 👀

Lines 1, 2, 7, 8
*/


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

/*

It takes out the commas in the string "10,000" making it an integer/number "10000"

*/
12 changes: 12 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ console.log(result);

// a) How many variable declarations are there in this program?

//Answer: there are 6 of them (all 'const' in this context)

// b) How many function calls are there?

//Answer: 1 which is console.log(result);

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

//Answer, according to MDN, The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. In simpler words, it's called the modulus operator, it’s a math operator that tells you the remainder left over after division. This means movieLength % 60 = 24

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

//Answer: const movieLength is subtrated from const remainingSeconds. The answer to this is then divided by 60 and stored in const totalMinutes. Therefore, this answers the question of "How many whole minutes are in this movie?"

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

//Answer: it's the the total time of the movie in hours, minutes and seconds. Perhaps showTime.

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

// Yes, it could work for a majority of movie lengths. I tried and it gave me the answer 0:0:20 to one. But this could have a few skewed looking lengths like 2:4:20
34 changes: 34 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,37 @@ console.log(`£${pounds}.${pence}`);

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

/* Answer

1. const penceString = "399p": initialises a string variable with the value "399p"

2. const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);
-- This removes the "p" character from the string. Then substring(0, penceString.length - 1) takes everything except the last character
Result: "399"

3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
-- This is done to make sure the number has at least 3 digits, so it perhaps can be separated into pounds and pence later. For example if it were "5", it would become "005"

4. const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
-- This stores the pounds part.
---paddedPenceNumberString.length - 2 means everything except the last two digits.
--- Therefore this will go from "399" to 3 pounds and 99 pence

5.const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

--- Similar to the above, this capture the last two digits and turns them into pence. from "399" to 3 pounds and 99 pence

6. console.log(`£${pounds}.${pence}`);

--- This output then logs the combination of the pounds and pence into a string that looks like a real money amount. £3.99

*/
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?

⚡️there's a big popup showing hello world

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`?

⚡️ Prompt shows me a popup and asks me what my name is
⚡️ it showed my inputted name in the console
13 changes: 13 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ 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`
--- 'object'

Answer the following questions:

What does `console` store?

--- it stores diferent functions and tests things, from errors to informations to logs, warnings and beyond

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

--- console is the object. Log and assert are methods of declaring an object.
--- The console.assert() writes an error message to the console if the assertion is false.
--- The console.log() it outputs a message to the console.
--- The "." accesses a method or property inside an object
Loading