Introduction

This article describes how to play a love calculator in JavaScript. A love calculator is an application that allows you to know how compatible you and your partner are and how strong shall be the depth of love between you and your dear one.
Coding
Love_Calculator.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <script type="text/javascript">
  6. </script>
  7. <style type="text/css">
  8. #Text1
  9. {
  10. z-index: 1;
  11. left: 451px;
  12. top: 169px;
  13. position: absolute;
  14. width: 28px;
  15. height: 21px;
  16. }
  17. #Text2
  18. {
  19. z-index: 1;
  20. left: 437px;
  21. top: 88px;
  22. position: absolute;
  23. }
  24. .style12
  25. {
  26. width: 138px;
  27. }
  28. .style13
  29. {
  30. width: 178px;
  31. }
  32. #Button1
  33. {
  34. width: 163px;
  35. }
  36. .style14
  37. {
  38. width: 184px;
  39. }
  40. #result
  41. {
  42. width: 27px;
  43. z-index: 1;
  44. left: 511px;
  45. top: 155px;
  46. position: absolute;
  47. height: 22px;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <script type="text/javascript">
  53. function calculateLove()
  54. {
  55. Fname = document.getElementById('firstname').value.toUpperCase();
  56. FnameLength = Fname.length;
  57. Sname = document.getElementById('secondname').value.toUpperCase();
  58. SnameLength = Sname.length;
  59. var lovecount=0;
  60. for(var i=0;i<FnameLength;i++)
  61. {
  62. var L1=Fname.substring(i,i+1);
  63. if(L1=='A') lovecount +=3;
  64. if(L1=='E') lovecount +=3;
  65. if(L1=='I') lovecount +=3;
  66. if(L1=='O') lovecount +=3;
  67. if(L1=='U') lovecount +=4;
  68. if(L1=='L') lovecount +=1;
  69. if(L1=='V') lovecount +=4;
  70. }
  71. for (var count = 0; count < SnameLength; count++)
  72. {
  73. var L2 = Sname.substring(count, count + 1);
  74. if(L2=='A') lovecount +=3;
  75. if(L2=='E') lovecount +=3;
  76. if(L2=='I') lovecount +=3;
  77. if(L2=='L') lovecount +=3;
  78. if(L2=='O') lovecount +=4;
  79. if(L2=='V') lovecount +=1;
  80. if(L2=='E') lovecount +=4;
  81. }
  82. var Total = 0;
  83. if (lovecount > 0) Total = 5 - ((FnameLength + SnameLength) / 2)
  84. if (lovecount > 2) Total = 10 - ((FnameLength + SnameLength) / 2)
  85. if (lovecount > 4) Total = 20 - ((FnameLength + SnameLength) / 2)
  86. if (lovecount > 6) Total = 30 - ((FnameLength + SnameLength) / 2)
  87. if (lovecount > 8) Total = 40 - ((FnameLength + SnameLength) / 2)
  88. if (lovecount > 10) Total = 50 - ((FnameLength + SnameLength) / 2)
  89. if (lovecount > 12) Total = 60 - ((FnameLength + SnameLength) / 2)
  90. if (lovecount > 14) Total = 70 - ((FnameLength + SnameLength) / 2)
  91. if (lovecount > 16) Total = 80 - ((FnameLength + SnameLength) / 2)
  92. if (lovecount > 18) Total = 90 - ((FnameLength + SnameLength) / 2)
  93. if (lovecount > 20) Total = 100 - ((FnameLength + SnameLength) / 2)
  94. if (lovecount > 22) Total = 110 - ((FnameLength + SnameLength) / 2)
  95. if (FnameLength == 0 || SnameLength == 0)
  96. Total = "Error";
  97. if (Total < 0) Total = 0;
  98. if (Total > 99) Total = 99;
  99. var txtresult = document.getElementById("result");
  100. txtresult.value = Math.floor(Total).toString();
  101. //alert("Toatal" + Math.floor(Total));
  102. }
  103. </script>
  104. <div style="height: 220px">
  105. <table style="border-style: ridge; border-color: #FF99FF; width: 33%; height: 187px;">
  106. <tr>
  107. <td class="style13">
  108. </td>
  109. <td class="style12" style="color: #FF66FF; font-weight: bold">
  110. Love Calculator</td>
  111. <td class="style14">
  112. </td>
  113. </tr>
  114. <tr>
  115. <td class="style13">
  116. <input id="firstname" type="text" /></td>
  117. <td class="style12">
  118. <img alt="" src="l1.gif" style="height: 98px" /></td>
  119. <td class="style14">
  120. <input id="secondname" type="text" /></td>
  121. </tr>
  122. <tr>
  123. <td class="style13">
  124. </td>
  125. <td class="style12">
  126. <input id="Button1" style="color: #FF66FF; font-weight: bold;" type="button"
  127. value="Calculate" onclick="calculateLove()" /></td>
  128. <td class="style14">
  129. <img id="img" alt="" src="love2.jpg" style="height: 60px; width: 112px" /><input id="result"
  130. readonly="readonly" type="text" style="border-style: hidden" /> </td>
  131. </tr>
  132. </table>
  133. </div>
  134. </body>
  135. </html>
Output 1
Enter a name in the first TextBox, as in:
First-image.jpg
Output 2
Enter a name in the second TextBox, as in:
second-image.jpg
Output 3
Click on the calculate button, then see the results as in the following:
result.jpg
For more information, download the attached sample application.