Skip to content

ITP_GLASGOW_MAR | HANNA_MYKYTIUK | MODULE_STRUCTURING_AND_TESTING_DATA | SPRINT_2 #441

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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ function capitalise(str) {
}

// =============> write your explanation here
// =============> write your new code here
//Variable str is passed as parametr in line 7. There is no need to create variable str again in line
// =============> write your new code here
// line 8: str = `${str[0].toUpperCase()}${str.slice(1)}`;
14 changes: 12 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Predict and explain first...

// the code won't work
// Why will an error occur when this program runs?
// =============> write your prediction here

// because of variable decimaNumber is already declared. And out of range of conlole function
// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
Expand All @@ -18,3 +18,13 @@ console.log(decimalNumber);

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

function convertToPercentage(decimalNumber) {
decimalNumber = 0.5;
Copy link

Choose a reason for hiding this comment

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

If we replace the parameter value, how do we expect to convert different numbers using this function?

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

console.log(decimalNumber);
Copy link

Choose a reason for hiding this comment

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

I think the function is expected to compute and return a percentage string when given a number; it should not output 0.5 every time the function is called.

If we want to output 0.25 as 25%, we could write console.log( convertToPercentage(0.25) ); outside the function.

return percentage;
}


10 changes: 7 additions & 3 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

//function expect num variable and gets 3 of number.
function square(3) {
return num * num;
}

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

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

//function expect num variable and gets 3 of number.
// Finally, correct the code to fix the problem

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


8 changes: 8 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,11 @@ 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)}`);



9 changes: 8 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> write your explanation here: incorrect return statment
// 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)}`)
36 changes: 26 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
// Predict and explain first...

//it won't work
// Predict the output of the following code:
// =============> Write your prediction here
// I've just checked
//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
// =============> 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
// Explain why the output is the way it is
// =============> write your explanation here
// because of const num=103
// 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.



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

// Explain why getLastDigit is not working properly - correct the problem
//function getLastDigit(num) - should be like that
6 changes: 5 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,9 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
let BMI = weight/(height*height);
return BMI.toFixed(1);

Copy link

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 think the function should return? A number or a string?
Does your function return the type of value you think it should return?

// return the BMI of someone based off their weight and height
}
}
console.log(calculateBMI(62.99,1.64));
6 changes: 6 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,9 @@
// 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 to_upper_snacke_case(str){
str = str.replaceAll(" ", "_");
str = str.toUpperCase();
return str;
}
console.log(to_upper_snacke_case("lord of the rings"));
31 changes: 31 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,34 @@
// 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";

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

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



}
toPounds("4896p");






11 changes: 6 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +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
// =============> write your answer here: tree 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
// =============> write your answer here: 00:01:01
Copy link

Choose a reason for hiding this comment

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

I think you misread the question. That's not the value assigned to num when the function pad() is called for the first time.


// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here: '00' - string of full hours

// 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
// =============> write your answer here: 1 - Number of seconds remainder

// 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
// =============> write your answer here: '01' - String of seconds remainder
36 changes: 35 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(-2);
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${hours - 12}:${minutes} pm`;
}
return `${time} am`;
}
Expand All @@ -23,3 +24,36 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

//////////////
// hours > 12 and minutes NOT 00
const currentOutput3 = formatAs12HourClock("23:45");
const targetOutput3 = "11:45 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

// hours < 12 and minutes NOT 00
const currentOutput4 = formatAs12HourClock("09:13");
const targetOutput4 = "09:13 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

// hours = 00 and minutes = 00
const currentOutput5 = formatAs12HourClock("00:00");
Copy link

Choose a reason for hiding this comment

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

I think "00:00" should be converted to "12:00 am" (midnight).

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

// hours = 12 and minutes = 00
const currentOutput6 = formatAs12HourClock("12:00");
const targetOutput6 = "00:00 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);
Copy link

Choose a reason for hiding this comment

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

I think "12:00" should be converted to "12:00 pm" (noon).

13 changes: 9 additions & 4 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle < 90) return "Acute angle";
if (angle >90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle >180 && angle < 360) return "Reflex angle";
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +46,16 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse,"Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(200);
assertEquals(reflex, "Reflex angle");
10 changes: 8 additions & 2 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
}
if (numerator < denominator) {
return true;
} else if ( numerator >= denominator) {
return false;
}
}

// here's our helper again
function assertEquals(actualOutput, targetOutput) {
Expand Down Expand Up @@ -40,13 +44,15 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction,true );
// ====> complete with your assertion

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);
// ====> complete with your assertion

// Stretch:
Expand Down
30 changes: 28 additions & 2 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
if (rank === "A") return 11;
let rank;
if (card === "10"){
rank = card;
} else {
rank = card.charAt(0);
}

if (rank === "A") {
return 11;
} else if (Number(rank) >= 2 && Number(rank) <= 9) {
return Number(rank);
} else if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
throw new Error("Invalid card rank");
}
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +48,30 @@ assertEquals(aceofSpades, 11);
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(fiveofHearts, 5);

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const faceCard = getCardValue("10");
assertEquals(faceCard, 10);

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const handleAce = getCardValue("A");
assertEquals(handleAce, 11);

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
const invalidCard = getCardValue("g");
assertEquals(invalidCard, "Invalid card rank.");
} catch(error){
console.log(error.message);
}

Loading