-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
106 lines (92 loc) · 3.02 KB
/
index.html
File metadata and controls
106 lines (92 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>String Questions</title>
</head>
<body>
<h2>String most important question</h2>
<script>
// Convert String to Array with Each Character:
let str = "i am hamza rafique";
let arr = Array.from(str);
console.log(arr, "arry");
// convert str to array for each words,
let onearray = str.split(" ");
console.log(onearray, "one array");
// Replace Any Character:
let replaceCharacter = str.replace("a", "e");
console.log(replaceCharacter, "replace character");
// Get Substring from String:
const substing = str.substring(12, 19);
console.log(substing, "substing");
// Remove Last Character:
const removelastchar = str.slice(0, -1);
console.log(removelastchar, "remove last char");
// Remove First Character:
const removefirstchar = str.slice(1);
console.log(removefirstchar, "remove start char");
// Remove String Before Specific Character:
const removeStringBeforeSpecificChar = str.substring(str.indexOf("", 6));
console.log(
removeStringBeforeSpecificChar,
"remove string before specific character"
);
// Remove Extra Spaces from Both Sides:
const removeExtraSpaces = str.trim();
console.log(removeExtraSpaces);
//Find longest String in array
const StrArray = [
"Apple",
"banana",
"orange",
"pianapple",
"amrud",
"jamnu",
];
let longestStr = StrArray[0];
for (let index = 0; index < StrArray.length; index++) {
if (StrArray[index].length > longestStr.length) {
longestStr = StrArray[index];
}
}
console.log(longestStr, "longest String");
//reverse a string
let reverse = "longest string is here";
const reverseStr = reverse.split("").reverse().join("");
console.log(reverseStr, "reverserSting");
//using recursive function
function ReversMethod(str) {
let res = "";
for (let i = 0; i < str.length; i++) {
res = str[i] + res;
return res;
}
}
console.log(ReversMethod("im hamza rafique recursive function"));
// 2. Most commonly used character in a string. Write a function that takes a string and returns the character that is most commonly used in the string.
function MostCommonlyUsedCharacter(str) {
let map = {};
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (map[char]) {
map[char]++;
} else {
map[char] = 1;
}
}
let max = 0;
let char = "";
for (let key in map) {
if (map[key] > max) {
max = map[key];
char = key;
}
}
return char;
}
</script>
</body>
</html>