简体   繁体   中英

In javascript how to make multiplication table to be generated by default of 5 or any other number, if the user enters no input number?

Write a program to take input a number from user & display it's multiplication table on your browser. If user does not enter a new number, multiplication table of 5 should be displayed by default.

And this is how I want it to be generated with by default table of 5 encoded,

 var num = prompt("Enter Number", "0") //prompt user to enter the number var num = parseInt(num); //parse the num to number var i = 0; document.write('<table border="1" cellspacing="0">'); for (i = 1; i < 10; i++) { document.write("<tr><td>" + num + " x " + i + " = " + num * i + "</td></tr>"); }

This will help you with your problem. It works like this. if parseInt(num) is a truthy(not 0) it'll use parseInt(num) else if it is falsy(0) then it'll use 5

var num = prompt("Enter Number", "0") //prompt user to enter the number
var num = parseInt(num) || 5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM