'Can not reach final if inside function
Hello I have to validate a number and I have a function for it that was translated from another language to javascript. The first two if conditions work but I can not reach the final if. I tried inserting a correct and an incorrect number and it does not reach the final if.
Can someone tell me why can I not reach the final if ?
function strReverse(str) {
var splitext = str.split("");
var revertext = splitext.reverse();
var reversed = revertext.join("");
return reversed;
}
function validCUI() {
var v = document.getElementById("cui").value;
if (v.length < 2 || v.length > 10) {
alert("Numarul de caractere ale Codului Unic de Identificare trebuie sa fie intre doua si 10 caractere");
document.getElementById("cui").value = "";
}
if (v.charAt(0) === "0") {
alert("Primul caracter al unui Cod Unic de Identificare nu poate fi 0(zero)!\n\nCorectati valoarea introdusa...");
document.getElementById("cui").value = '';
}
alert("xxx");
var key = "753217532";
key = strReverse(key);
var cuirev = strReverse("" + v.valueOf());
var control = cuirev.substring(0, 1);
cuirev = cuirev.substring(1);
var length = cuirev.length;
var suma = 0;
alert("inainte de for");
for (var i = 0; i < length; i++) {
suma += parseInt(cuirev.charAt(i), 10) * parseInt(key.charAt(i), 10);
}
alert("dupa for");
suma *= 10;
alert("inainte de final");
if ((((suma % 11) == 10) && (control == "0")) || (((suma % 11) != 10) && ((suma % 11).toString() == control))) {
alert("CUI corect");
} else {
alert("CUI incorect");
document.getElementById("cui").value = '';
}
}
<input id="cui" type="text" value="" />
<input type="button" onclick="validCUI()" value="validate" />
Solution 1:[1]
Please always check your console for errors first!
function strReverse(v){
var splitext = str.split("");
var revertext = splitext.reverse();
var reversed = revertext.join("");
return reversed;
}
Here, for example you use str
while the argument of the function is v
, I suppose you meant v.split('');
Solution 2:[2]
Change var splitext = str.split("");
to this var splitext = v.split("");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Istvan Tabanyi |
Solution 2 | EzioMercer |