From 2e13184c70bbfaf780d8d75f7f3405818661f14b Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Tue, 4 Apr 2023 17:31:50 -0500 Subject: [PATCH] Add countChars function --- commands/countChars.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/commands/countChars.ts b/commands/countChars.ts index ac10779..d83258d 100644 --- a/commands/countChars.ts +++ b/commands/countChars.ts @@ -33,17 +33,18 @@ module.exports = { function countChars(string) { var outputString = ""; string = string.toLowerCase(); - var letterCount = Array(26).fill(0); // Creates an array with 26 values, all equaling 0. + var letterCount = Array(65535).fill(0); // Creates an array with 26 values, all equaling 0. for (let i = 0; i < string.length; ++i) { let currentChar = string.charAt(i); - let arrayIndex = currentChar.charCodeAt(0) - 97; - if (string.charAt(i).search(/^[a-z]+$/) === 0) { - letterCount[arrayIndex]++; + let arrayIndex = currentChar.charCodeAt(0); + if (string.charAt(i).search(/^[A-Z]+$/) === 0) { + arrayIndex += 32; //This should make it cast any uppercase characters to lowercase } + letterCount[arrayIndex]++; } for (let i = 0; i < letterCount.length; ++i) { if (letterCount[i] > 0) { - outputString += "Number of " + String.fromCharCode(i + 97).toUpperCase() + "'s: " + letterCount[i] + "\n"; + outputString += "Number of " + String.fromCharCode(i).toUpperCase() + "'s: " + letterCount[i] + "\n"; } } return outputString;