Skip to content

NW7 | Jovy_So| Structuring and Testing Data | WEEK 2 #134

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 3 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
4 changes: 2 additions & 2 deletions Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
/*This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem? */
6 changes: 5 additions & 1 deletion Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/* const age = 33;
age = age + 1; */

let age = 33;
age = age + 1;
console.log(age);
2 changes: 1 addition & 1 deletion Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
5 changes: 4 additions & 1 deletion Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213";
const last4Digits = cardNumber.slice(-4);

// The last4Digits variable should store the last 4 digits of cardNumber
Expand All @@ -7,3 +7,6 @@ const last4Digits = cardNumber.slice(-4);
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value


//Error: cardNumber.slice is not a function
41 changes: 39 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
//const 12HourClockTime = "20:53";
//const 24hourClockTime = "08:53";

function convert12To24(time) {
const [hours, minutes] = time.split(":");
let hour = parseInt(hours);
const period = time.includes("PM") ? "PM" : "AM";

if (period === "PM" && hour < 12) {
hour += 12;
} else if (period === "AM" && hour === 12) {
hour = 0;
}

return `${hour.toString().padStart(2, '0')}:${minutes}`;
}

function convert24To12(time) {
const [hours, minutes] = time.split(":");
let hour = parseInt(hours);
const period = hour >= 12 ? "PM" : "AM";

if (hour > 12) {
hour -= 12;
} else if (hour === 0) {
hour = 12;
}

return `${hour}:${minutes} ${period}`;
}

const twelveHourClockTime = "08:53 PM"; // Example input for 12-hour format
const twentyFourHourClockTime = "20:53"; // Given input for 24-hour format

const convertedTo24 = convert12To24(twelveHourClockTime);
const convertedTo12 = convert24To12(twentyFourHourClockTime);

console.log(`12-hour to 24-hour: ${convertedTo24}`); // Output: 20:53
console.log(`24-hour to 12-hour: ${convertedTo12}`); // Output: 8:53 PM
6 changes: 6 additions & 0 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing

/* Answer: In line 3, count = count + 1;, the = operator is performing an assignment.
It takes the current value of the variable count, which is 0 at this point,
adds 1 to it, and then assigns the result back to the variable count.
So, after this line executes, count will now hold the value of 1.
Essentially, this line increments the value of count by 1 */
11 changes: 10 additions & 1 deletion Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const num = 56.5678;

const wholeNumberPart = Math.floor(num);
const decimalPart = num - wholeNumberPart;
const roundedNum = Math.round(num);

console.log(wholeNumberPart);
console.log(decimalPart);
console.log(roundedNum);


// You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num )
// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num )
// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number )

// Log your variables to the console to check your answers
// Log your variables to the console to check your answers
4 changes: 4 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ let firstName = "Creola";
let middleName = "Katherine";
let lastName = "Johnson";

let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

console.log(initials);

// Declare a variable called initials that stores the first character of each string.
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
3 changes: 3 additions & 0 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);

// Create a variable to store the dir part of the filePath variable
// Answer: const dir = slice(0, lastSlashIndex);

// Create a variable to store the ext part of the variable
// Answer: const ext = slice(base.lastIndexOf("."));
2 changes: 2 additions & 0 deletions Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing

//Answer: Each time I run the code, num will be a random integer between 1 and 100, including both endpoints.
4 changes: 4 additions & 0 deletions Sprint-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

-->Answer: This will pop up an alert box displaying "Hello world!".

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

-->Answer: This code works as follows: prompt("What is your name?") displays a dialog box prompting the user to enter their name. The value entered by the user will be stored in the variable myName.
11 changes: 11 additions & 0 deletions Sprint-1/explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?

-->Answer: ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?

