Lucky Number JavaScript code challenge I completed from https://codesignal.com (formally codefights.com). The original had tests on codesignal that just ran against the function. However, I adapted it in jp_javascript.js for the web using jQuery to listen for events and to manipulate the DOM. This functions receives a number which is considered lucky if the cardinality of the number is even eg. 11, 1415 and the numbers on the right and left side of the number add up to the same value, eg. 1405, 445607. This page is a work in progress.
function (n) { var n2 = n; var num = []; var divisor = 10; var t = n2.toString().length; if (t === undefined && t === 0){ return false; } if ( (t > 14) || ((t%2) > 0 )){ // Test for Even amt of numbers and to return false; // limit the number processed from the input } else { for (var i = 0; i < t; i++) { num[i] = (n%divisor); n = Math.floor(n/divisor); } var halfsize = (num.length)/2; var totalone = 0; var totaltwo = 0; for (var i = 0; i < halfsize; i++) { totalone += num[i]; } for (var i = halfsize; i < num.length; i++) { totaltwo += num[i]; } return (totalone === totaltwo); } }
Enter Number: