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.

-8. Name +8. Narcissistic Number Checker -__The challenge:__

+__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
+

+ +__code Implementation:__ +

+In this challenge we have 2 common ways to solve it:
+ + 1. Using loop and modulo + 2. Using reduce function +

+ +1. Using loop and modulo +```js +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 +} +``` + +- The function will get an argument integer-typed +- Creating 2 variable that is an array when we strip the number and for keeping the base number +- Stripping the number using loop and modulo +- Looping the array and raised each of the element and then sum all of it + +2. Using reduce function +```js +function narcissisticchecker(value) { + return ('' + value).split('').reduce(function (total, num) { + return total + Math.pow(num, ('' + value).length) + }, 0) == value; +} +``` +- The function will get an argument integer typed +- Casting the argument and split every digit +- Using reduce function to reduce an array to one value only. In this context we reduce every element in the array to sum all of the element with each of the element raised to the power of the base +- At last check is it same between value from the sum of all element in the array and base value +- Here, provided reduce function that accept 2 argument [here](https://www.w3schools.com/jsref/jsref_reduce.asp). -__code Implementation:__