Infolinks

Breaking

Adsense

Friday, April 22, 2016

CREATE A MULTIPLICATION TABLE USING FOR LOOP IN JAVASCRIPT

This is another example of a program that uses controlled structures using a looping statement. This time I did use the FOR loop using javascript language. On my previous article, I define loop as a structure that can execute a block of code several times as long as a specified condition is true. Loops are important to minimize codes that are executed for several times.  I also created similar program using php language.

Can you imagine creating a multiplication table like on the output below without using iteration? I know it’s cumbersome and will take lots of your effort and time. A multiplication table program will use two (2) iterations to display rows and columns. The first loop will specify the multiplicand which is from 1 to 10 and the second loop will specify the multiplier which is from 1 to 10 also.

JAVASCRIPT CODE FOR MULTIPLICATION TABLE

<html>
<head>
<script>
var multiplicand;
var multiplier;
document.writeln("<table>");
for (multiplicand=1;multiplicand<=10;multiplicand++)
{
document.writeln("<tr>");
for (multiplier=1;multiplier<=10;multiplier++)
{
document.writeln("<td>");
document.writeln(multiplier+'x'+multiplicand+"=");
document.writeln(multiplicand*multiplier);
document.writeln("</td>");
}
document.writeln("<tr>");
}
document.writeln("</table>");
</script>
</head>
</html>

SAMPLE OUTPUT FOR MULTIPLICATION TABLE

Hope it works for you! ^_^

CREATE A MULTIPLICATION TABLE USING FOR LOOP IN JAVASCRIPT

No comments:

Post a Comment

Adbox