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
35 changes: 22 additions & 13 deletions Sprint-3/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

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

// =============> write your explanation here
// =============> write your new code here
// Predict and explain first...
// I am predicting the code will generate 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

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

// After we run the code, it generated this error SyntaxError: Identifier str has already been declared
// This happened because we tried to declare again using the keyword let the variable str
// which was declared before in the parameter of the function

//Here is the fixed code
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello World"));
52 changes: 32 additions & 20 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// Predict and explain first...

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

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

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

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

// Why will an error occur when this program runs?
// I predict the code will generate 2 errors because one the variable decimalNumber is declared twice and two
// the function is not called instead the variable decimalNumber is called and it can't be accessed because
// it was declared locally as a parameter of the 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);*/

// After we run the code, it generated an error SyntaxError: Identifier 'decimalNumber' has already been declared
// because we tried to declare decimalNumber again. After fixing this error again we got another error ReferenceError: decimalNumber is not defined
// because the decimalNumber is locally scoped, javascript can't access it when the function is called
// additionally the function was not called console.log(decimalNumber) this simply means print the value of decimalNumber

// Finally, correct the code to fix the problem
// Here is the fixed code
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.5));
41 changes: 21 additions & 20 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

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

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

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

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

// Finally, correct the code to fix the problem

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


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

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

// I predict the error will come from the argument because when we write a function we first declare parameters not values

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

// SyntaxError: Unexpected number

// this error tells us javascript expects valid variable identifiers when function is defined not values

// Finally, correct the code to fix the problem

// Here is the fixed code
function square(num) {
return num * num;
}
console.log(square(3));
36 changes: 22 additions & 14 deletions Sprint-3/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

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

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

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

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

// I predict the code will run normally and print the result of a * b on the first console, but it will return undefined on the
// second console.log

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

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

// The reason for printing undefined is that we didn't define a return value so the js doesn't know what to return and it simply returns undefined
// We can fix the code by replacing the console.log with a return keyword so the js knows what to return when the function is called

// Finally, correct the code to fix the problem
// Here is the fixed code

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

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
34 changes: 21 additions & 13 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here

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

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

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
// Predict and explain first...
// I predict that the code will run but it will print undefined because the return expression is not setup properly

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

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

// When we run the code it printed out undefined because the return keyword is there but the expression that needs to be returned is not on the same line
// therefore the return keyword can't reach the expression and the expression is ignored because the function has done execution right after it reaches the return keyword

// Finally, correct the code to fix the problem
// Here is the fixed code

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
60 changes: 36 additions & 24 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
// Predict and explain first...

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

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

// 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
// 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
// Predict and explain first...

// Predict the output of the following code:
// I predict the code will run but will give us unexpected output as the function does not have parameter but we provided an argument

/*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)}`);*/

// Now run the code and compare the output to your prediction
// 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
// It is because we declared a function with no parameters so the function will ignore any arguments we provide
// since the var num is declared and assigned with a value so every time the function is called it will use the var num

// Finally, correct the code to fix the problem
// Here is the fixed code
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
42 changes: 23 additions & 19 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// 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.

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

// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// 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.

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

// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const BMI = weight / height ** 2;
const stringBMI = BMI.toString();
return stringBMI.slice(0, stringBMI.indexOf(".") + 2);
}

console.log(calculateBMI(70, 1.73));
36 changes: 20 additions & 16 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// A set of words can be grouped together in different cases.

// For example, "hello there" in snake case would be written "hello_there"
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.

// Implement a function that:

// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

// 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
// A set of words can be grouped together in different cases.

// For example, "hello there" in snake case would be written "hello_there"
// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces.

// Implement a function that:

// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

// 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 uppercaseAndUnderscore(word) {
return word.toUpperCase().replaceAll(" ", "_");
}
console.log(uppercaseAndUnderscore("welcome to cyf"));
31 changes: 25 additions & 6 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// 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
// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// 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) {
if (penceString == "") {
return "please put the amount";
}
const totalPence = parseInt(penceString, 10);
const pounds = totalPence / 100;
const formattedPound = pounds.toFixed(2);
return "£" + formattedPound;
}

console.log(toPounds("1991p"));
console.log(toPounds("2020p"));
console.log(toPounds("399p"));
console.log(toPounds("69p"));
console.log(toPounds("00p "));
console.log(toPounds(" 50p "));
console.log(toPounds("232423"));
console.log(toPounds(""));
Loading
Loading