Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ok here is the original program that outputs a diamond to the div my project is to make this a two parameter function
- here's the new project
- 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.
- For example, if the second parameter was 5, the diamond should look like this:
- *
- ***
- *****
- ***
- *
- and would be written to the div having the ID given by the first parameter.
- here's the code to s7.1
- <html>
- <head>
- <script type="text/javascript">
- function sumNums()
- {
- // Add the numbers and store the result in sum
- var num1= parseInt(document.getElementById("num1").value)
- if(num1%2==0)
- num1=num1+1;
- scounter = 1
- acounter = 1
- for (i=1; i<=num1; i+=2)
- {
- space = Math.round((num1-i)/2)
- while ( scounter <= space)
- {
- document.getElementById("resultSpot").innerHTML += " "
- scounter= scounter + 1
- }
- while ( acounter <= i)
- {
- document.getElementById("resultSpot").innerHTML += "*";
- acounter= acounter + 1
- }
- document.getElementById("resultSpot").innerHTML += "<br/>";
- scounter= 1
- acounter= 1
- }
- for (i=1; i<=num1; i+=2)
- {
- space2 = Math.round(i/2)
- while (scounter <= space2)
- {
- document.getElementById("resultSpot").innerHTML += " "
- scounter= scounter + 1
- }
- while (acounter <= num1-(i+1))
- {
- document.getElementById("resultSpot").innerHTML += "*";
- acounter= acounter + 1
- }
- document.getElementById("resultSpot").innerHTML += "<br/>";
- scounter= 1
- acounter= 1
- }
- return true;
- }
- // ***********************************
- // A function for clearing the screens
- function clearAll()
- {
- // Clear the result area
- document.getElementById("resultSpot").innerHTML = "";
- // Clear the first number
- document.getElementById("num1").value = "";
- // Clear the second number
- document.getElementById("num2").value = "";
- document.getElementById("num3").value = "";
- document.getElementById("num4").value = "";
- document.getElementById("num5").value = "";
- return true;
- }
- // ***********************************
- // JavaScript to attach the functions to the buttons
- window.onload = function()
- {
- // Attach proper function to calculate button
- document.getElementById("addNums").onclick = sumNums;
- // Attach proper function to the clear button
- document.getElementById("clearNums").onclick = clearAll;
- // Set the initial focus to the first number field
- document.getElementById("num1").focus();
- }
- </script>
- </head>
- <body>
- Number 1:
- <input id="num1" name="num1" type="text" size="10" maxlength="10" />
- <br /><br /><input id ="addNums" type ="button" value="Calculate" />
- <br /><input id ="clearNums" type ="button" value="Clear" />
- <pre><div id="resultSpot">
- </div></pre>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement