Skip to content

WEST MIDLANDS | JAN_2025 | IFEANYI MADUBUGWU | MODULE STRUCTUING AND STORING DATA | SPRINT 1 #262

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 1 commit 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
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ 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:
// Line 3 is an expression which calculate the current value of count and also add to 1.
Copy link
Contributor

Choose a reason for hiding this comment

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

"... and also add to 1" does not quite make sense. Can you rephrase the statement?

There is a term commonly used in programming to describe operation like count = count + 1. May I suggest feeding the code to ChatGPT to see how else the code can be described? You may be able to learn some programming terms in the process.

3 changes: 2 additions & 1 deletion Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ let lastName = "Johnson";
// 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.

let initials = ``;
let initials = firstName[0] + middleName[0] + lastName[0];
console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn

16 changes: 13 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
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
// Create a variable to store the ext part of the variable

const dir = ;
const ext = ;
const dir = filePath.slice(0, lastSlashIndex);
const ext = base.slice(base.lastIndexOf("."));

console.log(`${dir}`);
console.log(`${ext}`);
console.log(`The base part of ${filePath} is ${base}`);







// https://www.google.com/search?q=slice+mdn
3 changes: 3 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ 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:
// The variable num represents a randomly generated whole number between 1 and 100 (inclusive).Since minimum is 1 and maximum is 100, we generate a random number within this range using Math.random(). This function produces a decimal between 0 and 1, which we scale to the desired range.Math.floor() rounds the result down to the nearest whole number, ensuring we get an integer.Each time the code runs, num will hold a different random integer between 1 and 100.
Copy link
Contributor

Choose a reason for hiding this comment

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

This function produces a decimal between 0 and 1

The phrase "between 0 and 1" alone is not precise enough in program specification because
it does not state clearly whether 0 and 1 are included in the range.

One concise way to specify a range of values is to use the interval notation.
You can ask ChatGPT "how to specify a range of numbers using interval notation" to learn more about such notation.

Would you try describing the return value of Math.random() and the value of num using this notation?

4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-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?
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;
console.log(age);


8 changes: 7 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// 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}`);
//console.log(`I was born in ${cityOfBirth}`);
//const cityOfBirth = "Bolton";

// Answer:
// i noticed on this syntax you tried to evaluate a variable before it was declare. that was why we got an error message.
//below is the correct syntax:
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
10 changes: 9 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// const cardNumber = 4533787178994213;
// const last4Digits = cardNumber.slice(-4);

const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().slice(-4);

console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Before running the code, make and explain a prediction about why the code won't work
// 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

//Answer:
//After running the code, I noticed that cardNumber.slice is not a function. This happens because .slice() method only works on strings, not numbers. To fix this, we need to convert the number to a string using .toString(). Please refer to the corrected code above.
15 changes: 13 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const hour24ClockTime1 = "20:53";
const hour24ClockTime2 = "08:53";

function convertTo12Hours(time24) {
let [hours, minutes] = time24.split(":");
let period = hours >= 12 ? "PM" : "AM";
hours = hours % 12 || 12;
return hours + ":" + minutes + " " + period;

}

console.log(convertTo12Hours("20:53"));
console.log(convertTo12Hours("08:53"));
9 changes: 7 additions & 2 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
carPrice = Number(carPrice.replaceAll("," , ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +12,16 @@ 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
// Answer: We have 5 function calls. Number(),replaceAll(),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?
//Answer: The error is on Line 5 because replaceAll("," "") is incorrectly written. The correct syntax is replaceAll(",", "").

// c) Identify all the lines that are variable reassignment statements
// Answer: Line 4 and 5 are variable reassign statement.

// d) Identify all the lines that are variable declarations
// Answer: Line 1,2, 7 and 8 are variable declaration.

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Answer: we need to remove comma from this expression Number(carPrice.replaceAll(",","")) to allow us convert number to string using .string() method. this allows mathematical expression to work correctly in java.
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Which part of the expression converts number to string using .string() method?

  2. Typo?

... in java

7 changes: 7 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +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?
// Answer: there is only 5 variable declaration.

// b) How many function calls are there?
// Answer: only one function call console.log()

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// Answer: Movie length calculate the remaining seconds after dividing by 60
8784 % 60 = 146, leaving reminder of 24.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// Answer: totalMinutes we remove movieLength and remainingSeconds and then divide by 60.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you rephrase this sentence to make it grammatically correct?


// e) What do you think the variable result represents? Can you think of a better name for this variable?
//Answer: this represent a format time string
Copy link
Contributor

Choose a reason for hiding this comment

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

You missed the second part of the question.

Can you think of a better name for this variable?


// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// this code works, but it does not work for negative value or decimal function.
28 changes: 17 additions & 11 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
const penceString = "399p";
// this p at the end of 399p represent pence

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// // this remove the letter p from the variable penceString. which means start from the first letter 0 and stop one character before the end.

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
// This makes sure the string is at least 3 digits long by adding "0" at the start if needed.

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
const penceNumber = parseInt(paddedPenceNumberString, 10);
// Converts the string to a number to avoid issues.
Copy link
Contributor

Choose a reason for hiding this comment

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

What issues are you referring to?


const pounds = Math.floor(penceNumber / 100);
// get the whole pound

const pence = (penceNumber % 100).toString().padStart(2, "0");
// this code takes the last two digits for the pence part.

console.log(`£${pounds}.${pence}`);
// this evaluate the final price in pence and pound format



// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
Expand All @@ -25,3 +29,5 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"


4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
// After calling the alert function with "Hello World!", I notice a pop-up on my screen displaying the message "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?
// After invoking the prompt function and storing its return value in the variable myName, I notice a pop-up on my screen with a text input field asking, "What is your name?".

What is the return value of `prompt`?
// The return value is whatever the user types in the input field. If the user clicks Cancel, the return value is null
9 changes: 9 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
// After entering console.log, I notice a new code output ƒ log() { [native code] }.

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

// After calling console, I notice a dropdown appears, showing the console object with various functions {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`
// i notice a return called objective which confirms that console is an object in JavaScript.

Answer the following questions:

What does `console` store?
// console is an object provided by the browser that stores logging and debugging methods like log(), error(), warn(),

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
// console is an objective, while .log is a method(function) inside the objective console.
Copy link
Contributor

Choose a reason for hiding this comment

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

"objective" is not the same as "object"

// the `.` is a property access in javascript.
console.log() means access the log method from the console and use it for the objective.
Loading