开发者

convert string of letters to numbers and add a zero before javascript

开发者 https://www.devze.com 2023-04-10 13:03 出处:网络
I need to write a javascript program that will ask the user to enter a string of lower‐case characters and then print its corresponding two‐digit code. For example, if

I need to write a javascript program that will ask the user to enter a string of lower‐case characters and then print its corresponding two‐digit code. For example, if the input is “home”, the output should be 08151305. So far I can get it to return the correct numbers, but I cannot get it to add the zero in front of the number if it is a single digit This is what I have:

<html>
<head>
<script type="text/javascript">
function show_prompt()
{
    var name = prompt("Please enter a string of lowercase characters");
    document.write(name,'<br>');
    document.write('Length of the input is ', name.length,'<br>');
    document.write("<br>")


    for (i=0; i < name.length; i++)
    {
        {
        document.write(i, ", ",name.charCodeAt(i) - 96, '<br>');
        }
    }

}
</开发者_运维技巧script>
</head>
<body>

<input type="button" onClick="show_prompt()"value="CSE 201 HW#4 Problem 3"/>

</body>
</html>


Well you can just check if it is a single digit and if so prepend "0":

function padToTwoDigits(c) {
   // convert to string and check length
   c = "" + c;
   return c.length === 1 ? "0" + c : c;

   // or work with it as a number:
   return c >=0 && c <=9 ? "0" + c : c;
}

// then within your existing code
document.write(i, ", ",padToTwoDigits(name.charCodeAt(i) - 96), '<br>');

Of course those are just some ideas to get you started. You can pretty that up somewhat, e.g., you might create a more generic pad function with a parameter to say how many digits to pad to.


You can write your own pad function such as:

function pad(number) {       
     return (number < 10 ? '0' : '') + number       
}

and use the pad function like:

 document.write(i, ", ",pad(name.charCodeAt(i) - 96), '<br>');


Try this.

function show_prompt() {
    var name = prompt("Please enter a string of lowercase characters");
    document.write(name, '<br>');
    document.write('Length of the input is ', name.length, '<br>');
    document.write("<br>")
    for (i = 0; i < name.length; i++) {
        var n = name.charCodeAt(i) - 96;
        document.write(i, ", ", n < 10 ? "0" + n : n, '<br>');
    }

}


I wrote up a quick sample here: http://jsfiddle.net/ZPvZ8/3/

I wrote up a prototype method to String that handles padding zeros.

String.prototype.pad = function(char, len){
    var chars = this.split();
    var initialLen = len - this.length;
    for(var i = 0; i < initialLen; i++){
        chars.unshift(char);
    }

    return chars.join('');
};

I convert it to an array and then add elements with the padding character. I chose to use an array since performing string manipulations is an expensive operation (expensive in memory and CPU usage).

To use it in your program, you'd just call it like this:

    var res = new Array();
    for (var i = 0; i < name.length; i++) {
        var strCode = (name.charCodeAt(i) - 96).toString();
        res.push(strCode.pad('0', 2));
    }
    document.write(res.join(''));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号