JS Snippets: String: Difference between revisions

From WikiMLT
 
Line 42: Line 42:


* ''[https://scrimba.com/learn/interviewchallenges/challenge-find-anagrams-in-an-array-cv2mMrHK Scrimba challenge: Find anagram 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">
<syntaxhighlight lang="typescript" line="1" class="code-continue" start="1">
const anagrams = [
  "moz biblical torchbearers",
  "it's razorbill beachcomber",
  "och mcrobbie trailblazers",
  "bib chorizo cellarmaster",
  "thor scribble carbimazole",
];
</syntaxhighlight><syntaxhighlight lang="typescript" line="1" class="code-continue" start="8">
function sortPhrase(phrase) {
function sortPhrase(phrase) {
   return phrase.toLowerCase().split("").sort().join("").trim("");
   return phrase.toLowerCase().split("").sort().join("").trim("");
Line 62: Line 54:


<syntaxhighlight lang="javascript" class="code-continue">
<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));
console.log(isAnagramInArray("Bob Ziroll Scrimba Teacher", anagrams));
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 17:23, 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("");
}

Is Ana­gram: Sen­tences in Ar­ray

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']

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