Convert huge if else statement into a loop in Javascript - javascript

I'm trying to format a number to be displayed in a more friendly way. At the moment, the code is really difficult to update and it's huge and clumsy.
function abb(){
if(m>=1 && m<999){
gold_display = m;
}else if(m>999 && m<999999){
var b = (m / 1000).toFixed(3);
gold_display = b+"k";
}else if (m>999999 && m<999999999){
var b = (m / 1000000).toFixed(3);
gold_display = b+"m";
}else if (m>999999999 && m<999999999999){
var b = (m / 1000000000).toFixed(3);
gold_display = b+"b";
}else if (m>999999999999 && m<99999999999999){
var b = (m / 1000000000000).toFixed(3);
gold_display = b+"t";
}
}
EDIT: THIS WOULD BE THE CODE:
function abb(){
if(m>=1 && m<1000){
gold_display = m;
}else if(m>999 && m<1000000){
var b = (m / 1000).toFixed(3);
gold_display = b+"k";
}else if (m>999999 && m<1000000000){
var b = (m / 1000000).toFixed(3);
gold_display = b+"m";
}else if (m>999999999 && m<1000000000000){
var b = (m / 1000000000).toFixed(3);
gold_display = b+"b";
}else if (m>999999999999 && m<100000000000000){
var b = (m / 1000000000000).toFixed(3);
gold_display = b+"t";
}
}
Yeah it's akward but it was the only solution that came up into my mind, is there a way to convert this into a loop and shrink the code?
I'm thinking in saving the letters "k", "m", "b","t" in an array because I want to go higher to "q", "qi","s", etc.
Thanks and sorry if the question is kinda stupid :(

Count how many times you need to divide by 1000 before you are under 1000
function bigNumber(big) {
let divisor = 1,
steps = 0,
small = big;
while (small >= 1000) {
divisor *= 1000;
small = big / divisor;
++steps;
}
return {steps, divisor, small};
}
let gold = 123456789,
foo = bigNumber(gold);
foo.small.toFixed(3) + ['', 'k', 'm', 'b', 't'][foo.steps];
// "123.457m"

It's not much but one simplification of your code is to eliminate the lower-bound checking since the if/else-if structure will take care of that:
function abb(m) {
if (m < 1) {
// some kind of error?
} else if (m < 1000) {
return m;
} else if (m < 1000000) {
return (m/1000).toFixed(3) + "k";
} else if (m < 1000000000) {
return (m/1000000).toFixed(3) + "m";
} else {
// and so on
}
}
You could do something more clever but I think it is probably best to be simple about it.
But if I understand your code correctly, you're trying to format numbers as thousands ("k"), millions ("m"), billions ("b") etc. Why not look at a library for this such as http://numeraljs.com/?
Specifically, you can use this code:
var numeral = require('numeral').language('en');
to import the numeral.js library, and then this code to perform the formatting:
var str = numeral(12345678).format("0.0a");// "12.3m"
var str = numeral(1234).format("0.0a");// "1.2k"
etc.

Full disclosure: this solution comes with the benefit of no for loop, but it does have some added complexity.
Take the logarithm of m. If 1 <= m< 999 then 0 <= exponent < 3. Similarly, if 1000 <= m < 999999 then 3 <= exponent < 6, and so on. You can see the pattern here, every prefix covers a range of 3 in the logarithmic scale. So you could do something like the following:
function abb(m){
var prefixes = ["","k","m","b","t"];
var log = Math.log10(m);
var prefixRange = parseInt(log/3, 10);
var prefix = prefixes[prefixRange];
return (m / Math.pow(1000, prefixRange)).toFixed(3) + prefix;
}
Actually, in the case of very large numbers, the above will break, so prefixRange = parseInt(log/3, 10) needs to be changed to prefixRange = Math.min(parseInt(log/3, 10), 4),to ensure we don't read beyond the length of array prefixes.
Also, notice that toFixed is not really to be trusted for large numbers in this case. 999999999 / 1000000 gives 1000.000, which I guess is not what you want. Better to round with floor in this case. Since you are only interested in the first digits (3 for the integer and 3 for the decimal part), you can first get those 6 digits and then divide by 1000.
A better function, addressing both issues is:
function abb(m){
var prefixes = ["","k","m","b","t"];
var log = Math.log10(m);
var prefixRange = Math.min(parseInt(log/3, 10), 4);
var prefix = prefixes[prefixRange];
return Math.floor((m / Math.pow(1000, prefixRange - 1)))/1000 + prefix;
}

Related

LeetCode 125: Palindrome Number Easy Leetcode [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my answer. However, I couldn't pass the test case for "11".
I couldn't find what is wrong in the code. Please help! Thank you!
/**
* #param {number} x
* #return {boolean}
*/
var isPalindrome = function(x) {
if (x === 0) {
return true;
}
if (x < 0 || x % 10 === 0) {
return false;
}
let rev = 0;
while (x > rev) {
pop = x % 10;
x = x / 10;
rev = (rev * 10) + pop;
}
if (x === rev || x === rev / 10) {
return true;
}
else {
return false;
}
};
Finding palindromes is inherently something which you would typically do using strings, not numeric variables, so I suggest converting your number to a string, and going from there:
var isPalindrome = function(x) {
x = x + ""; // convert to string, if x be a number
var isPalindrome = true;
for (i = 0; i < x.length/2; i++) {
if (x.substring(i, i+1) != x.substring(x.length-1-i, x.length-i)) {
isPalindrome = false;
break;
}
}
return isPalindrome;
}
console.log(isPalindrome(1234321));
console.log(isPalindrome(1234329));
The strategy here is just to iterate half the string, and assert that each character matches its counterpart in the other half. Note that we don't need to check the middle character, in the case of an input with an odd number of characters.
Your question seems to be LeetCode 9 and in the discussion board, there are good accepted solutions such as:
JavaScript
var isPalindrome = function(x) {
if (x < 0)
return false;
let reversed = 0;
for (let i = x; i > 0; i = Math.floor(i / 10))
reversed = reversed * 10 + i % 10;
return reversed === x;
};
Python
class Solution:
def isPalindrome(self, x):
if x < 0 or (x > 0 and not x % 10):
return False
return str(x) == str(x)[::-1]
Java
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x != 0 && x % 10 == 0))
return false;
int reversed = 0;
while (x > reversed) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return (x == reversed || x == reversed / 10);
}
}
There is another similar isPalindrome question that if you might be interested, I've just copied below:
JavaScript I
var isPalindrome = function(s) {
var original = s.replace(/\W/g, ''); // means NON-WORD characters
var reversed = original.split('').reverse().join('');
return original.toLowerCase() == reversed.toLowerCase();
};
JavaScript II
var isPalindrome = function(s) {
var original = s.replace(/[^a-z0-9]/isg, '');
var reversed = original.split('').reverse().join('');
return original.toLowerCase() == reversed.toLowerCase();
};
Java
class Solution {
public boolean isPalindrome(String s) {
String original = s.replaceAll("(?i)[^a-z0-9]", "").toLowerCase();
String reversed = new StringBuffer(original).reverse().toString();
return original.equals(reversed);
}
}
Python
class Solution:
def isPalindrome(self, s):
s = ''.join(re.findall(r'(?is)[a-z0-9]+', s)).lower()
return s == s[::-1]
\W (non-word-character) matches any single character that doesn't match by \w (same as [^a-zA-Z0-9_]).
Reference
You can find additional explanations in the following links:
LeetCode 9 JavaScript Discussion Board
LeetCode 125 JavaScript Discussion Board
Using string for checking palindrome is very easy and straight forward. Having said that if you want to see how you can do it without changing number to string,
First initialise an variable start with Math.pow(10, digit count-1)
Loop till the value of start is greater than 0
inside loop compare the first and last digit if they are not equal return false
on each iteration remove the first and last digit from x and reduce start by 100
var isPalindrome = function(x) {
// as per question on leetcode negative values cannot be palindrome
if( x < 0) {
return false
}
x = Math.abs(x)
// to get the digits from start we need to get log10 of given value
let len = Math.ceil( Math.max( Math.log10(x), 1 ) ) - 1
let start = Math.pow(10, len)
while(start){
// compare first digit with the last digit
if(Math.floor(x/start) != (x % 10)){
return false
}
// remove first digit of current x
x = x % start
// remove last digit of current x
x = Math.floor(x/10)
// reduce start by 100 as we removed 2 digits
start = Math.floor(start / 100)
}
return true
};
console.log(isPalindrome(1))
console.log(isPalindrome(1221))
console.log(isPalindrome(-121))
console.log(isPalindrome(12341))
console.log(isPalindrome(100111))
Note:- We do (digit count - 1) so that we can capture the first digit
Original leetcode question link

How to split numbers into individual

I have this whole number 75892. i want to split it in individual and then add below is my code. it works fine but is there any other solution to minimize my code? thanks
var x=75892;
var d,ans=0;
while(x!=0){
d=x%10;
ans=ans+d;
x=parseInt(x/10);
}
document.write("SUM:"+ans);
That approach is fine other than the parseInt part. parseInt will first convert the number to string, then parse that string. You wanted Math.floor:
x = Math.floor(x / 10);
You can also use += and just generally there are a couple of shortcuts you could take:
var x = 75892;
var ans = 0;
while (x != 0) {
ans += x % 10;
x = Math.floor(x / 10);
}
document.write("SUM:" + ans);
Another approach would be to go through the digits, e.g. a string-based approach instead of a math-based one. But frankly, I don't think it would be better.
You wanted shorter - here's a one-line ES6 version:
var ans = [...(String(x))].map(Number).reduce((p, c) => p + c);
Use spread on a stringified version of x, convert them to numbers, then use reduce to sum the numbers.
DEMO
var x="" + 75892;
var sum = 0;
for( var i = 0; i < x.length; i++ )
{
sum += parseInt( x[i] );
}
console.log( "sum: " + sum );
Please try this once.
Use, please review Sum of a string of one-digit numbers in javascript?
var sum = function(a,b){return a+b}
function stringSum(s) {
var int = function(x){return parseInt(x,10)}
return s.split('').map(int).reduce(sum);
}
document.write(stringSum("75892"))
You just need to split the variable-
var x=75892;
var arr = x.split('');
var count=0;
for (var i=arr.length; i--;) {
count = count+parseInt(arr[i]);
}
The following code is, when i was thought in college days, Now share with you. Beauty is there is no loop.
var x=75892;
var d,ans = 0;
x = parseInt(x);
if(x < 0){
x = -1 * x;
}
if(x > 0){
d = x % 9;
if(d == 0){
ans = 9;
}else{
ans = d;
}
}
document.write("SUM:"+ans);
If you're asking if there's a smarter way, than just peeling off digits at the end, then no - not in the decimal system at least.
Unless you want to reduce to a single digit (eg. 75892 -> 31 -> 4) in which case
function(d){
if(d === 0){
return 0;
}
var m = d%9;
return m === 0 ? 9 : m;
}
will do the trick

