-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
84 lines (51 loc) · 2.13 KB
/
utils.js
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
function generateHiddenString(input) {
// Replace spaces with underscores and other characters with spaces
const sanitizedInput = input.replace(/[^a-zA-Z ]/g, ' ');
// Split the sanitized input into words
const words = sanitizedInput.split(' ');
// Create an array of underscore strings with the same length as each word
const underscoreStrings = words.map(word => '➖'.repeat(word.length));
// Join the underscore strings with spaces
const result = underscoreStrings.join(' ');
return result;
}
function replaceSubstringAtIndex(originalString, index, newChar) {
if (index < 0 || index >= originalString.length) {
console.error("Invalid index. Index should be within the bounds of the original string.");
return originalString;
}
const updatedString =
originalString.slice(0, index) + newChar + originalString.slice(index + newChar.length);
return updatedString;
}
function indexOfElementInString(element, searchString) {
const index = searchString.indexOf(element);
if (index !== -1) {
// Element found, return the index
return index + 1; // Adding 1 to make it 1-based index
} else {
// Element not found, return 0 or false
return 0; // You can also return `false` instead of 0 if you prefer
}
}
function replaceFirstOccurrence(inputString, targetChar, replacementChar) {
const index = inputString.indexOf(targetChar);
if (index !== -1) {
const updatedString =
inputString.substring(0, index) +
replacementChar +
inputString.substring(index + 1);
return updatedString;
}
return inputString; // If the targetChar is not found, return the original string
}
/**
* Check if Game is ended, by just simply checking
* if all characters except space are replaced with hashtags
*/
function isGameEnded(str, char = "#") {
// Create a regular expression to match the pattern
const regex = new RegExp(`^${char}*$`);
// Remove spaces from the string and test if the modified string matches the pattern
return regex.test(str.replace(/\s/g, ''));
}