Skip to content

Commit d652a61

Browse files
committed
08 Numbers and Maths in JS
Learned about Number in JS and what are the methods related to numbers, learned about Maths in JS and Methods related to maths.
1 parent 7714913 commit d652a61

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

01_Basics/06_stackAndHeap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ let anotherName = myYoutubeName // This will have the copy value of myYoutubeNam
88
anotherName = "amanyadavz" // It's value will go in stack.
99
console.log(myYoutubeName); // Output : ayushyadavz (Original value did not change)
1010
console.log(anotherName); // Output : amanyadavz (Only Copy value is changed)
11-
// So, here we have changed the copy not the original value.
11+
/* So, here we have changed the copy not the original value.
12+
13+
---------------------------------------------------------------------------------------- */
1214

1315
// Example of Heap,
1416
let userOne = { // UserOne will go into Stack. (Primitive)

01_Basics/07_strings.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
// 7. Strings
1+
/* 7. Strings
22
3-
// (i) Concatenation in Strings,
3+
(i) Concatenation in Strings, */
44
const name = "Ayush"
55
const repoCount = 19
66
// Outdated Syntax,
77
console.log(name + repoCount + " etc."); // Output : Ayush19 etc.
88
// Modern day Syntax,
99
console.log(`My name is ${name} and I have total ${repoCount} repositories on GiHub.`);
1010
// Output : My name is Ayush and I have total 19 repositories on GiHub.
11-
// Note: Benifits of this syntax is that we can use methods like, ${name.toUpperCase} e.t.c.
11+
/* Note: Benifits of this syntax is that we can use methods like, ${name.toUpperCase} e.t.c.
12+
13+
-------------------------------------------------------------------------------------------------- */
1214

1315
// (ii) One more way of declaring strings,
1416
const gameName = new String('SuperMario') /* When we run this in Browser's console we can see

01_Basics/08_nums_and_maths.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* 8. Numbers And Maths
2+
3+
(i) Numbers,
4+
When Javascript automatically defines the data type, */
5+
const balance = 400
6+
// console.log(balance); // Output: 400
7+
// Note: Here Javascript automatically detected that the score type is number.
8+
9+
// But we can also define explicitely the data type,
10+
const newBalance = new Number(400)
11+
// console.log(newBalance); // Output: [Number: 400]
12+
// Note: When we do this in browser console the we can see the methods.
13+
14+
// ---------------------------------------------------------------------------------------------------------
15+
16+
// Some Methods,
17+
// console.log(newBalance.toString()); // Output: 400
18+
// Note: When we will check it's typeof the we will see that it is changed into strings.
19+
// console.log(newBalance.toString().length); // Output: 3 (cause 100 has 3 chracters.)
20+
21+
// console.log(newBalance.toFixed(2)); // Output: 400.00
22+
23+
const otherNumbers = 123.8966
24+
// console.log(otherNumbers.toPrecision(3)); // Output: 124
25+
const otherNum = 23.8966
26+
// console.log(otherNum.toPrecision(3)); // Output: 23.9
27+
// Note: It means we want precise value but on how many values we want to focus.
28+
29+
const hundreds = 1000000
30+
// console.log(hundreds.toLocaleString('en-IN')); // Output: 10,00,000 (converted accor. to IN standards.)
31+
32+
/* *******************************************MATHS*******************************************************
33+
34+
(ii) Maths, */
35+
console.log(Math); /* Output: Object [Math] {}
36+
Note: When we run this console.log(Math) in browser's console we will get value under object. */
37+
38+
console.log(Math.abs(-4)); // Output: 4 (Changes only minus values in plus)
39+
40+
console.log(Math.round(4.6)); // Output: 5
41+
console.log(Math.round(4.4)); // Output: 4
42+
console.log(Math.ceil(4.2)); // Output: 5 (Choose top value)
43+
console.log(Math.floor(4.9)); // Output: 4 (Choose lowest value)
44+
45+
console.log(Math.min(3, 2, 4, 6)); /* Output: 2 (Tells min value)
46+
Note: There is also "max". */
47+
48+
console.log(Math.random()); // Output: 0.36675387800250636 (Gives random numbers between 0 & 1)
49+
console.log((Math.random()*10) + 1); // Now numbers will range from 1 to 9
50+
console.log(Math.floor(Math.random()*10) + 1); // Round offs at Random numbers at lowest value.
51+
52+
const min = 10
53+
const max = 20
54+
console.log(Math.floor(Math.random() * (max - min + 1)) + min);
55+
// Note: (max - min) to get a range, (+ 1) to avoid zero, (+ min) to get minimum value to this.

0 commit comments

Comments
 (0)