JS Snippets: String: Difference between revisions
From WikiMLT
m Стадий: 3 [Фаза:Разработване, Статус:Разработван]; Категория:JavaScript |
|||
Line 1: | Line 1: | ||
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | <noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude> | ||
==References== | |||
== | *[https://github.com/metalevel-tech/exc-js-homework MLT GitHub: JavaScript Homework Tasks] | ||
... | |||
==Reverse a String== | |||
<syntaxhighlight lang="typescript"> | |||
function reverse(str: string) { | |||
return str.split("").reverse().join(""); | |||
} | |||
</syntaxhighlight><syntaxhighlight lang="typescript"> | |||
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"> | |||
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"> | |||
function isAnagram(str1: string, str2: string) { | |||
if (str1.length !== str2.length) return false; | |||
return str1.split("").sort().join("") === str2.split("").sort().join(""); | |||
} | |||
</syntaxhighlight> | |||
==Title Case== | |||
*''Write a function that will capitalize every word in a sentence.'' | |||
<syntaxhighlight lang="typescript" class="code-continue"> | |||
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="javascript" class="code-continue"> | |||
Pumpkin Pranced Purposefully Across The Pond | |||
</syntaxhighlight> | |||
<noinclude> | <noinclude> | ||
<div id= | <div id="devStage"> | ||
{{devStage | {{devStage | ||
| Прндл = JavaScript | | Прндл = JavaScript |
Revision as of 12:32, 11 March 2023
References
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("");
}
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