diff --git a/Narcisstic_Number/narcisstic.js b/Narcisstic_Number/narcisstic.js new file mode 100644 index 0000000..d10aff9 --- /dev/null +++ b/Narcisstic_Number/narcisstic.js @@ -0,0 +1,35 @@ +/* + A Narcissistic Number is a positive number which is the sum of its own digits, + each raised to the power of the number of digits in a given base. + + Eg. + - 153 => 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 + - 1634 => 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 +*/ + +// Algorithm using a simple loop and modulo +function narcissisticChecker(value) { + let digits = [] + let num = value + while (value > 9) { + digits.push(value % 10) + value = Math.floor(value / 10) + } + digits.push(value) + var result = 0; + for (number of digits) { + result += Math.pow(number, digits.length) + } + + return result === num +} + +// Algorithm using Javascript +function narcissisticchecker(value) { + return ('' + value).split('').reduce(function (total, num) { + return total + Math.pow(num, ('' + value).length) + }, 0) == value; +} + +narcissisticChecker(153) +narcissisticchecker(1634) \ No newline at end of file diff --git a/README.md b/README.md index 5e7e529..76bca2b 100644 --- a/README.md +++ b/README.md @@ -700,15 +700,72 @@ It's that simple! Hope this helps.
+__The challenge:__ +
+*What is a Narcissistic Number:* A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this challenge, we will be dealing with an integer and need to check it, is it Narcissistic Number or not. +
-__Algorithmic Thinking:__+__Algorithmic Thinking:__ +
+According to challenge, we will get an integer argument and we need to check it, is it Narcissistic Number or not. This challenge will need one integer-typed parameter for the function
+Next we will strip one by one the digit and each of the digit need to be raised based on given base and be put in a array. Using loop we will be adding every element and check is it same with the given integer or not
+
+Finally, we return True or False depending on the result of evaluation.
+
+True: When it is a Narcissistic Number
+False: Otherwise
+
+In this challenge we have 2 common ways to solve it:
+
+ 1. Using loop and modulo
+ 2. Using reduce function
+