# Counting case sensitive letters

**URL:** <https://community.glideapps.com/t/counting-case-sensitive-letters/77042>\
**Category:** Ask for Help\
**Created:** [October 14, 2024, 8:20am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042 "2024-10-14T08:20:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJ-SuD](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/aj-sud/32/87448_2.png) [@AJ-SuD](https://community.glideapps.com/u/AJ-SuD)\
**Post date:** [October 14, 2024, 8:20am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/1 "2024-10-14T08:20:16Z")

</div>

Hi Gliders  
I’m having problems with a JS, could you please help me?

```auto
function countIdenticalLetters(str) {
    const letterCounts = {};
    for (const letter of str) {
       if (/[a-zA-Z]/.test(letter)) {
            letterCounts[letter] = (letterCounts[letter] || 0) + 1;
        }
    }

    return letterCounts;
}
const input = "Hello, World!";
const result = countIdenticalLetters(input);
result;

```

Thank’s  
Alex

---

<div class="post-metadata">

**Author:** ![micheal](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/micheal/32/80181_2.png) [@micheal](https://community.glideapps.com/u/micheal)\
**Post date:** [October 14, 2024, 8:36am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/2 "2024-10-14T08:36:41Z")

</div>

what are you trying to make here?  
are you trying to count how many identical letters? What would the output look like?

---

<div class="post-metadata">

**Author:** ![AJ-SuD](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/aj-sud/32/87448_2.png) [@AJ-SuD](https://community.glideapps.com/u/AJ-SuD)\
**Post date:** [October 14, 2024, 8:39am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/3 "2024-10-14T08:39:35Z")

</div>

Hi micheal,  
yes correct.  
H:1, e:1, l:3,…

---

<div class="post-metadata">

**Author:** ![micheal](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/micheal/32/80181_2.png) [@micheal](https://community.glideapps.com/u/micheal)\
**Post date:** [October 14, 2024, 8:40am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/4 "2024-10-14T08:40:18Z")

</div>

hmmm okay let me seee

---

<div class="post-metadata">

**Author:** ![micheal](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/micheal/32/80181_2.png) [@micheal](https://community.glideapps.com/u/micheal)\
**Post date:** [October 14, 2024, 8:49am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/5 "2024-10-14T08:49:21Z")

</div>

Alright, try this code

```auto
function countLetters() {
  let result = "";

  let inputText = p1.toLowerCase();

  // Loop through letters 'a' to 'z'
  for (let i = 97; i <= 122; i++) {
    let letter = String.fromCharCode(i);
    let count = 0;

    // Count occurrences of the current letter
    for (let j = 0; j < inputText.length; j++) {
      if (inputText[j] === letter) {
        count++;
      }
    }

    // Append the letter and its count to the result if it exists
    if (count > 0) {
      result += letter + ": " + count + ", ";
    }
  }

  // Remove trailing comma and space
  return result.slice(0, -2);
}

return countLetters();

```

---

<div class="post-metadata">

**Author:** ![AJ-SuD](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/aj-sud/32/87448_2.png) [@AJ-SuD](https://community.glideapps.com/u/AJ-SuD)\
**Post date:** [October 14, 2024, 9:19am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/6 "2024-10-14T09:19:11Z")

</div>

Hi Micheal,  
it is counting but it shouldn’t be toLowerCase  
and also I have a problem with ä,ö,ü & ß from the german Keyboardlayout & punctuation marks like & and %…

Thank’s  
Alex

---

<div class="post-metadata">

**Author:** ![micheal](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/micheal/32/80181_2.png) [@micheal](https://community.glideapps.com/u/micheal)\
**Post date:** [October 14, 2024, 9:22am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/7 "2024-10-14T09:22:50Z")

</div>

Alright try this

```auto
function countCharacters() {
  let result = "";

  let inputText = p1; // Keep original case for case sensitivity

  // Initialize an object to hold character counts
  let charCount = {};

  // Loop through each character in the string
  for (let i = 0; i < inputText.length; i++) {
    let char = inputText[i];

    // Skip spaces if you don't want them counted
    if (char === " ") {
      continue;
    }

    // Increment the character count in the object
    if (charCount[char]) {
      charCount[char]++;
    } else {
      charCount[char] = 1;
    }
  }

  // Construct the result string
  for (let char in charCount) {
    result += char + ": " + charCount[char] + ", ";
  }

  // Remove trailing comma and space
  return result.slice(0, -2);
}

return countCharacters();

```

---

<div class="post-metadata">

**Author:** ![AJ-SuD](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/aj-sud/32/87448_2.png) [@AJ-SuD](https://community.glideapps.com/u/AJ-SuD)\
**Post date:** [October 14, 2024, 9:29am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/8 "2024-10-14T09:29:53Z")

</div>

It’s working  
Thank you!

---

<div class="post-metadata">

**Author:** ![micheal](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/micheal/32/80181_2.png) [@micheal](https://community.glideapps.com/u/micheal)\
**Post date:** [October 14, 2024, 9:31am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/9 "2024-10-14T09:31:49Z")

</div>

Awesome happy to help

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [October 21, 2024, 9:32am UTC](https://community.glideapps.com/t/counting-case-sensitive-letters/77042/10 "2024-10-21T09:32:26Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
