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

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

//function capitalise(str) { //function already declares a parameter named str.
//let str = `${str[0].toUpperCase()}${str.slice(1)}`; //to declare another variable called str using let, which is not allowed
// return str;
//}


// here is the new code

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
let result = `${str[0].toUpperCase()}${str.slice(1)}`; //Taking the first character of the string and converts it to uppercase and rest of the string, starting from index 1.
return result;
}

// =============> write your explanation here
// =============> write your new code here
19 changes: 16 additions & 3 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

//variable decimalNumber is already declared, we are trying to declare again
// console.log will throw an error since decimal no is not declared outside function

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

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

return percentage;
}

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

// =============> write your explanation here

// function call is required and value for the variable 'decimalNumber' should be passed through the function


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

19 changes: 13 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@

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

// =============> write your prediction of the error here
// Variable should be declared as "num" and later value should be passed to "num" > write your prediction of the error here

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

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

// =============> explain this error message here
// can’t put a number (3) as a parameter name.
//Function parameters must be identifiers (like num, x, or value), not literal values.
//returning num * num, but num is never defined> 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));



15 changes: 10 additions & 5 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// console.log is used to print the output, but here it is used in the function call, which is not the correct approach> write your prediction here

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
// We need to return the value instead of just logging it inside the function.> write your explanation here

// 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)}`);
16 changes: 11 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// with return we should give some value that comes in the output> write your prediction here

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

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

// =============> write your explanation here
// a+b should be used after return in line 5> write your explanation here
// 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)}`);
30 changes: 22 additions & 8 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// console.log should be return only 103> Write your prediction here

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
/*The last digit of 42 is 3
The last digit of 105 is 3
The last digit of 806 is 3> write the output here*/
// Explain why the output is the way it is
// =============> write your explanation here
// because we have provided a constant value 103 to num> write your explanation here
// 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(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: yes
/* here is the new output
The last digit of 42 is 2
The last digit of 105 is 5
The last digit of 806 is 6*/
// now I have defined a function that takes one argument, num. also removed const funtion
6 changes: 4 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,7 @@
// 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
}
let bmi = weight / (height * height);
return bmi.toFixed(1);
}
console.log(calculateBMI(70, 1.68));
4 changes: 4 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,7 @@
// 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

const sentence = "hello there";

console.log(sentence.toUpperCase().replace(/ /g, "_"));
27 changes: 27 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,30 @@
// 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) {
// Remove the trailing 'p'
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// Make sure it's at least 3 digits (e.g., '9' -> '009')
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Split into pounds and pence parts
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")); // £3.99
console.log(toPounds("9p")); // £0.09
console.log(toPounds("50p")); // £0.50
console.log(toPounds("1234p")); // £12.34
console.log(toPounds("7p")); // £0.07
13 changes: 8 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,28 @@ 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
// 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
// when pad function is called 3rd time pad(remainingSeconds) -> remainingSeconds = 1
// 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
// when pad function is called 3rd time pad(remainingSeconds) -> remainingSeconds = 1, but after padding it becomes 01 and returns it
// 01
19 changes: 19 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
// Morning times
console.assert(formatAs12HourClock("01:00") === "01:00 am");
console.assert(formatAs12HourClock("11:30") === "11:30 am");

// Noon
console.assert(formatAs12HourClock("12:00") === "12:00 pm");

// Afternoon
console.assert(formatAs12HourClock("13:15") === "01:15 pm");
console.assert(formatAs12HourClock("15:45") === "03:45 pm");

// Evening
console.assert(formatAs12HourClock("23:59") === "11:59 pm");

// Midnight
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("00:30") === "12:30 am");

console.log(" All tests passed!");