Skip to content

Manchester | 25-ITP-May | Jennifer Isidienu | Sprint 2 | Acoursework #687

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 2 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
10 changes: 9 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here
// The code below is expected to show an error because the variable 'str' is declared twice.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -10,4 +11,11 @@ function capitalise(str) {
}

// =============> write your explanation here
// The error occurs because the variable 'str' is declared with 'let' twice in the same scope.
// The first declaration is a placeholder value, and the second is a variable declaration inside the function.
// =============> write your new code here
// To fix this, we can remove the 'let' keyword from the second declaration of 'str'.
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
15 changes: 14 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// An error will occur because the variable `decimalNumber` is declared twice,
// which is not allowed in JavaScript.

// 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 @@ -15,6 +16,18 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// The error occurs because the variable 'decimalNumber' is declared again
// inside the function 'convertToPercentage', although it has already been
// defined as a parameter. JavaScript does not allow redeclaring a parameter
// using const, let or var within the same scope. This causes a syntax error
// when the code is executed.

// 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));
20 changes: 17 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@

// 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
// It will show an error because function parameters must be variable names,
// not literal values.

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

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// The error message "SyntaxError: Unexpected number" indicates that
// the JavaScript interpreter encountered a number instead of a valid
// identifier like a variable name.
// In function declarations, parameters must be variable names and not
// literal values like 3. That is why using function square(3) caused a syntax error.

// Finally, correct the code to fix the problem

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

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

console.log(square(3));

13 changes: 13 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...

// =============> write your prediction here
//The code will be undefined because the "console.log(a * b)" function
// will not return a value, rather it will only log a result.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +11,17 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The `multiply` function is designed to log the product of `a` and `b`
// to the console, but it does not return any value.
// When `multiply(10, 32)` is used inside the template string,
// it shows `undefined` because the function does not return anything.
// As a result, the console will first display 320 from inside the function,
// and then print; "The result of multiplying 10 and 32 is undefined".

// 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)}`);
10 changes: 10 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
/// The code is expected to return undefined because the return statement
// is placed before the actual addition operation. The function `sum` is
// defined to take two parameters, `a` and `b`, but it does not return their
// sum due to the misplaced semicolon after the `return` statement.

// 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)}`);
18 changes: 18 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,28 @@ 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
// 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
// The function `getLastDigit` does not accept any arguments.
// It always uses the constant `num` defined at the top, which is 103.
// Therefore, no matter what number is passed in the `console.log`,
// the function always returns the last digit of 103 ("3").
// `getLastDigit` passes a value that is ignored since the function
// takes no parameters and always uses the fixed variable num = 103.

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(num) {
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)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
17 changes: 12 additions & 5 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
// Below are the steps for how BMI is calculated

// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
// The BMI calculation divides an adult's weight in kilograms (kg)
// by their height in metres (m) squared.

// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
// For example, if you weigh 70kg (around 11 stone) and are 1.73m
// (around 5 feet 8 inches) tall, you work out your BMI by:

// squaring your height: 1.73 x 1.73 = 2.99
// dividing 70 by 2.99 = 23.41
// Your result will be displayed to 1 decimal place, for example 23.4.

// You will need to implement a function that calculates the BMI of someone based off their weight and height
// You will need to implement a function that calculates the BMI of someone
// based off their weight and height

// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// 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 Number(BMI.toFixed(1));
}

console.log(calculateBMI(70, 1.73));
7 changes: 7 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,10 @@
// 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(input) {
return input.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
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

// This function converts a string like "399p" into a formatted pounds and pence amount like "£3.99"

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);

return `£${pounds}.${pence}`;
}
console.log(toPounds("399p"));
console.log(toPounds("99p"));
console.log(toPounds("5p"));
console.log(toPounds("1399p"));
console.log(toPounds("0p"));
31 changes: 26 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,45 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// 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?
// a) When formatTimeDisplay is called how many
// times will pad be called?
// =============> write your answer here
// 3 times. It is called once for hours,
// once for minutes and once for seconds.

// 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?
// b) What is the value assigned to num when
// pad is called for the first time?
// =============> write your answer here
// The value assigned to num when pad is
// called for the first time is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when called for the first time is "00".
// This is because it pads the number 0 with leading zeros to
// ensure it has a length of 2 characters.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// 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
// The value assigned to num when pad is called for the last time is 1.
// This is because the last call to pad is for the remaining seconds
// which is 61 % 60 = 1.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// 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
// The return value assigned to num when pad is called for
// the last time is "01".
// This is because it applies .padStart(2, "0"), which returns
// a two character string, with a leading zero to ensure
// it has a minimum length of 2.
28 changes: 27 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This is the latest solution to the problem from the prep.
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
// Your task is to write tests for as many different groups of
// input data or edge cases as you can, and fix any bugs you find.


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
Expand All @@ -23,3 +25,27 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

//
function formatAs12HourClock(time) {
const [hourStr, minutes] = time.split(":");
const hours = Number(hourStr);

if (hours === 0) {
return `12:${minutes} am`; // Midnight
}
if (hours === 12) {
return `12:${minutes} pm`; // Noon
}
if (hours > 12) {
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
}
return `${time} am`; // Morning
}

// Test cases
console.log(formatAs12HourClock("08:00"));
console.log(formatAs12HourClock("23:00"));
console.log(formatAs12HourClock("00:30"));
console.log(formatAs12HourClock("12:45"));
console.log(formatAs12HourClock("13:15"));