Skip to content

Glasgow | ITP May -25 | Pandi Simatupang | Module-Structuring-and-Testing-Data | coursework/sprint-2 #650

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 11 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
12 changes: 10 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
//the function will turn the first given string into Uppercase.

Choose a reason for hiding this comment

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

Suggested change
//the function will turn the first given string into Uppercase.
//the function will turn the first character of a given string into Uppercase.


// 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;
let strWithUpperCase = `${str[0].toUpperCase()}${str.slice(1)}`;
return strWithUpperCase;
}

// =============> write your explanation here
// str[0].toUpperCase() ---> gets the first character and uses build-in method to convert a char to uppercase
// str.slice(1) ----> will slice the array or sting "start from 1" to the rest of array or string
// we get an error because we "re-declare" the str variable as parameter and as call back of expression

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

const myStr = "i forgot add this string";
console.log(capitalise(myStr));
26 changes: 20 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// decimalNumber variable was declared twice. First as parameter and second as const in function body
// use different variable name, either in parameter or the one in function body.
// any way this function always return the same value because the variable is constant and define inside th function,
// usually a function take a variable from function parameters.
// console.log() will fail to print because no parameter define first.

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

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

return percentage;
}
// return percentage;
// }

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

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

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
//-----> should be place as parameter and not as a constant ,,,,,const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

decimalNumber = 0.27;
console.log(convertToPercentage(decimalNumber));
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/2.js

Choose a reason for hiding this comment

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

For information/reference, when declaring a function we call these parameters, when we call the function we call them arguments. It is a subtle distinction but is important to remember especially when communicating with other developers.

Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

// 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
//the function will return square for every given number.

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

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

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

// ans: function need num to be defined, instead parameter was given 3 as literal value that does not point to any variable as parameter
// Finally, correct the code to fix the problem

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

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

mynum = 3; //define the num

console.log(square(mynum)); //verify the result
18 changes: 12 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// Predict and explain first...

// =============> write your prediction here
// function will return with result of multiplication of given parameters (i,e: a times b)
// function multiply(a, b) {
// console.log(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

// the function does not return any thing, so as result ${multiply(10, 32)} --> will return as "undefined"
// 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)}`);
20 changes: 15 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// sure this function wont work because, hang on.. I should predict and pretend that everything is fine
// the function will return with summary of given parameters and console.log will verify it

function sum(a, b) {
return;
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
//function will execute line the code and escape from the function block whenever it "sees" return.. it will return with any expression put on it
// in this function a+b which is expected as the result is placed after return line because return has ";" so i wont bother look the next line.

Choose a reason for hiding this comment

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

It is important to note that ; [semicolon] is not needed in this case for the function to return when it reaches the return statement. New line is also a valid way to end the line, thus the function will not execute a + b even if we were missing a ;.

// just remove the semicolon next to the return.. and put a+b instead, because some how "prettier" as trusty worthy formatter for this course will always add extra ; for every logical expression (somehow).. :) xixixixi
// 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)}`);
33 changes: 26 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@

// Predict the output of the following code:
// =============> Write your prediction here
// alright! from the sake of the function name, I can predict that it will successfully get the last digit
// oh hang-on, where is the parameter???? this programmer must be tired or "coding while eating"... careful aye.. holiday is coming.

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
// nope.. It runs as predicted :) xixixiixi... that way we are in this "debugging" session.
// it runs but no syntax error but the output are not correct.

// Explain why the output is the way it is
// =============> write your explanation here
// the function does not get any "define parameter", like nothing, so it tries to the "num" in global variable
// that is given just before the function that is const num = 103;
// so, every time that function is called, it will use num 103 as parameter

// Finally, correct the code to fix the problem
// =============> write your new code here
const num = 103;

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
// no syntax error just logic error :) because, sometime we need to manipulate the function just like that. BTW that is not save anyway.
16 changes: 14 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,17 @@
// 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
}
// squaring your height: 1.73 x 1.73 = 2.99
let heightSquared = height * height;
// dividing 70 by 2.99 = 23.41
let weightMod = weight / heightSquared;
// Your result will be displayed to 1 decimal place, for example 23.4.
let BMI = weightMod.toFixed(1);
// return the BMI of someone based off their weight and height
return BMI;
}

let myWeight = 70;
let myHeight = 1.73;

console.log(`my BMI is ${calculateBMI(myWeight, myHeight)}`);
14 changes: 14 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js

Choose a reason for hiding this comment

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

Can you wrap your code into a function that is then called with the argument "hello world I am sleepy"?

Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@
// 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

let myStr = "hello world I am sleepy";

// let myStrUpperCase = myStr.toUpperCase();

// let arrWord = myStrUpperCase.split(" ");

// let myStrUpperCaseWithSnakeCase = arrWord.join("_");

//console.log(`${arrWord}`);

let combine = myStr.toUpperCase().split(" ").join("_");

console.log(`${combine}`);
63 changes: 63 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,66 @@
// 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 = "9p"; //init variable

// const penceStringWithoutTrailingP = penceString.substring(
// 0,
// penceString.length - 1
// ); //remove p character at the end of the variable
// console.log(penceStringWithoutTrailingP);

// const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //
// console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits.

// const pounds = paddedPenceNumberString.substring(
// 0,
// paddedPenceNumberString.length - 2
// ); //get ponds value conversion changes for given pence

// console.log(pounds);

// const pence = paddedPenceNumberString
// .substring(paddedPenceNumberString.length - 2)
// .padEnd(2, "0"); //get the reminds after conversion to pounds

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

//------------------------------------------------------------------------------------------

function pensToPounds(penceString) {
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
); //remove p character at the end of the variable
//console.log(penceStringWithoutTrailingP);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); //
//console.log(paddedPenceNumberString); //give format of 3 zeroes if the number is less than 3 digits.

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
); //get ponds value conversion changes for given pence

//console.log(pounds);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0"); //get the reminds after conversion to pounds

return `£${pounds}.${pence}`;
}

//test----1
let moneyA = "90p";
console.log(`${pensToPounds(moneyA)}`);

//test----2
moneyA = "710p";
console.log(`${pensToPounds(moneyA)}`);

//test----3
moneyA = "1210p";
console.log(`${pensToPounds(moneyA)}`);
12 changes: 8 additions & 4 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

let mySeconds = 61;
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

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

//ans: So, the value assigned to num when pad is called for the first time is 0.
// c) What is the return value of pad is called for the first time?
// =============> write your answer here

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

//ans: The last call to pad is pad(remainingSeconds). Therefore, when pad is called for the last time, the value assigned to num is 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
//ans: 01....the return value assigned to num when pad is called for the last time in this program is "01".
28 changes: 27 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
if (time === "00:00" || time === "24:00") {
//put the edge case on the first checking if-else-case
return `12:00 am`;
} else if (time === "12:00") {
return `12:00 pm`;
} else if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
Expand All @@ -23,3 +28,24 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("24:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

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