My assignment, and I realize this is pretty basic stuff:
[COLOR=”#0000CD”]Write a JavaScript program embedded in a HTML document with the following requirements:
• Request the user to enter a number
• Check if the user input is not empty. Also check value entered is a number
• Write on the html document a triangle out of the numbers as follow:
E.g. output: (let’s say the user entered number 10)
Your input number is 10.
10
11 11
12 12 12
13 13 13 13
14 14 14 14 14
15 15 15 15 15 15
• The triangle should have 6 rows.
• Use Comments explaining how the program works
My Code so far ( what little there is):
[CODE]<script type=”text/javascript”>
var myNumber = prompt (“Please enter a number”, 1);
var myRows = 6;
var loopCounter;
if ((isNaN(myNumber)) || (myNumber == 0 )) //Working.
{
alert(“Please enter a number.”);
window.location.reload();
}
document.write(“Your input number is ” + myNumber + “.” + “<BR>”);
for (loopCounter = 0; loopCounter <= 19; loopCounter++)
{
document.write(myNumber);
}
</script>
This comes as I read the chapter on Decisions, Loops, and Functions. So I assume it involves a loop to count the number of rows and columns.
I’ve been looking as the JavaScript Loop examples in W3Schools as they are easier to follow than the text book. My thoughts are it should be a While Loop although I couldn’t get even the little bit of code I have to run using While.
In my head I should be taking myNumber and increasing it by one more than the input number and displaying it once. By using the ++ I should be able to increase the original myNumber by one and increase the number of times it’s displayed and so on based on the previous output until 6 rows are displayed. I can’t sort out how to get there.
Thank you!