-->Answer: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`

-->Answer: 'object'

Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?

-->Answer:
console: This is a built-in global object in JavaScript that provides access to the browser's debugging console. It includes various methods for logging information, warnings, errors, and more.

The dot (.) is used to access properties and methods of an object. In this case, it is used to access the log and assert methods of the console object.
19 changes: 19 additions & 0 deletions Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@ console.log(`The percentage change is ${percentageChange}`);
// Read the code and then answer the questions below

// a) How many function calls are there in this file? Write down all the lines where a function call is made
/* Three.
1. Number
2. replaceAll
3. console.log

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
/* a "," missing in fifth line, should be:
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); */


// c) Identify all the lines that are variable reassignment statements
/* 1. carPrice
2. priceAfterOneYear


// d) Identify all the lines that are variable declarations
/* 1. carPrice
2. priceAfterOneYear
3. priceDifference
4. percentageChange

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/* carPrice.replaceAll(",", "") removed comma from carPrice

The overall purpose of the expression Number(carPrice.replaceAll(",", "")) is to:
1. Clean up the string representation of the car price by removing any commas that are commonly used in formatting large numbers.
2. Convert the cleaned string into a numeric type so that mathematical operations (like addition, subtraction, or percentage calculations) can be performed accurately.
8 changes: 8 additions & 0 deletions Sprint-1/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ console.log(result);
// For the piece of code above, read the code and then answer the following questions

// a) How many variable declarations are there in this program?
/* One. movieLength

// b) How many function calls are there?
/* One. console.log

// c) Using documentation, explain what the expression movieLength % 60 represents
/* It is used to calculate the remaining seconds in the movie length (given in seconds).
Here, % is the modulus operator, which calculates the remainder of two numbers when divided.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/* It is calculating the total number of minutes in the movie length, excluding the remaining seconds.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
/* Total length of the movie.
"total_length"

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
It works for non-negative values of movieLength.
6 changes: 6 additions & 0 deletions Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 2. const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1): remove "p"
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Pad the pence number string
// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2): Extract pounds
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: Extract pence
// 6. console.log(`£${pounds}.${pence}`);: Output the Result

5 changes: 4 additions & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

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

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

/*line 4 is using console.log to get a result but does not return any value
Therefore, '${multiply(10, 32)}`);'will shown as undefined. */
7 changes: 5 additions & 2 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Predict and explain first...

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

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


/*The result will showed as undefined.
Because the formula a+b is not in the same line following 'return'*/
6 changes: 4 additions & 2 deletions Sprint-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Predict and explain first...

const num = 103;

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


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

/*The first line have defined var 'num', so all of the result are '3'.*/
7 changes: 5 additions & 2 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// 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;
const result = `${str[0].toUpperCase()}${str.slice(1)}`;
return result;
}

/* 'str' is a variable, if I use 'let' for it, 'str' would be re-declaring.
I should use result instead of str in the line 7 & 8, to avoid redefining 'str' */
9 changes: 7 additions & 2 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
// Why will an error occur when this program runs?
// Try playing computer with the example to work out what is going on

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

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage());

/*This will occur error because it is re-defining the decimalNumber variable inside the function,
but decimalNumber has already been used as a parameter.

I should use delete 'decimalNumber in line 6, and change line 13 to console.log(convertToPercentage);*/
6 changes: 4 additions & 2 deletions Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

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

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


// 'num' has not been define. The computer did not know I want 3 as the formula number.
// I should use 'num' inside the function body and put '3' to pass the argument when calling the function
15 changes: 14 additions & 1 deletion Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

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


console.log(formatAs12HourClock("12:02"));
console.log(formatAs12HourClock("23:49"));
console.log(formatAs12HourClock("23:02"));
console.log(formatAs12HourClock("15:49"));
console.log(formatAs12HourClock("9:03"));
console.log(formatAs12HourClock("11:01"));
console.log(formatAs12HourClock("12:01"));
console.log(formatAs12HourClock("13:49"));
console.log(formatAs12HourClock("14:20"));
11 changes: 11 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function bodyMassIndex(weigh, height) {

const bmi = weigh / (height * height);

return Math.round(bmi * 10) / 10;
}

console.log(bodyMassIndex(70, 1.73));
console.log(bodyMassIndex(65, 1.73));
console.log(bodyMassIndex(60, 1.78));
8 changes: 8 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution

function toUpperSnakeCase(input) {
return input.replace(/\s+/g, '_').toUpperCase();
}

console.log(toUpperSnakeCase('hello there'));
console.log(toUpperSnakeCase("lord of the rings"));
console.log(toUpperSnakeCase("upper snake case"));
Loading