-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-searching-method-practice.js
More file actions
52 lines (40 loc) · 1.21 KB
/
Copy pathstring-searching-method-practice.js
File metadata and controls
52 lines (40 loc) · 1.21 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
// Problem 1: Contains Word
function containsWord(text, word){
return text.includes(word)
}
console.log(containsWord("I love JavaScript", "JavaScript")); // true
console.log(containsWord("Hello World", "Python")); // false
// Problem 2: First Position
function findFirst(text, char){
return text.indexOf(char)
}
console.log(findFirst("javascript", "a"));
// Problem 3: Last Position
function findLast(text, char){
return text.lastIndexOf(char);
}
console.log(findLast("banana", "a"));
// Problem 4: Count Word
function countWord(text,word){
const rg = new RegExp(word,"ig")
return text.match(rg).length
}
console.log(countWord("cat dog cat bird cat", "cat"));
// Problem 5: Find All Positions
function findPositions(text, char) {
const arr = [];
const lowerChar = char.toLowerCase();
for (let i = 0; i < text.length; i++) {
if (text[i].toLowerCase() === lowerChar) {
arr.push(i);
}
}
return arr;
}
console.log(findPositions("banana", "a")); // [1, 3, 5]
// Problem 6: Case Insensitive Search
function containsWord(text,word){
const rg = new RegExp(word,"ig")
return text.includes(text)
}
console.log(containsWord("JavaScript", "javascript")); // true