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
13 changes: 10 additions & 3 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// It would fail at line 2 with either syntax or reference errors =============> write your prediction here

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// 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;
}
console.log(capitalise("beautiful"));

// =============> write your explanation here
// function is assigned 2 arguments/values=============> write your explanation here
// =============> write your new code here

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("beautiful"));
16 changes: 13 additions & 3 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// decimalNumber is called twice=============> write your prediction here

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

Expand All @@ -14,7 +14,17 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// decimalNumber has been declared as a function and a variable within function scope=============> write your explanation here
//Removing const decimalNumber still returns Syntax error if still within the function scope so it's not restricted to the function

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

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

return percentage;
}
const decimalNumber = 0.5;

console.log(decimalNumber);
18 changes: 10 additions & 8 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

// 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
// Unidentified function error due to a random number =============> write your prediction of the error here

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

// =============> write the error message here
// Syntax errorError Unexpected number.=============> write the error message here

// =============> explain this error message here
// Means Function identifier should be a name not a number =============> explain this error message here.

// Finally, correct the code to fix the problem

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


function square(num) {
return (square = num * num);
}
console.log(square(3));
12 changes: 9 additions & 3 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// Predict and explain first....

// =============> write your prediction here
// Code will fail because a , b are not declared values or double function values=============> write your prediction here

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

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

//The 2nd console log returned value value is undefined. I have added a return function to output the multiply value.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
console.log(a * b);
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
9 changes: 7 additions & 2 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// Predict and explain first....
// =============> write your prediction here
// I think code will run without an error although not entirely sure what the empty return; will return

function sum(a, b) {
return;
Expand All @@ -8,6 +9,10 @@ function sum(a, b) {

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

// =============> write your explanation here
// Maybe that unassigned return; may cause the code not to run fully =============> 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)}`);
18 changes: 14 additions & 4 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@

// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
//I think const being declared before function may cause a problem on returning num value (line 10)

function getLastDigit() {
const num = 103;
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)}`);

// Now run the code and compare the output to your prediction
// Now run the code and compare the output to your prediction.
// =============> write the output here
// My prediction was close regarding line 10 as the value of num is not returned.
// Explain why the output is the way it is
// =============> write your explanation here
// The code has returned 3 as the last digit for all provided numbers because num value is restricted to 103 as value and can't be used outside the function
// Also function getLastDigit() has no parameter set
// 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
// getLastDigit was no working as variable num was taking precedence
6 changes: 6 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}

function calculateBMI(weight, height) {
return (weight / height).toFixed(1);
}
console.log(`the bmi of 70 and 2.99 is ${calculateBMI(70, 2.99)}`);
console.log(`the bmi of 90 and 1.96 is ${calculateBMI(90, 1.96)}`);
7 changes: 7 additions & 0 deletions Sprint-3/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(text) {
return text.trim().replace(/\s+/g, "_").toUpperCase();
}

const result = toUpperSnakeCase("Hello there");
console.log(result);
42 changes: 42 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,45 @@
// 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

/* const penceString = "399p";

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

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

// Revised code:
function poundsToPence(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(poundsToPence("499p"));
console.log(poundsToPence("1009p"));
console.log(poundsToPence("2999p"));
5 changes: 5 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) {

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

// 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 when it is called for the first time?
// =============> write your answer here
0;

// 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 is 1 because the last pad argument is remainingSeconds. So 61 as the given seconds - remaining seconds = 1

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// The answer is 01 because of the final output for formatTimeDisplay(61) is 00:01:01. The assigned value of 1 with the length of 1, the numString = "0" + numString becomes 1
Loading