Skip to content
Open
19 changes: 14 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@

// Predict and explain first...
// =============> write your prediction here
// =============> I predict that we will not need to write let in line 8 as str is already declared
// as an argument within the function captialise.

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

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

// capitalise ('hello');
//==============> A syntaxError occurred as 'str' is being cleared.


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

// =============> write your explanation here
// =============> write your new code here
console.log (capitalise ('hello'));
29 changes: 21 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@

// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> I believe that the function convertToPercentage is not mentioned in the console.log statement.
// Declaring the const decimalNumber is unnecessary as it is already declared as a parameter within the function convertToPercentage.

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

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

return percentage;
}
// return percentage;
//}

console.log(decimalNumber);
// console.log(decimalNumber);

// =============> write your explanation here
// =============> A syntaxError occurred saying decimalNumber is already declared.

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

function convertToPercentage(num) {

const percentage = `${num * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.1));


20 changes: 13 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@


// 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
// =============> The num parameter should be mentioned inside the function square instead of the number 3.
// then it should be called as square (3) in the console.log statement.

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

// =============> function square(3) syntaxError: Unexpected number.
// =============> 3 wasn't expected... instead a declaration like 'num' was expected.

// =============> write the error message here

// =============> explain this error message here

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


console.log(square(3));
19 changes: 13 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@

// Predict and explain first...

// =============> write your prediction here
// =============> In line 6, instead of console.log printing the result of multiply(10, 32), return a*b would be better
//because the console.log would only print a*b.

function multiply(a, b) {
console.log(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
// =============> After running the code, we got 'the result of multiplying 10 and 32 is undefined'
// as the multiply parameters weren't considered as parameters inside the function multiply.

// 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)}`);
8 changes: 4 additions & 4 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Predict and explain first...
// =============> write your prediction here
// =============> SyntaxError will occur due to putting a semicolon after return.

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

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

// =============> write your explanation here
// =============> unlike the expectation that the code runs without error as 'the sum of 10 and 32 is undefined'.
// Finally, correct the code to fix the problem
// =============> write your new code here
// we needed just to remove the semicolon after return statement and to bring up the a+b expression side by side with return.
32 changes: 21 additions & 11 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> The code will return 3 as a string first, but after calling the getLastDigit function,
// it will not return anything because every number based will be converted to string.

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

function getLastDigit() {
return num.toString().slice(-1);
}
//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 42 is ${getLastDigit(42)}`);
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
//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
// Explain why the output is the way it is
// =============> write your explanation here
// =============> The last digit of 42 is 3 three times.
// the prediction was close but not accurate, the case is that the parameter num needs to be
//the function instead of const 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
// parameter num needs to be with the getLastDigit function instead of const num=103.
8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {

const bmi = weight / (weight*height);
return bmi.toFixed(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of value do you expect your function to return? A number or a string?
Does your function return the type of value you expect?

Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example,

  console.log(123);              // Output 123
  console.log("123");            // Output 123
  
  // Treated differently in the program
  let sum1 = 123 + 100;         // Evaluate to 223 -- a number
  let sum 2 = "123" + 100;      // Evaluate to "123100" -- a string.

Copy link
Author

Choose a reason for hiding this comment

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

I expect the function will return a number and it does return a number.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you look up how to check if a value is a number in JavaScript, and then apply the technique to verify your expectation?

// return the BMI of someone based off their weight and height
}
}

console.log(`the BMI is ${calculateBMI(80, 1.8)}`);
//I expect the function will return a number and it does return a number.
13 changes: 13 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// A set of words can be grouped together in different cases.

// For example, "hello there" in snake case would be written "hello_there"
Expand All @@ -14,3 +15,15 @@
// 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 capSnakeCase(str) {
const upper = str.toUpperCase();
const snake = upper.replace(/ /g, "_");
return snake;
}
console.log(capSnakeCase("lord of the rings"));

// step 1: get a string input
//step 2: change the upper case
//step 3: replace spaces with underscores
// step 4: return capSnakeCase
12 changes: 12 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,15 @@
// 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"))
console.log(toPounds("5p"))
console.log(toPounds("5678p"))
13 changes: 7 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

function pad(num) {
return num.toString().padStart(2, "0");
}
Expand All @@ -10,25 +11,25 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));
// 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
// =============> It will be called 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
// =============> 0

// 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
// =============> The leftover second is assigned to num as remainingSEconds 61%60

// 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 the remainingSeconds from passed from pad to num is changed to string and padded as 01
50 changes: 50 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// 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.
Expand All @@ -23,3 +24,52 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAs12HourClock("13:00");
const targetOutput5 = "01:00 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);



// modified code:

function formatAs12HourClock(time) {
let hours = Number(time.slice(0, 2));
const minutes = time.slice(3);

let suffix;
if (hours >= 12) {
suffix = "pm";

} else {
suffix = "am";
}
hours = hours % 12 || 12; // Convert 0 to 12, 13 to 1
const formattedHours = hours.toString().padStart(2, '0');

return `${formattedHours}:${minutes} ${suffix}`;
}

const currentOutput6 = formatAs12HourClock("23:00");
const targetOutput6 = "11:00 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);
Empty file added median.js
Empty file.
Empty file added median.test.js
Empty file.
Loading