JS Snippets: String: Difference between revisions
From WikiMLT
Стадий: 1 [Фаза:Идентифициране, Статус:Създаване]; Категория:JavaScript; { The page is created via DevStage } |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | <noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | ||
==References== | |||
== | *Scrimba: [https://scrimba.com/learn/interviewchallenges JavaScript Interview Challenges] | ||
... | *MLT GitHub: [https://github.com/metalevel-tech/exc-js-homework JavaScript Homework Tasks] | ||
==Reverse a String== | |||
<syntaxhighlight lang="typescript" line="1"> | |||
function reverse(str: string) { | |||
return str.split("").reverse().join(""); | |||
} | |||
</syntaxhighlight><syntaxhighlight lang="typescript" line="1"> | |||
function reverse(str: string) { | |||
let newStr = ""; | |||
for (let i = str.length - 1; i >= 0; i--) newStr += str[i]; | |||
return newStr; | |||
} | |||
</syntaxhighlight> | |||
== Is Palindrome== | |||
* ''Palindromes are words that are the same forward or backward.'' | |||
<syntaxhighlight lang="typescript" line="1"> | |||
function isPalindrome(str: string) { | |||
return str === str.split("").reverse().join(""); | |||
} | |||
</syntaxhighlight> | |||
==Is Anagram== | |||
*''Anagrams are groups of words that can be spelled with the same letters.'' | |||
<syntaxhighlight lang="typescript" line="1"> | |||
function isAnagram(str1: string, str2: string) { | |||
if (str1.length !== str2.length) return false; | |||
return str1.split("").sort().join("") === str2.split("").sort().join(""); | |||
} | |||
</syntaxhighlight> | |||
== Is Anagram: Sentences in Array == | |||
* ''[https://scrimba.com/learn/interviewchallenges/challenge-find-anagrams-in-an-array-cv2mMrHK Scrimba challenge: Find anagram in array.]'' | |||
<syntaxhighlight lang="typescript" line="1" class="code-continue" start="1"> | |||
function sortPhrase(phrase) { | |||
return phrase.toLowerCase().split("").sort().join("").trim(""); | |||
} | |||
function isAnagramInArray(anagram, arr) { | |||
const anagramSorted = sortPhrase(anagram); | |||
return arr.filter((entry) => anagramSorted === sortPhrase(entry)); | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
const anagrams = [ | |||
"moz biblical torchbearers", | |||
"it's razorbill beachcomber", | |||
"och mcrobbie trailblazers", | |||
"bib chorizo cellarmaster", | |||
"thor scribble carbimazole", | |||
]; | |||
console.log(isAnagramInArray("Bob Ziroll Scrimba Teacher", anagrams)); | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell-session" class="code-continue"> | |||
(3) ['moz biblical torchbearers', 'och mcrobbie trailblazers', 'thor scribble carbimazole'] | |||
</syntaxhighlight> | |||
==Title Case== | |||
*''Write a function that will capitalize every word in a sentence.'' | |||
<syntaxhighlight lang="typescript" class="code-continue" line="1"> | |||
function capitalizeWord(word: string) { | |||
return word[0].toUpperCase() + word.slice(1); | |||
} | |||
function toTitleCase(str: string) { | |||
return str.split(" ").map(word => capitalizeWord(word)).join(" "); | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
console.log(toTitleCase("pumpkin pranced purposefully across the pond")); | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell-session" class="code-continue"> | |||
Pumpkin Pranced Purposefully Across The Pond | |||
</syntaxhighlight> | |||
== Remove the Duplicate Chars == | |||
<syntaxhighlight lang="typescript" class="code-continue" line="1"> | |||
function removeDupeChars(chars: string) { | |||
let output = ""; | |||
for (const char of chars) | |||
if (!output.includes(char)) | |||
output += char; | |||
return output; | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
console.log(removeDupeChars("pumpkin pranced purposefully across the pond")); | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="shell-session" class="code-continue"> | |||
pumkin racedosflyth | |||
</syntaxhighlight> | |||
<noinclude> | <noinclude> | ||
Line 12: | Line 110: | ||
{{devStage | {{devStage | ||
| Прндл = JavaScript | | Прндл = JavaScript | ||
| Стадий = | | Стадий = 6 | ||
| Фаза = | | Фаза = Утвърждаване | ||
| Статус = | | Статус = Утвърден | ||
| ИдтПт = {{REVISIONUSER}} | | ИдтПт = Spas | ||
| ИдтДт = {{Today}} | | РзбПт = Spas | ||
| ИдтРв = {{REVISIONID}} | | АвтПт = Spas | ||
| УтвПт = {{REVISIONUSER}} | |||
| ИдтДт = 11.03.2023 | |||
| РзбДт = 11.03.2023 | |||
| АвтДт = 11.03.2023 | |||
| УтвДт = {{Today}} | |||
| ИдтРв = [[Special:Permalink/32386|32386]] | |||
| РзбРв = [[Special:Permalink/32389|32389]] | |||
| АвтРв = [[Special:Permalink/32391|32391]] | |||
| УтвРв = {{REVISIONID}} | |||
}} | }} | ||
</div> | </div> | ||
</noinclude> | </noinclude> |
Latest revision as of 16:23, 11 March 2023
References
- Scrimba: JavaScript Interview Challenges
- MLT GitHub: JavaScript Homework Tasks
Reverse a String
function reverse(str: string) {
return str.split("").reverse().join("");
}
function reverse(str: string) {
let newStr = "";
for (let i = str.length - 1; i >= 0; i--) newStr += str[i];
return newStr;
}
Is Palindrome
- Palindromes are words that are the same forward or backward.
function isPalindrome(str: string) {
return str === str.split("").reverse().join("");
}
Is Anagram
- Anagrams are groups of words that can be spelled with the same letters.
function isAnagram(str1: string, str2: string) {
if (str1.length !== str2.length) return false;
return str1.split("").sort().join("") === str2.split("").sort().join("");
}
Is Anagram: Sentences in Array
function sortPhrase(phrase) {
return phrase.toLowerCase().split("").sort().join("").trim("");
}
function isAnagramInArray(anagram, arr) {
const anagramSorted = sortPhrase(anagram);
return arr.filter((entry) => anagramSorted === sortPhrase(entry));
}
const anagrams = [
"moz biblical torchbearers",
"it's razorbill beachcomber",
"och mcrobbie trailblazers",
"bib chorizo cellarmaster",
"thor scribble carbimazole",
];
console.log(isAnagramInArray("Bob Ziroll Scrimba Teacher", anagrams));
(3) ['moz biblical torchbearers', 'och mcrobbie trailblazers', 'thor scribble carbimazole']
Title Case
- Write a function that will capitalize every word in a sentence.
function capitalizeWord(word: string) {
return word[0].toUpperCase() + word.slice(1);
}
function toTitleCase(str: string) {
return str.split(" ").map(word => capitalizeWord(word)).join(" ");
}
console.log(toTitleCase("pumpkin pranced purposefully across the pond"));
Pumpkin Pranced Purposefully Across The Pond
Remove the Duplicate Chars
function removeDupeChars(chars: string) {
let output = "";
for (const char of chars)
if (!output.includes(char))
output += char;
return output;
}
console.log(removeDupeChars("pumpkin pranced purposefully across the pond"));
pumkin racedosflyth