Skip to content

Structure sprint 2 #620

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 14 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
13 changes: 10 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// Predict and explain first... i predicted it was going to be an error
// =============> write your prediction here

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

//str as a parameter to the function. Then you're trying to redeclare it inside the function with let str = ...,
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
//str is already declared with function, trying to redeclare it inside the function with let str is not accepted in javascript
// =============> write your new code here
// function capitalise(str)
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello"));
12 changes: 9 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...

// SyntaxError message because the 'decimalNumber' has already been declared by function,
// Declaring const decimalNumber again inside the function is not accepted by javascript
// Why will an error occur when this program runs?
// =============> write your prediction here

// SyntaxError: Identifier 'decimalNumber' has already been declared.
// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
Expand All @@ -15,6 +16,11 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

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

// The function convertToPercentage takes a parameter decimalNumber, Multiply it by 100 and result should be in "%"
// 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));
14 changes: 9 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// SyntaxError due to wrong variable

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

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

// SyntaxError: Unexpected token
// =============> explain this error message here

// Function parameters must be valid identifiers names like num, not literal values.
// Finally, correct the code to fix the problem

// function square(num)
// =============> write your new code here

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

4 changes: 4 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// 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)}`);
7 changes: 7 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ 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
// The return statement ends before 'a + b' because JavaScript automatically inserts a semicolon after return
// =============> write your new code here
function sum(a, b) {
return a + b;
}

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

// my prediction is that the output will be the last digit of the number 103, because the const num is declared at the top
// Predict the output of the following code:
// 3 should be the output
// =============> Write your prediction here

3
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
function getLastDigit() {}
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// The const n= 103 declared first makes every other parameter under it invalid
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(num) {
return num.toString().slice(-1);}
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// variable (num) is defined in the function getLastDigit()
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)}`);
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 @@ -16,4 +16,10 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}

const heightSquared = height * height;
const rawBMI = weight / heightSquared;
return Number(rawBMI.toFixed(1));
}
console.log(calculateBMI(70, 1.73)); // 23.4

5 changes: 5 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,8 @@
// 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 strToUppersnakeCase(str) {
return str.toUpperCase().replace(/\s+/g, "_");
}
console.log(strToUppersnakeCase("hello there"));
console.log(strToUppersnakeCase("lord of the rings"));
18 changes: 18 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
// In Sprint-1, there is a program written in interpret/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) {
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")); // £3.99
console.log(toPounds("99p")); // £0.99
console.log(toPounds("9p")) // £0.09
12 changes: 7 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// 3 times,`${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
// 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

// num = 0 — from pad(totalHours)
// c) What is the return value of pad is called for the first time?
// =============> write your answer here

// "00" — pad(0) → "0".padStart(2, "0") → "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

// num = 1 — from pad(remainingSeconds), which is the third and last call.
// 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" — because 1.toString() is "1" and .padStart(2, "0") → "01"

Loading