I have this code below:
<html>
<head>
<title> Eloquent Javascript Ex 2 </title>
<script language = "javascript">
/*
function twoPowerTen()
{
var result = 1;
var counter = 0;
while( counter < 10 )
{
result = 2 * result;
document.writeln( result );
counter = counter + 1;
}
}
int limit = 1;
for( int y = 0; y < 5; y++ )
{
for( int x = 0; x < limit; x++ )
{
System.out.print( "*" );
}
System.out.println();
limit = limit + 1;
}
*/
function drawTriangle()
{
var limit = 1;
for( var y = 0; y < 5; y++ )
{
for( var x = 0; x < limit; x++ )
{
document.write( "*" );
}
document.write( "<br>" );
limit = limit + 1;
}
}
function whileDrawTriangle()
{
var limit = 1;
var x, y = 0;
while( y < 5 )
{
while( x < limit )
{
document.write( "*" );
x++;
}
limit = limit + 1;
y++;
}
}
</script>
</head>
<body>
<input type = "button" value = "Draw # Triangle" onClick = "whileDrawTriangle()" />
</body>
</html>
which doesn’t seem to want to run when I hit the button – it’s suppose to print a triangle of astericks to the screen. BTW I know the HTML isn’t well constructed but the point is to just implement basic javascript as Im getting to grips with it’s fundamentals.
What’s the problem?
You need to initialize you variable x to 0;