Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
18 changes: 12 additions & 6 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ If your PR is rejected, check the task list.

Self checklist

- [ ] I have committed my files one by one, on purpose, and for a reason
- [ ] I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
- [ ] I have tested my changes
- [ ] My changes follow the [style guide](https://curriculum.codeyourfuture.io/guides/contributing/)
- [ ] My changes meet the [requirements](./README.md) of this task
- [x] I have committed my files one by one, on purpose, and for a reason
- [x] I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
- [x] I have tested my changes
- [x] My changes follow the [style guide](https://curriculum.codeyourfuture.io/guides/contributing/)
- [x] My changes meet the [requirements](./README.md) of this task

## Changelist

Briefly explain your PR.
This Pull Request includes JavaScript exercises where I learned about identifying and fixing errors.

I improved my skills in creating new branches, committing individual files, and committing all changes at once. I also prefer using command-line commands rather than buttons for these tasks.

I used String.prototype.padStart() for the first time and gained a good understanding of its usefulness.

To view changes instantly, I used nodemon with a command (while true; do nodemon $(ls *.js | fzf); done) for interactive, real-time feedback.

## Questions

Expand Down
7 changes: 5 additions & 2 deletions Sprint-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?

// To make a multi-line comment in JavaScript, you can start with /* and end with */ . Any text
// in between will be considered a comment and will not be executed as code by the computer.
13 changes: 12 additions & 1 deletion Sprint-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// to run this file we need to go in the root and once we are in the file run, node 1.js eg => cd /Sprint-1/errors => ~ node 1.js


// this line is not working because const does not allow you change the data so we can change to let.
// cost age. = 33;
// age = age + 1;
// console.log(age);


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

6 changes: 5 additions & 1 deletion Sprint-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// 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}`);

// we just need to move the console.log() to avoid render and expression which was calling the data declared in the variable
//ReferenceError: Cannot access 'cityOfBirth' before initialization
24 changes: 22 additions & 2 deletions Sprint-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);

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


// const cardNumber = 4533787178994213;
// const last4Digits = cardNumber.(-4);

// console.log(last4Digits);



// String.prototype.slice()
// The slice() method of String values extracts a section of this string and returns it as a new string,
// without modifying the original string. but in this example we have number we can extract this.
// 1 slice work only with String
// 2 we have to convert the variable to String
// 3 Option we can just added a single quotation but this variable is saving number.


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

console.log(last4Digits);
console.log(typeof(last4Digits));
14 changes: 12 additions & 2 deletions Sprint-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";

// this is given an error because we can no start an variable starting naming with number, So we can fix moving the number
// in the middle or we can write the number instead declare the integer number

const hour12ClockTime = "8:53";
console.log(hour12ClockTime);


const hour24ClockTime = "20:53";
console.log(hour24ClockTime);
8 changes: 6 additions & 2 deletions Sprint-1/exercises/count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// 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
// Line 3 updates the value of count by taking its current value, adding 1 to it,
// and then using the assignment operator (=) to store the new value back into count.

let count = 0;

count = count + 1;
console.log(count);

// 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
19 changes: 19 additions & 0 deletions Sprint-1/exercises/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@ const num = 56.5678;
// 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 )

const wholeNumberPart = (Math.floor(num));


// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num )

// parseFloat() picks the longest substring starting from the beginning that generates a valid
// number literal. If it encounters an invalid character, it returns the number represented up
// to that point, ignoring the invalid character and all characters following it.
// If the argument's first character can't start a legal number literal per the syntax above,
// parseFloat returns NaN.

const decimalPart = parseFloat((num - wholeNumberPart).toFixed(4));


// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number )


const roundedNum = (Math.round(num));

// Log your variables to the console to check your answers
console.log(wholeNumberPart, decimalPart, roundedNum);

9 changes: 9 additions & 0 deletions Sprint-1/exercises/initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ 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 acronym = `${firstName[0]}${middleName[0]}${lastName[0]}`
let initials = firstName[0] + middleName[0] + lastName[0];


console.log(`this option with interpolation ${acronym}`);
console.log(`This string concatenation ${initials}`);
25 changes: 23 additions & 2 deletions Sprint-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@

// (All spaces in the "" line should be ignored. They are purely for formatting.)


/* ================ lastIndexOf() ===================

-we use this to obtain the last position of / where is located the File.txt
-const base = filePath.slice(lastSlashIndex + 1); with + 1 we start to subtract after /
Index: 0 1 2 ... 37 38 39 40 41 42
filePath: / U s ... / f i l e .txt
-
*/

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
console.log(lastSlashIndex); // 44
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
console.log(base); // file.txt
// 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 lastDotIndex = filePath.lastIndexOf(".");
console.log(lastDotIndex); // index position 49
const extractDir = filePath.slice(0, lastDotIndex);
console.log(`Directory and filename (without extension) is: ${extractDir}`);


// Create a variable to store the ext part of the variable"
const extensionName = filePath.slice(lastDotIndex + 1);
console.log(`extension: ${extensionName}`); // txt
38 changes: 37 additions & 1 deletion Sprint-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,44 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num);


