Skip to content

Commit

Permalink
kaprekar-number-checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonali Moholkar authored and Sonali Moholkar committed Oct 24, 2018
1 parent e568b1d commit d4f106e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Algorithms/kaprekar-number-checker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function isKaprekar(k) {
var n = length(k);
var square = k * k;

var firstPeice = String(square).substring(0, length(square) - n);
var secondPiece = String(square).substring(length(square) - n);

var sumOfPieces = Number(firstPeice) + Number(secondPiece);

var outputMessage =
'inputNumber: ' + k + ', ' +
'Sum: ' + sumOfPieces;

return {
isKaprekar: sumOfPieces === k,
explanation: outputMessage
};
}

function length(value) {
return String(value).length;
}


console.log(isKaprekar(45))

0 comments on commit d4f106e

Please sign in to comment.