JS Snippets: String: Difference between revisions

From WikiMLT
Spas (talk | contribs)
Spas (talk | contribs)
m Стадий: 4 [Фаза:Авторизиране, Статус:Разработен]; Категория:JavaScript
Line 59: Line 59:


<noinclude>
<noinclude>
<div id="devStage">
<div id='devStage'>
{{devStage  
{{devStage  
  | Прндл  = JavaScript
  | Прндл  = JavaScript
  | Стадий = 3
  | Стадий = 4
  | Фаза  = Разработване
  | Фаза  = Авторизиране
  | Статус = Разработван
  | Статус = Разработен
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | РзбПт = Spas
| АвтПт = {{REVISIONUSER}}
  | ИдтДт  = 11.03.2023
  | ИдтДт  = 11.03.2023
  | РзбДт  = {{Today}}
  | РзбДт = 11.03.2023
| АвтДт = {{Today}}
  | ИдтРв  = [[Special:Permalink/32386|32386]]
  | ИдтРв  = [[Special:Permalink/32386|32386]]
  | РзбРв  = {{REVISIONID}}
  | РзбРв = [[Special:Permalink/32389|32389]]
| АвтРв = {{REVISIONID}}
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Revision as of 12:34, 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