Skip to content

Valid credit card - Yanina Mercado#50

Open
YaninaMR wants to merge 3 commits intoLaboratoria-learning:masterfrom
YaninaMR:master
Open

Valid credit card - Yanina Mercado#50
YaninaMR wants to merge 3 commits intoLaboratoria-learning:masterfrom
YaninaMR:master

Conversation

@YaninaMR
Copy link

@YaninaMR YaninaMR commented Nov 7, 2017

No description provided.

@@ -0,0 +1,37 @@
var num = prompt('Input credit number');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Es una buena práctica el separar el código que interactua con el usuario, haciendo las llamadas a prompt, del código que hace la verificación. La idea es que siempre puedas describir que hace una función sin utilizar conectores, como 'y'. Esta función pide al usuario que ingrese el número de la tarjeta y la valida.

function isValidCard(num) {
// valid credit number
var val = /^[0-9]*$/;
if (num === '' || (!val.test(num))) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!val.test(num) también va a regresar false si la cadena es vacia, así que puedes simplificar el test a

if (!val.test(num)) {
...
}

num = numValid;
}
// convert creditCard in numbers array
var arr = num.split('');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puedes recorrer la cadena num sin necesidad de hacer split

for (var i = 0; i < num.length; i++) {
num.charAt(i);
}

}
// valid ciphers, credit card and addition digits credit card
var sum = arr[arr.length - 1];
for (i = 0;i < (arr.length - 1);i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agrega espacios entre ; y i.

sum = sum + arr[i];
}
// return valid or not valid and the total sum
return (sum % 10 === 0) ? (alert(num + ': Valid CreditCard ' + ', Total sum :' + sum)) : (alert(num + ': invalid CreditCard' + ',Total sum :' + sum));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este return es muy largo y complicado para usar a ? b : c. Dividelo en varias lineas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants