Advertisement
Guest User

Nick Schindler

a guest
Dec 9th, 2010
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. Ok here is the original program that outputs a diamond to the div my project is to make this a two parameter function
  2.  
  3. here's the new project
  4. Exercise S8.E1: Modify exercise S7.1 so that the diamond is output by a function. The function should take two parameters. The first is the name of the div where the diamond should be output. The second parameter should be the number of asterisks in the middle row of the diamond. The function should output the diamond to the div.
  5. For example, if the second parameter was 5, the diamond should look like this:
  6. *
  7. ***
  8. *****
  9. ***
  10. *
  11.  
  12. and would be written to the div having the ID given by the first parameter.
  13.  
  14. here's the code to s7.1
  15.  
  16. <html>
  17. <head>
  18. <script type="text/javascript">
  19.  
  20. function sumNums()
  21. {
  22. // Add the numbers and store the result in sum
  23.  
  24. var num1= parseInt(document.getElementById("num1").value)
  25.  
  26.  
  27. if(num1%2==0)
  28. num1=num1+1;
  29.  
  30.  
  31. scounter = 1
  32. acounter = 1
  33.  
  34. for (i=1; i<=num1; i+=2)
  35. {
  36.  
  37. space = Math.round((num1-i)/2)
  38.  
  39.  
  40. while ( scounter <= space)
  41. {
  42. document.getElementById("resultSpot").innerHTML += "&nbsp"
  43. scounter= scounter + 1
  44. }
  45. while ( acounter <= i)
  46. {
  47.  
  48. document.getElementById("resultSpot").innerHTML += "*";
  49. acounter= acounter + 1
  50. }
  51. document.getElementById("resultSpot").innerHTML += "<br/>";
  52. scounter= 1
  53. acounter= 1
  54.  
  55. }
  56.  
  57.  
  58. for (i=1; i<=num1; i+=2)
  59. {
  60.  
  61. space2 = Math.round(i/2)
  62.  
  63. while (scounter <= space2)
  64. {
  65. document.getElementById("resultSpot").innerHTML += "&nbsp"
  66. scounter= scounter + 1
  67. }
  68.  
  69. while (acounter <= num1-(i+1))
  70. {
  71. document.getElementById("resultSpot").innerHTML += "*";
  72. acounter= acounter + 1
  73. }
  74.  
  75. document.getElementById("resultSpot").innerHTML += "<br/>";
  76. scounter= 1
  77. acounter= 1
  78. }
  79. return true;
  80. }
  81.  
  82. // ***********************************
  83. // A function for clearing the screens
  84. function clearAll()
  85. {
  86. // Clear the result area
  87. document.getElementById("resultSpot").innerHTML = "";
  88.  
  89. // Clear the first number
  90. document.getElementById("num1").value = "";
  91.  
  92. // Clear the second number
  93. document.getElementById("num2").value = "";
  94.  
  95. document.getElementById("num3").value = "";
  96.  
  97. document.getElementById("num4").value = "";
  98.  
  99. document.getElementById("num5").value = "";
  100. return true;
  101. }
  102.  
  103. // ***********************************
  104. // JavaScript to attach the functions to the buttons
  105. window.onload = function()
  106. {
  107. // Attach proper function to calculate button
  108. document.getElementById("addNums").onclick = sumNums;
  109.  
  110. // Attach proper function to the clear button
  111. document.getElementById("clearNums").onclick = clearAll;
  112.  
  113. // Set the initial focus to the first number field
  114. document.getElementById("num1").focus();
  115. }
  116. </script>
  117.  
  118.  
  119. </head>
  120.  
  121.  
  122. <body>
  123.  
  124.  
  125. Number 1:
  126. <input id="num1" name="num1" type="text" size="10" maxlength="10" />
  127.  
  128.  
  129. <br /><br /><input id ="addNums" type ="button" value="Calculate" />
  130. <br /><input id ="clearNums" type ="button" value="Clear" />
  131.  
  132. <pre><div id="resultSpot">
  133. </div></pre>
  134.  
  135. </body>
  136. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement