Skip to content

Glasgow | ITP May '25 | Mirabelle Morah | Module-Structuring-and-Testing-Data | Sprint 2 #606

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 4 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
15 changes: 13 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here

/* 💡Answer: To be honest, I get confused when I see template literals and understand the older version of writing JS better, using operators, but I am dedicated to trying my best in this. I see a lot of the use of 'str' in this and I suspect this is almost correct but may fail because of the variable name 'str' used one too many times, as a parameter and a variable. I know that slice deletes parts of a string and 'toUpperCase' could be to capitalise things. */

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> What I get is a "SyntaxError: Identifier 'str' has already been declared". This means that in line 10 of my code, I have declared the variable 'str' twice, once as a parameter and once as a variable. This is not allowed in JavaScript, as each variable must have a unique name within its scope. To fix this, I need to remove the 'let' keyword from the second declaration of 'str', so that I am simply reassigning the value to the existing parameter instead of trying to declare it again.

// =============> write your explanation here
// =============> write your new code here

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

console.log(capitalise("wheew! Done it without template literals :)")); // Should print "Wheew! Done it without template literals :)"
14 changes: 12 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> 💡Answer: Same issue as in the firts one, you're redeclaring a parameter. What we can do is to assign the same parameter with new value.

// Try playing computer with the example to work out what is going on

/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +14,17 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

// =============> write your explanation here
// =============> it is giving me a "SyntaxError: Identifier 'decimalNumber' has already been declared" error. This is because I am trying to declare the variable 'decimalNumber' again inside the function, but it has already been declared as a parameter. In JavaScript, you cannot declare a variable with the same name in the same scope.
// To fix this, I need to remove the 'const' keyword from the second declaration.

// Finally, correct the code to fix the problem
// =============> write your new code here

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

console.log(convertToPercentage(0.5)); // Output: "50%"
17 changes: 10 additions & 7 deletions Sprint-2/1-key-errors/2.js

Choose a reason for hiding this comment

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

Another way to see this is also that in the wrong syntax example, we are passing the argument to the function when declaring the function. Instead we pass arguments to functions when we call the function, and pass parameters to function when we declare a function. This is a small but important distinction specifically when discussing code with other developers!

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 didn't know, thank you for explaining to me. I'll keep learning

Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@

// Predict and explain first BEFORE you run any code...

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> 💡Answer: Mhmm there seems to be an error in line 8 because you can't use a number/integer alone as the start name of a parameter. It should be more of a name or string or add the number at the end of the value/name

function square(3) {
/* function square(3) {
return num * num;
}
*/

// =============> write the error message here
// =============> write the error message here: "SyntaxError: Unexpected number" and "ReferenceError: num is not defined"

// =============> explain this error message here
// =============> explain this error message here: Using a number alone is not a valid identifier name in JavaScript. Identifiers must start with a letter, underscore (_), or dollar sign ($). Also , the variable `num` is not defined anywhere in the function, which leads to a ReferenceError when trying to use it.

// Finally, correct the code to fix the problem

// =============> write your new code here

// =============>
function square(Now3) {
return Now3 * Now3;
}

console.log(square(3)); // Output: 9
18 changes: 13 additions & 5 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

// =============> write your prediction here
// =============> Answer 💡: does not have a return statement, so it will return undefined

function multiply(a, b) {
/*function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); */

// =============> write your explanation here
// =============> write your explanation here: The code shows the message
// "320 The result of multiplying 10 and 32 is undefined"
// It is because the multiply function does not return a value, it only logs the result to the console.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>

function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // logs "The result of multiplying 10 and 32 is 320"
15 changes: 11 additions & 4 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: Answer 💡: looks like it will fail because of the way return is written

function sum(a, b) {
/*function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
*/

// =============> write your explanation here
// =============> write your explanation here: The error occurs because the return statement is used incorrectly. It has a semicolon after it, which causes the function to return undefined immediately, and the line `a + b;` is never executed.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // logs "The sum of 10 and 32 is 42"
24 changes: 20 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js

Choose a reason for hiding this comment

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

Again if we go back to the above comment, this function is missing a parameter, thus passing an argument to it will simply be ignored. In other programming languages passing an argument to a function which does not have parameters will throw an exception.

Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here -- Answer 💡: no parameter for function getLastDigit()

const num = 103;
/*const num = 103;

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

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`); */

// Now run the code and compare the output to your prediction
// =============> write the output here
//
/* -- because there's no parameter, the console logs teh following for all the function calls
The last digit of 42 is 3
The last digit of 105 is 3
The last digit of 806 is 3*/

// Explain why the output is the way it is
// =============> write your explanation here
// =============> This is as a result of the function always using the constant variable num, outside the function, which is set to 103, to get its answer.
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

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

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// The function now takes a parameter 'number' and uses it to get the last digit of the number passed to it. This is because .slice(-1) is used to get the last character of the string representation of the number.
8 changes: 6 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// return the BMI of someone based off their weight and height
const bmi = weight / (height * height);
return bmi.toFixed(1); // rounds to 1 decimal place & returns a string
}

console.log(`The BMI is ${calculateBMI(70, 1.51)}`); // logs "The BMI is 30.7"
6 changes: 6 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

function toUpperSnakeCase(str) {
return str.toUpperCase().replace(/ /g, "_");
}

console.log(toUpperSnakeCase("what a lovely day")); // Output: "WHAT_A_LOVELY_DAY"
24 changes: 24 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,27 @@
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs

function toPounds(penceString) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

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

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

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

return `£${pounds}.${pence}`;
}

console.log(toPounds("399p")); // Output: £3.99
console.log(toPounds("99p")); // Output: £0.99
console.log(toPounds("1p")); // Output: £0.01
12 changes: 7 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61)); // Output: 00:01:01

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> the answer is 0 (that is 00:01:01)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 00

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> 1

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> 01, because it adds a zero at the front to keep it within the HH:MM:SS format (that is 00:01:01)