# convert 15 digit Id to 18 digit Id

I have below javascript code which is used to convert 15 digit salesforce ID to 18 digit salesforce Id:

````document.getElementById("convert").addEventListener("click", myFunction); function myFunction(){     var digit = document.getElementById("15digitId").value;      var suffix = '';     var flags;     var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",     isUppercase = function(c) {                 return c >= "A" && c < = "Z";             };     alert(digit.length);      for(var i=0;i < 3; i++){         alert(".. 1st loop.. ");         flags=0;         for(var j=0;j < 5; j++){             alert(digit.substring(i*5+j,i*5+j+1));             var c = digit.substring(i*5+j,i*5+j+1);              if(c.toUpperCase().equals(c) && c >= 'A'&& c <= 'Z'){                 flags = flags + (1 << j);             }          if(flags <= 25){             suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);             }else{             suffix = suffix + '012345'.substring(flags-25,flags-24);          }         }     }     document.getElementById('18digitId').value = digit + suffix; } `
```

This code is working till var c = digit.substring(i*5+j,i*5+j+1); line and after that it stopped working

Replay

This function should output the correct result:

``````function myFunction() {
var idParts = document.getElementById("15digitId").value.match(/(.{5})(.{5})(.{5})/)
var base36 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
var output = []
var outer, inner, subparts, buffer

for(outer = 1; outer <= 3; outer++) {
subparts = idParts[outer].split('')
buffer = 0
for(inner = 4; inner >= 0; inner--) {
buffer = (buffer << 1) | (subparts[inner].match(/[A-Z]/) ? 1 : 0)
}
output.push(base36[buffer])
}
document.getElementById('18digitId').value = value+output.join('')
}
```
```
Category: javascript Time: 2016-07-29 Views: 0
Tags: javascript