Valid credit card - Yanina Mercado#50
Open
YaninaMR wants to merge 3 commits intoLaboratoria-learning:masterfrom
Open
Valid credit card - Yanina Mercado#50YaninaMR wants to merge 3 commits intoLaboratoria-learning:masterfrom
YaninaMR wants to merge 3 commits intoLaboratoria-learning:masterfrom
Conversation
rlazo
reviewed
Dec 16, 2017
| @@ -0,0 +1,37 @@ | |||
| var num = prompt('Input credit number'); | |||
There was a problem hiding this comment.
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))) { |
There was a problem hiding this comment.
!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(''); |
There was a problem hiding this comment.
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++) { |
| 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)); |
There was a problem hiding this comment.
Este return es muy largo y complicado para usar a ? b : c. Dividelo en varias lineas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.