JavaScript - Improving algorithm for finding square roots of perfect squares without Math.sqrt

I'm trying to learn algorithms and coding stuff by scratch. I wrote a function that will find square roots of square numbers only, but I need to know how to improve its performance and possibly return square roots of non square numbers
function squareroot(number) {
var number;
for (var i = number; i >= 1; i--) {
if (i * i === number) {
number = i;
break;
}
}
return number;
}
alert(squareroot(64))
Will return 8
Most importantly I need to know how to improve this performance. I don't really care about its limited functionality yet
Here is a small improvement I can suggest. First - start iterating from 0. Second - exit loop when the square of root candidate exceeds the number.
function squareroot(number) {
for (var i = 0; i * i <= number; i++) {
if (i * i === number)
return i;
}
return number; // don't know if you should have this line in case nothing found
}
This algo will work in O(√number) time comparing to initial O(n) which is indeed performance improvement that you asked.
Edit #1
Just even more efficient solution would be to binary search the answer as #Spektre suggested. It is known that x2 is increasing function.
function squareroot(number) {
var lo = 0, hi = number;
while(lo <= hi) {
var mid = Math.floor((lo + hi) / 2);
if(mid * mid > number) hi = mid - 1;
else lo = mid + 1;
}
return hi;
}
This algo has O(log(number)) running time complexity.
The stuff that you try to do is called numerical methods. The most rudimentary/easy numerical method for equation solving (yes, you solve an equation x^2 = a here) is a Newtons method.
All you do is iterate this equation:
In your case f(x) = x^2 - a and therefore f'(x) = 2x.
This will allow you to find a square root of any number with any precision. It is not hard to add a step which approximate the solution to an integer and verifies whether sol^2 == a
function squareRoot(n){
var avg=(a,b)=>(a+b)/2,c=5,b;
for(let i=0;i<20;i++){
b=n/c;
c=avg(b,c);
}
return c;
}
This will return the square root by repeatedly finding the average.
var result1 = squareRoot(25) //5
var result2 = squareRoot(100) //10
var result3 = squareRoot(15) //3.872983346207417
JSFiddle: https://jsfiddle.net/L5bytmoz/12/
Here is the solution using newton's iterative method -
/**
* #param {number} x
* #return {number}
*/
// newstons method
var mySqrt = function(x) {
if(x==0 || x == 1) return x;
let ans, absX = Math.abs(x);
let tolerance = 0.00001;
while(true){
ans = (x+absX/x)/2;
if(Math.abs(x-ans) < tolerance) break;
x = ans;
}
return ans;
};
Separates Newton's method from the function to approximate. Can be used to find other roots.
function newton(f, fPrime, tolerance) {
var x, first;
return function iterate(n) {
if (!first) { x = n; first = 1; }
var fn = f(x);
var deltaX = fn(n) / fPrime(n);
if (deltaX > tolerance) {
return iterate(n - deltaX)
}
first = 0;
return n;
}
}
function f(n) {
return function(x) {
if(n < 0) throw n + ' is outside the domain of sqrt()';
return x*x - n;
};
}
function fPrime(x) {
return 2*x;
}
var sqrt = newton(f, fPrime, .00000001)
console.log(sqrt(2))
console.log(sqrt(9))
console.log(sqrt(64))
Binary search will work best.
let number = 29;
let res = 0;
console.log((square_root_binary(number)));
function square_root_binary(number){
if (number == 0 || number == 1)
return number;
let start = 0;
let end = number;
while(start <= end){
let mid = ( start + end ) / 2;
mid = Math.floor(mid);
if(mid * mid == number){
return mid;
}
if(mid * mid < number){
start = mid + 1;
res = mid;
}
else{
end = mid - 1;
}
}
return res;
}
If you analyze all natural numbers with their squares you might spot a pattern...
Numbers Squares Additives
1 1 3
2 4 5
3 9 7
4 16 9
5 25 11
6 36 13
7 49 15
Look at the first row in the squares column (i.e 1) and add it with the first row in the additives column (ie. 3). You will get four which is in the second row of the squares column.
If you keep repeating this you'll see that this applies to all squares of natural numbers. Now if you look at the additives column, all the numbers below are actually odd.
To find the square root of a perfect square you should keep on subtracting it with consecutive odd numbers (starting from one) until it is zero. The number of times it could be subtracted is the square root of that number.
This is my solution in typescript...
function findSquareRoot(number: number): number {
for (let i = 1, count = 0; true; number -= i, i += 2, count++) {
if (number <= 0) {
return number === 0 ? count : -1; // -1 if number is not a perfect square
}
}
}
Hopefully this has better time complexity :)
I see this solution on Github which is the much better and easiest approach to take a square root of a number without using any external library
function TakingPerfectSquare(Num) {
for (var i = 0; i <= Num; i++) {
var element = i;
if ((element == element) && (element*element == Num)) {
return true;
}
}
return false;
}
console.log(TakingPerfectSquare(25));

Implementation of Luhn algorithm

I am trying to implement simple validation of credit card numbers. I read about the Luhn algorithm on Wikipedia:
Counting from the check digit, which is the rightmost, and moving
left, double the value of every second digit.
Sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5)
together with the undoubled digits from the original number.
If the total modulo 10 is equal to 0 (if the total ends in zero)
then the number is valid according to the Luhn formula; else it is
not valid.
On Wikipedia, the description of the Luhn algorithm is very easily understood. However, I have also seen other implementations of the Luhn algorithm on Rosetta Code and elsewhere (archived).
Those implementations work very well, but I am confused about why they can use an array to do the work. The array they use seems to have no relation with Luhn algorithm, and I can't see how they achieve the steps described on Wikipedia.
Why are they using arrays? What is the significance of them, and how are they used to implement the algorithm as described by Wikipedia?
Unfortunately none of the codes above worked for me. But I found on GitHub a working solution
// takes the form field value and returns true on valid number
function valid_credit_card(value) {
// accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
var nCheck = 0, nDigit = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n),
nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9) nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}
return (nCheck % 10) == 0;
}
the array [0,1,2,3,4,-4,-3,-2,-1,0] is used as a look up array for finding the difference between a number in 0-9 and the digit sum of 2 times its value. For example, for number 8, the difference between 8 and (2*8) = 16 -> 1+6 = 7 is 7-8 = -1.
Here is graphical presentation, where {n} stand for sum of digit of n
[{0*2}-0, {1*2}-1, {2*2}-2, {3*2}-3, {4*2}-4, {5*2}-5, {6*2}-6, {7*2}-7....]
| | | | | | | |
[ 0 , 1 , 2 , 3 , 4 , -4 , -3 , -2 ....]
The algorithm you listed just sum over all the digit and for each even spot digit, look up the the difference using the array, and apply it to the total sum.
Compact Luhn validator:
var luhn_validate = function(imei){
return !/^\d+$/.test(imei) || (imei.split('').reduce(function(sum, d, n){
return sum + parseInt(((n + imei.length) %2)? d: [0,2,4,6,8,1,3,5,7,9][d]);
}, 0)) % 10 == 0;
};
Works fine for both CC and IMEI numbers. Fiddle: http://jsfiddle.net/8VqpN/
Lookup tables or arrays can simplify algorithm implementations - save many lines of code - and with that increase performance... if the calculation of the lookup index is simple - or simpler - and the array's memory footprint is affordable.
On the other hand, understanding how the particular lookup array or data structure came to be can at times be quite difficult, because the related algorithm implementation may look - at first sight - quite different from the original algorithm specification or description.
Indication to use lookup tables are number oriented algorithms with simple arithmetics, simple comparisons, and equally structured repetition patterns - and of course - of quite finite value sets.
The many answers in this thread go for different lookup tables and with that for different algorithms to implement the very same Luhn algorithm. Most implementations use the lookup array to avoid the cumbersome figuring out of the value for doubled digits:
var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
//
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// | | | | | | | | | |
//
// - d-igit=index: 0 1 2 3 4 5 6 7 8 9
// - 1st
// calculation: 2*0 2*2 2*2 2*3 2*4 2*5 2*6 2*7 2*8 2*9
// - intermeduate
// value: = 0 = 2 = 4 = 6 = 8 =10 =12 =14 =16 =18
// - 2nd
// calculation: 1+0 1+2 1+4 1+6 1+8
//
// - final value: 0 2 4 6 8 =1 =3 =5 =7 =9
//
var luhnFinalValue = luhnArray[d]; // d is numeric value of digit to double
An equal implementation for getting the luhnFinalValue looks like this:
var luhnIntermediateValue = d * 2; // d is numeric value of digit to double
var luhnFinalValue = (luhnIntermediateValue < 10)
? luhnIntermediateValue // (d ) * 2;
: luhnIntermediateValue - 10 + 1; // (d - 5) * 2 + 1;
Which - with the comments in above true and false terms - is of course simplified:
var luhnFinalValue = (d < 5) ? d : (d - 5) * 2 + 1;
Now I'm not sure if I 'saved' anything at all... ;-) especially thanks the value-formed or short form of if-then-else. Without it, the code may look like this - with 'orderly' blocks
and embedded in the next higher context layer of the algorithm and therefore luhnValue:
var luhnValue; // card number is valid when luhn values for each digit modulo 10 is 0
if (even) { // even as n-th digit from the the end of the string of digits
luhnValue = d;
} else { // doubled digits
if (d < 5) {
luhnValue = d * 2;
} else {
lunnValue = (d - 5) * 2 + 1;
}
}
Or:
var luhnValue = (even) ? d : (d < 5) ? d * 2 : (d - 5) * 2 + 1;
Btw, with modern, optimizing interpreters and (just in time) compilers, the difference is only in the source code and matters only for readability.
Having come that far with explanation - and 'justification' - of the use of lookup tables and comparison to straight forward coding, the lookup table looks now a bit overkill to me. The algorithm without is now quite easy to finish - and it looks pretty compact too:
function luhnValid(cardNo) { // cardNo as a string w/ digits only
var sum = 0, even = false;
cardNo.split("").reverse().forEach(function(dstr){ d = parseInt(dstr);
sum += ((even = !even) ? d : (d < 5) ? d * 2 : (d - 5) * 2 + 1);
});
return (sum % 10 == 0);
}
What strikes me after going through the explanation exercise is that the initially most enticing implementation - the one using reduce() from #kalypto - just lost totally its luster for me... not only because it is faulty on several levels, but more so because it shows that bells and whistles may not always 'ring the victory bell'. But thank you, #kalypto, it made me actually use - and understand - reduce():
function luhnValid2(cardNo) { // cardNo as a string w/ digits only
var d = 0, e = false; // e = even = n-th digit counted from the end
return ( cardNo.split("").reverse().reduce(
function(s,dstr){ d = parseInt(dstr); // reduce arg-0 - callback fnc
return (s + ((e = !e) ? d : [0,2,4,6,8,1,3,5,7,9][d]));
} // /end of callback fnc
,0 // reduce arg-1 - prev value for first iteration (sum)
) % 10 == 0
);
}
To be true to this thread, some more lookup table options have to be mentioned:
how about just adjust varues for doubled digits - as posted by #yngum
how about just everything with lookup tables - as posted by #Simon_Weaver - where also the values for the non-doubled digits are taken from a look up table.
how about just everything with just ONE lookup table - as inspired by the use of an offset as done in the extensively discussed luhnValid() function.
The code for the latter - using reduce - may look like this:
function luhnValid3(cardNo) { // cardNo as a string w/ digits only
var d = 0, e = false; // e = even = n-th digit counted from the end
return ( cardNo.split("").reverse().reduce(
function(s,dstr){ d = parseInt(dstr);
return (s + [0,1,2,3,4,5,6,7,8,9,0,2,4,6,8,1,3,5,7,9][d+((e=!e)?0:10)]);
}
,0
) % 10 == 0
);
}
And for closing lunValid4() - very compact - and using just 'old fashioned' (compatible) JavaScript - with one single lookup table:
function luhnValid4(cardNo) { // cardNo as a string w/ digits only
var s = 0, e = false, p = cardNo.length; while (p > 0) { p--;
s += "01234567890246813579".charAt(cardNo.charAt(p)*1 + ((e=!e)?0:10)) * 1; }
return (s % 10 == 0);
}
Corollar: Strings can be looked at as lookup tables of characters... ;-)
A perfect example of a nice lookup table application is the counting of set bits in bits lists - bits set in a a (very) long 8-bit byte string in (an interpreted) high-level language (where any bit operations are quite expensive). The lookup table has 256 entries. Each entry contains the number of bits set in an unsigned 8-bit integer equal to the index of the entry. Iterating through the string and taking the unsigned 8-bit byte equal value to access the number of bits for that byte from the lookup table. Even for low-level language - such as assembler / machine code - the lookup table is the way to go... especially in an environment, where the microcode (instruction) can handle multiple bytes up to 256 or more in an (single CISC) instruction.
Some notes:
numberString * 1 and parseInt(numberStr) do about the same.
there are some superfluous indentations, parenthesis,etc... supporting my brain in getting the semantics quicker... but some that I wanted to leave out, are actually required... when
it comes to arithmetic operations with short-form, value-if-then-else expressions as terms.
some formatting may look new to you; for examples, I use the continuation comma with the
continuation on the same line as the continuation, and I 'close' things - half a tab - indented to the 'opening' item.
All formatting is all done for the human, not the computer... 'it' does care less.
algorithm datastructure luhn lookuptable creditcard validation bitlist
A very fast and elegant implementation of the Luhn algorithm following:
const isLuhnValid = function luhn(array) {
return function (number) {
let len = number ? number.length : 0,
bit = 1,
sum = 0;
while (len--) {
sum += !(bit ^= 1) ? parseInt(number[len], 10) : array[number[len]];
}
return sum % 10 === 0 && sum > 0;
};
}([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]);
console.log(isLuhnValid("4112344112344113".split(""))); // true
console.log(isLuhnValid("4112344112344114".split(""))); // false
On my dedicated git repository you can grab it and retrieve more info (like benchmarks link and full unit tests for ~50 browsers and some node.js versions).
Or you can simply install it via bower or npm. It works both on browsers and/or node.
bower install luhn-alg
npm install luhn-alg
If you want to calculate the checksum, this code from this page is very concise and in my random tests seems to work.
NOTE: the verification algorithmns on this page do NOT all work.
// Javascript
String.prototype.luhnGet = function()
{
var luhnArr = [[0,1,2,3,4,5,6,7,8,9],[0,2,4,6,8,1,3,5,7,9]], sum = 0;
this.replace(/\D+/g,"").replace(/[\d]/g, function(c, p, o){
sum += luhnArr[ (o.length-p)&1 ][ parseInt(c,10) ]
});
return this + ((10 - sum%10)%10);
};
alert("54511187504546384725".luhnGet());​
Here's my findings for C#
function luhnCheck(value) {
return 0 === (value.replace(/\D/g, '').split('').reverse().map(function(d, i) {
return +['0123456789','0246813579'][i % 2][+d];
}).reduce(function(p, n) {
return p + n;
}) % 10);
}
Update: Here's a smaller version w/o string constants:
function luhnCheck(value) {
return !(value.replace(/\D/g, '').split('').reverse().reduce(function(a, d, i) {
return a + d * (i % 2 ? 2.2 : 1) | 0;
}, 0) % 10);
}
note the use of 2.2 here is to make doubling d roll over with an extra 1 when doubling 5 to 9.
Code is the following:
var LuhnCheck = (function()
{
var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
return function(str)
{
var counter = 0;
var incNum;
var odd = false;
var temp = String(str).replace(/[^\d]/g, "");
if ( temp.length == 0)
return false;
for (var i = temp.length-1; i >= 0; --i)
{
incNum = parseInt(temp.charAt(i), 10);
counter += (odd = !odd)? incNum : luhnArr[incNum];
}
return (counter%10 == 0);
}
})();
The variable counter is the sum of all the digit in odd positions, plus the double of the digits in even positions, when the double exceeds 10 we add the two numbers that make it (ex: 6 * 2 -> 12 -> 1 + 2 = 3)
The Array you are asking about is the result of all the possible doubles
var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
0 * 2 = 0 --> 0
1 * 2 = 2 --> 2
2 * 2 = 4 --> 4
3 * 2 = 6 --> 6
4 * 2 = 8 --> 8
5 * 2 = 10 --> 1+0 --> 1
6 * 2 = 12 --> 1+2 --> 3
7 * 2 = 14 --> 1+4 --> 5
8 * 2 = 16 --> 1+6 --> 7
9 * 2 = 18 --> 1+8 --> 9
So for example
luhnArr[3] --> 6 (6 is in 3rd position of the array, and also 3 * 2 = 6)
luhnArr[7] --> 5 (5 is in 7th position of the array, and also 7 * 2 = 14 -> 5 )
Another alternative:
function luhn(digits) {
return /^\d+$/.test(digits) && !(digits.split("").reverse().map(function(checkDigit, i) {
checkDigit = parseInt(checkDigit, 10);
return i % 2 == 0
? checkDigit
: (checkDigit *= 2) > 9 ? checkDigit - 9 : checkDigit;
}).reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
}) % 10);
}
Alternative ;) Simple and Best
<script>
// takes the form field value and returns true on valid number
function valid_credit_card(value) {
// accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
var nCheck = 0, nDigit = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n),
nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9) nDigit -= 9;
}
nCheck += nDigit;
bEven = !bEven;
}
return (nCheck % 10) == 0;
}
console.log(valid_credit_card("5610591081018250"),"valid_credit_card Validation");
</script>
Best Solution here
http://plnkr.co/edit/34aR8NUpaKRCYpgnfUbK?p=preview
with all test cases passed according to
http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
and the credit goes to
https://gist.github.com/DiegoSalazar/4075533
const LuhnCheckCard = (number) => {
if (/[^0-9-\s]+/.test(number) || number.length === 0)
return false;
return ((number.split("").map(Number).reduce((prev, digit, i) => {
(!(( i & 1 ) ^ number.length)) && (digit *= 2);
(digit > 9) && (digit -= 9);
return prev + digit;
}, 0) % 10) === 0);
}
console.log(LuhnCheckCard("4532015112830366")); // true
console.log(LuhnCheckCard("gdsgdsgdsg")); // false
I worked out the following solution after I submitted a much worse one for a test..
function valid(number){
var splitNumber = parseInt(number.toString().split(""));
var totalEvenValue = 0;
var totalOddValue = 0;
for(var i = 0; i < splitNumber.length; i++){
if(i % 2 === 0){
if(splitNumber[i] * 2 >= 10){
totalEvenValue += splitNumber[i] * 2 - 9;
} else {
totalEvenValue += splitNumber[i] * 2;
}
}else {
totalOddValue += splitNumber[i];
}
}
return ((totalEvenValue + totalOddValue) %10 === 0)
}
console.log(valid(41111111111111111));
I recently wrote a solution using Javascript, I leave the code here for anyone who can help:
// checksum with Luhn Algorithm
const luhn_checksum = function(strIn) {
const len = strIn.length;
let sum = 0
for (let i = 0; i<10; i += 1) {
let factor = (i % 2 === 1) ? 2: 1
const v = parseInt(strIn.charAt(i), 10) * factor
sum += (v>9) ? (1 + v % 10) : v
}
return (sum * 9) % 10
}
// teste exampple on wikipedia:
// https://en.wikipedia.org/wiki/Luhn_algorithm
const strIn = "7992739871"
// The checksum of "7992739871" is 3
console.log(luhn_checksum(strIn))
If you understand this code above, you will have no problem converting it to any other language.
For example in python:
def nss_checksum(nss):
suma = 0
for i in range(10):
factor = 2 if (i % 2 == 1) else 1
v = int(nss[i]) * factor
suma += (1 + v % 10) if (v >9) else v
return (suma * 9) % 10
For more info, check this:
https://en.wikipedia.org/wiki/Luhn_algorithm
My Code(En español):
https://gist.github.com/fitorec/82a3e27fae3bab709a07c19c71c3a8d4
def validate_credit_card_number(card_number):
if(len(str(card_number))==16):
group1 = []
group1_double = []
after_group_double = []
group1_sum = 0
group2_sum = 0
group2 = []
total_final_sum = 0
s = str(card_number)
list1 = [int(i) for i in list(s)]
for i in range(14, -1, -2):
group1.append(list1[i])
for x in group1:
b = 0
b = x * 2
group1_double.append(b)
for j in group1_double:
if(j > 9):
sum_of_digits = 0
alias = str(j)
temp1 = alias[0]
temp2 = alias[1]
sum_of_digits = int(temp1) + int(temp2)
after_group_double.append(sum_of_digits)
else:
after_group_double.append(j)
for i in after_group_double:
group1_sum += i
for i in range(15, -1, -2):
group2.append(list1[i])
for i in group2:
group2_sum += i
total_final_sum = group1_sum + group2_sum
if(total_final_sum%10==0):
return True
else:
return False
card_number= 1456734512345698 #4539869650133101 #1456734512345698 # #5239512608615007
result=validate_credit_card_number(card_number)
if(result):
print("credit card number is valid")
else:
print("credit card number is invalid")

Is there a good javascript snippet anyone knows for formatting "abbreviated" numbers?

The key is abbreviated. For example, 1m instead of 1000000, and 12k instead of 12000 etc. - much like on StackOverflow!
I'm not sure what else to add other than I've tried:
format numbers abbreviated javascript
format numbers short javascript
And a few other searches and scoured the results with no luck. I feel like someone must have done this before, hate reinventing wheels and all that!
Cheers
Edit: I'm looking to take a number, i.e. 12345 and turn it into 12k
Sorry I wasn't very clear!
Here's some code I've written quite some time ago but it works fine. It even supports decimals.
function is_numeric(string) {
for(var i = 0; i < string.length; i++) {
if(string.charAt(i) < '0' || string.charAt(i) > '9') {
return false;
}
}
return true;
}
function charValueMultiplier(letter) {
switch(letter) {
case 'M':
case 'm': return 1000000;
case 'k':
case 'K': return 1000;
default: return 0;
}
}
// parse string like 1.5M or 10k and return the number
function parseNumber(string) {
string = string.replace(/ /g, ''); // remove spaces
var total = 0;
var partial = 0;
var partialFraction = 0;
var fractionLength = 0;
var isFraction = false;
// process the string; update total if we find a unit character
for(var i = 0; i < string.length; i++) {
var c = string.substr(i, 1);
if(c == '.' || c == ',') {
isFraction = true;
}
else if(is_numeric(c)) {
if(isFraction) {
partialFraction = partialFraction * 10 + parseInt(c, 10);
fractionLength++;
}
else {
partial = partial * 10 + parseInt(c, 10);
}
}
else {
total += charValueMultiplier(c) * partial + charValueMultiplier(c) * partialFraction / Math.pow(10, fractionLength);
partial = 0;
partialFraction = 0;
fractionLength = 0;
isFraction = false;
}
}
return Math.round(total + partial + partialFraction / Math.pow(10, fractionLength));
}
I made an npm package that can do this for you: https://www.npmjs.com/package/approximate-number
Usage for Node.js (or browsers via Browserify):
npm install --save approximate-number
and then in your code:
var approx = require('approximate-number');
approx(123456); // "123k"
Or, for usage in browsers via Bower:
bower install approximate-number
And then
window.approximateNumber(123456); // "123k"
If I understand correctly, you have a number n and want to format it to a string. Then
// n being the number to be formatted
var s = "" + n; // cast as string
if (n >= 1000000) {
s = s.substring(0, s.length - 6) + "M";
} else if (n >= 1000) {
s = s.substring(0, s.length - 3) + "k";
}
should do the job. You can of course extend it to your needs or even include numbers < 1.

Categories

Resources