// In this exercise, you will need to work out what num represents?
// 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
// Try logging the value of num and running the program several times to build an idea of
// what the program is doing

/*
Math.floor() Math.floor() rounds down the decimal result from the previous step to the
nearest integer. This step ensures the random number is a whole number.
have a random integer between 0 and 99, inclusive.

Math.random() will take a number between 0 (inclusive) and 1 (no inclusive) eg. this
will be decimal numbers (0.345, 0,123)

Math.random() * (maximum - minimum + 1): is multiplied by 100, creating a new range.
Now, Math.random() * 100 generates a random decimal number between 0 and 100
(not including 100). Example 0.0, 25.4, 99.99, etc.

(maximum - minimum + 1) This part calculates the range of numbers you want to include,
which is from 1 to 100. Subtracting minimum from maximum gives 99, and adding 1 results
in 100. So, this range means we’re looking to generate a random number between 0 and 99
and then shift it to between 1 and 100.

+ minimum:
Adding minimum (which is 1 in this case) shifts the entire range up by 1.
Now, instead of getting a range of 0–99, the range becomes 1–100.
so, num will now be a random integer between 1 and 100.

*/

// in this example taken from mdn we see the same function being called and will generate a
//random number between the minimum and maximum

function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}

console.log(getRandomArbitrary(minimum, maximum));
18 changes: 16 additions & 2 deletions Sprint-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,23 @@ Let's try an example.
In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
---------- alert("hello Maria");
---------------Hello Maria

What effect does calling the `alert` function have? show a window with the alert at the top of the browser

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`.

--------- prompt("What is your name?"); //maria
--------- var myName = prompt("What is your name?");
------------- Maria

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

//prompt will display a dialog text box to interact wit the user, this can be used to ask question and then stored in one variable. this can have two option "ok" and "cancel", if cancel is pressed nothing will run. and this will be "undefine".


What is the return value of `prompt`?

//If the user enters a name and clicks "OK," the input text is stored in the variable myName.

25 changes: 23 additions & 2 deletions Sprint-1/explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@
In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course.

Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
---------------- ƒ log() { [native code] }


Now enter just `console` in the Console, what output do you get back?
--------------- console
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
methods properties built-in Js devtools which can be useful for debugging and tracking the code

Try also entering `typeof console`

Try also entering `typeof console`
------------- 'object'

Answer the following questions:

What does `console` store?
------------ Is an object Js which contains methos and properties for logging information, displaying errors, warnings, debugging in the browser.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
------------ the dot notation between `console.log` or `console.assert` is the access to method(function) store within the console object.

---------- eg. With the . operator, we can access the properties in each object within the students array, allowing us to check or modify what’s stored in each object.

let students = [
{ nombre: "Ana", age: 21 },
{ nombre: "Luis", age: 22 },
{ nombre: "Marta", age: 20 }
];

console.table(students);

48 changes: 41 additions & 7 deletions Sprint-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,55 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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


const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;


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
/*
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

-------- Number(carPrice.replaceAll(",", "")); this convert string in numbers
-------- replaceAll(",", "") this function is removing the "," and returning just the numbers
-------- console.log(`The percentage change is ${percentageChange}`); this is a function to print what we call inside.



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?

------- In the line 5 we were missing the "," dividing the ("," "") this gave an error.


c) Identify all the lines that are variable reassignment statements

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


d) Identify all the lines that are variable declarations

------- let carPrice = "10,000";
------- let priceAfterOneYear = "8,543";
------- const priceDifference = carPrice - priceAfterOneYear;
------- const percentageChange = (priceDifference / carPrice) * 100;

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

// 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?
------- The expression Number(carPrice.replaceAll(",", "")) performs two actions:
* carPrice.replaceAll(",", ""):
The replaceAll() method is used on the string carPrice. It removes all commas "," from the
string by replacing them with an empty string "".

// c) Identify all the lines that are variable reassignment statements
* Number(...);
this function is then called on the result of replaceAll(),
converting the string now without commas into a numeric value eg 10,000 => 10000

// d) Identify all the lines that are variable declarations

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