How to use string conversion In Javascript
Another way to approach this problem is to convert the square of the number to a string and then iterate through the characters of the string to calculate the sum of the digits.
Example: To demonstrate checking the number whether it is one number or not by converting it into a string and iterating over it.
Javascript
function isNeonNumber(num) { let square = (num * num).toString(); let sumOfDigits = 0; for (let digit of square) { sumOfDigits += parseInt(digit, 10); } return sumOfDigits === num; } console.log(isNeonNumber(9)); console.log(isNeonNumber(8)); |
Output
true false
JavaScript Program to Check whether the Input Number is a Neon Number
A neon number is a number where the sum of digits of its square equals the number itself. For example, 9 is a neon number because 9^2 = 81, and the sum of the digits 81 is 9. There are several ways to check if a given number is a neon number in JavaScript or not which are as follows:
Table of Content
- Using naive method
- Using string conversion
- Using array methods
Contact Us