Skip to content

Commit 15f937f

Browse files
committed
Add Number.html and String.html with examples of math and string functions in JavaScript
1 parent 81abab1 commit 15f937f

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
8+
<script>
9+
10+
// using maths functions
11+
12+
console.log(Math.PI); // 3.141592653589793
13+
console.log(Math.E); // 2.718281828459045
14+
15+
// round of numbers
16+
console.log(Math.round(4.3));
17+
18+
// square root
19+
console.log(Math.sqrt(16));
20+
21+
// power
22+
console.log(Math.pow(2, 3));
23+
24+
// random number
25+
console.log(Math.random());
26+
27+
// random number between 0 and 100
28+
console.log(Math.floor(Math.random() * 101));
29+
30+
// minimum and maximum
31+
console.log(Math.min(1, 2, 3, 4, 5));
32+
console.log(Math.max(1, 2, 3, 4, 5));
33+
34+
// absolute value
35+
console.log(Math.abs(-5)); // 5
36+
37+
// average of numbers
38+
const numbers = [1, 2, 3, 4, 5];
39+
40+
// type
41+
42+
data = 100
43+
console.log(typeof data);
44+
45+
data = "100"
46+
console.log(typeof data);
47+
48+
// type conversion
49+
data = "56.8"
50+
data = Number(data);
51+
console.log(typeof data);
52+
53+
// parseInt and parseFloat
54+
data = parseInt(data)
55+
console.log(typeof data);
56+
57+
data = parseFloat(data);
58+
console.log(typeof data);
59+
60+
// toString
61+
data = 100;
62+
console.log(typeof data.toString());
63+
64+
</script>
65+
66+
67+
</head>
68+
<body>
69+
70+
</body>
71+
</html>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
8+
<script>
9+
10+
data = "codes with pankaj"
11+
// find the length of string
12+
console.log(data.length);
13+
// find the index of a character
14+
console.log(data.indexOf("p"));
15+
// find the last index of a character
16+
console.log(data.lastIndexOf("c"));
17+
18+
// lowercase and uppercase
19+
console.log(data.toLowerCase());
20+
console.log(data.toUpperCase());
21+
22+
// trim
23+
data = " codes with pankaj "
24+
console.log(data.trim());
25+
console.log(data);
26+
27+
// split
28+
data = "codes with pankaj"
29+
console.log(data.split(" "));
30+
31+
// replace
32+
data = "codes with pankaj"
33+
console.log(data.replace("with", "and"));
34+
35+
// substring
36+
data = "codes with pankaj"
37+
console.log(data.substring(0, 5)); // "codes"
38+
console.log(data.substring(5)); // " with pankaj"
39+
40+
// charAt
41+
data = "codes with pankaj"
42+
console.log(data.charAt(0)); // "c"
43+
console.log(data.charAt(5)); // " "
44+
45+
// includes
46+
data = "codes with pankaj"
47+
console.log(data.includes("pankaj")); // true
48+
console.log(data.includes("time")); // false
49+
50+
// startsWith and endsWith
51+
console.log(data.startsWith("codes")); // true
52+
console.log(data.endsWith("pankaj")); // true
53+
54+
// repeat
55+
console.log(data.repeat(10)); // "codes with pankajcodes with pankaj"
56+
57+
// template literals
58+
const name = "Pankaj";
59+
const age = 25;
60+
console.log(`My name is ${name} and I am ${age} years old.`); // "My name is Pankaj and I am 25 years old."
61+
62+
</script>
63+
64+
</head>
65+
<body>
66+
67+
</body>
68+
</html>

0 commit comments

Comments
 (0)