-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q7.html
34 lines (28 loc) · 966 Bytes
/
Q7.html
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
<!-- 7)Write a JavaScript function to get the number of occurrences of each letter in specified string from user.
input : credence analytics
output : {"c":3,"r":1,"e":3,"d":1,"n":2,"a":2,"l":1,"y":1,"t":1,"i":1,"s":1} -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function countString(str, letter) {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) == letter) {
count += 1;
}
}
return count;
}
const string = prompt('Enter a string: ');
const letterToCheck = prompt('Enter a letter to check: ');
const result = countString(string, letterToCheck);
console.log(result);
</script>
</body>
</html>