Jump to content


JavaScript - using point value to a word


3 replies to this topic

#1 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 07 May 2006 - 02:08 AM

As a part of my program, I have to write a code that a user writes a word in a textbox and when user clicks the calculate button, I have to alert them the total point when a and A has a point value 1, b and B has a point value 2, and on and on... and z and Z has a point value 26. How on earth would you write this in a JavaScript? Please help me.

#2 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 07 May 2006 - 12:32 PM

<html>
<head>
</head>
<body>
<script type='text/javascript'>

document.write("<form name='Form'><input type='text' value='please indroduce the text here' name='txt'>");
document.write("<input type='button' value='calculate' onclick='calculate();'></form>");

function calculate()
{
var Sum=0;
for(i=0;i<document.Form.txt.value.length;i++)
{Sum+=document.Form.txt.value.toUpperCase().charCodeAt(i)-64;}
alert(Sum);
}

</script>
</body>
</html>
i tested it and it works
first of all, i put the textbox and the button on the screen
on the button i pun the tag: onclick that has the target the calculate function
the i calculate the required task using Unicode code
enjoy :)
document.Form.txt.value --> the value of the textbox
document.Form.txt.value.toUpperCase() ---> the string with capital letters
document.Form.txt.value.toUpperCase().charCodeAt(i)-64 --> this is the value of each char (a-1, A-1, b-2 ......)
"javascript is my life" and Pasca and C++, learning now PhP

Edited by Futingkiller, 07 May 2006 - 12:34 PM.


#3 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 07 May 2006 - 01:04 PM

Thank you very much. I really appreciate it.

#4 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 09 May 2006 - 08:28 AM

np, i am here to help





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users