TheAlgorithms/JavaScript · error · Error
Input data must be strings
Error message
Input data must be strings
What it means
Guard in percentageOfLetter. The function computes the floor percentage of characters in str equal to letter and requires BOTH arguments to be strings, throwing a plain Error (not TypeError) otherwise. Note there is NO empty-string guard: an empty str yields division 100*0/0 = NaN, and a multi-character letter is compared only by its first character implicitly via iteration.
Source
Thrown at String/PercentageOfLetters.js:15
/**
* @function percentageOfLetter
* @description Return the percentage of characters in 'str'
* that equal 'letter' rounded down to the nearest whole percent.
* More info: https://leetcode.com/problems/percentage-of-letter-in-string/
* @param {String} str
* @param {String} letter
* @returns {Number}
* @example
* const str = 'foobar', const letter = 'o'
* percentageOfLetter(str, letter) // ===> 33
*/
const percentageOfLetter = (str, letter) => {
if (typeof str !== 'string' || typeof letter !== 'string') {
throw new Error('Input data must be strings')
}
let letterCount = 0
// Iterate through the whole given text
for (let i = 0; i < str.length; i++) {
// Count how often the letter appears in the word
letterCount += str[i].toLowerCase() === letter.toLowerCase() ? 1 : 0
}
const percentage = Math.floor((100 * letterCount) / str.length)
return percentage
}
export { percentageOfLetter }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Ensure both arguments are strings; default missing values sensibly.
- Guard for empty str before calling to avoid the NaN result.
- Validate letter.length === 1 if a single character is expected.
Example fix
// before
percentageOfLetter(str, letter)
// after
if (typeof str === 'string' && typeof letter === 'string' && str.length) {
percentageOfLetter(str, letter)
} Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof str !== 'string' || typeof letter !== 'string') {
throw new Error('str and letter must be strings')
}
if (str.length === 0) throw new Error('str must be non-empty')
percentageOfLetter(str, letter) Type guard
const areStrings = (a, b) => typeof a === 'string' && typeof b === 'string'
Try / catch
try {
percentageOfLetter(str, letter)
} catch (e) {
if (e instanceof Error && /must be strings/i.test(e.message)) { /* coerce and retry */ } else throw e
} Prevention
- Also guard for empty str to avoid the NaN return value.
- Validate letter.length === 1 if a single char is expected.
- Note this throws Error, not TypeError — instanceof checks differ.
When it happens
Trigger: Calling percentageOfLetter(null, 'o'), percentageOfLetter('foo', undefined), percentageOfLetter(42, '4'), percentageOfLetter('foo', ['o']). Either argument non-string.
Common situations: An optional letter parameter omitted; str read from a source returning null; a number passed where a string was expected; empty str passed (does not throw but returns NaN).
Related errors
- Argument is not a string.
- Argument is not a string.
- The first param should be a string
- The second param should be a boolean
- Input should be a string
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/44d0bf635d79dfdf.
Report an issue: GitHub.