Your task is to implement function that accepts a number (n) and returns the sum of its…
- User
Your task is to implement function that accepts a number (n) and returns the sum of its digits until we get to a one digit number. Write in JS
For example:
For 100, the result should be 1 (1 + 0 + 0 = 1) getSumOfDigits(100) => 1
For 91, the result should be 1 (9 + 1 = 10, 1 + 0 = 1) getSumOfDigits(91) => 1
Assistant
js/* Given a number (n), return the sum of its digits until we get to a one digit number. For example: For 100, the result should be 1 (1 + 0 + 0 = 1) getSumOfDigits(100) => 1 For 91, the result should be 1 (9 + 1 = 10, 1 + 0 = 1) getSumOfDigits(91) => 1 */ const getSumOfDigits = (n) => { if (n < 10) { return n; } let sum = 0; while (n > 0) { sum += n % 10; n = Math.floor(n / 10); } return getSumOfDigits(sum); };
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.