JS Snippets: String: Difference between revisions

From WikiMLT
mNo edit summary
Line 7: Line 7:


==Reverse a String==
==Reverse a String==
<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript" line="1">
function reverse(str: string) {
function reverse(str: string) {
     return str.split("").reverse().join("");
     return str.split("").reverse().join("");
}
}
</syntaxhighlight><syntaxhighlight lang="typescript">
</syntaxhighlight><syntaxhighlight lang="typescript" line="1">
function reverse(str: string) {
function reverse(str: string) {
     let newStr = "";
     let newStr = "";
Line 22: Line 22:


* ''Palindromes are words that are the same forward or backward.''
* ''Palindromes are words that are the same forward or backward.''
<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript" line="1">
function isPalindrome(str: string) {
function isPalindrome(str: string) {
     return str === str.split("").reverse().join("");
     return str === str.split("").reverse().join("");
Line 31: Line 31:


*''Anagrams are groups of words that can be spelled with the same letters.''
*''Anagrams are groups of words that can be spelled with the same letters.''
<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript" line="1">
function isAnagram(str1: string, str2: string) {
function isAnagram(str1: string, str2: string) {
     if (str1.length !== str2.length) return false;
     if (str1.length !== str2.length) return false;
Line 42: Line 42:


*''Write a function that will capitalize every word in a sentence.''
*''Write a function that will capitalize every word in a sentence.''
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue" line="1">
function capitalizeWord(word: string) {
function capitalizeWord(word: string) {
     return word[0].toUpperCase() + word.slice(1);
     return word[0].toUpperCase() + word.slice(1);
Line 60: Line 60:


== Remove the Duplicate Chars ==
== Remove the Duplicate Chars ==
<syntaxhighlight lang="typescript" class="code-continue">
<syntaxhighlight lang="typescript" class="code-continue" line="1">
function removeDupeChars(chars: string) {
function removeDupeChars(chars: string) {
     let output = "";
     let output = "";

Revision as of 16:30, 11 March 2023

Ref­er­ences

Re­verse 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 Palin­drome

  • Palin­dromes are words that are the same for­ward or back­ward.
function isPalindrome(str: string) {
    return str === str.split("").reverse().join("");
}

Is Ana­gram

  • Ana­grams are groups of words that can be spelled with the same let­ters.
function isAnagram(str1: string, str2: string) {
    if (str1.length !== str2.length) return false;
    return str1.split("").sort().join("") === str2.split("").sort().join("");
}

Ti­tle Case

  • Write a func­tion that will cap­i­tal­ize every word in a sen­tence.
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

Re­move the Du­pli­cate 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