0

i want to replace every Characters with number from 0 to 5 to make var string = "abcdef"; turn into "01245" i tried this code but result was not what i want

var text = "abcdef";
var num =0;
var tx;
var res;
for (var char in text) {
    tx = text[char];
    res= text.replace(tx,num);
    num = num+1;
    console.log(res);


}

1 Answer 1

2

You can use this

String.prototype.charNumReplace=function(){
    var newstr="";
    var self=this.toLowerCase();
    for(var i =0;i<self.length;i++){
        newstr+=self.charCodeAt(i)-97;
    }
    return newstr;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.