Here's the code: this Display the table of any number given by user.
function table(num){
var num = prompt("please enter any number");
var x = num;
if (num <= 0){
alert("invalid number or Zero") ;
} else {
for(var i=1; i <= 10; i++){
var y = x * i;
document.write(x + ' X ' + i +' = ' + y + '<br/>') ;
}
}
}
table();
now if -1 entered it display alert("invalid number or Zero"); and code breaks nothing is displayed. What I am looking for is how will it go again to start of function and prompt again for the number.
Here's the code: this Display the table of any number given by user.
function table(num){
var num = prompt("please enter any number");
var x = num;
if (num <= 0){
alert("invalid number or Zero") ;
} else {
for(var i=1; i <= 10; i++){
var y = x * i;
document.write(x + ' X ' + i +' = ' + y + '<br/>') ;
}
}
}
table();
now if -1 entered it display alert("invalid number or Zero"); and code breaks nothing is displayed. What I am looking for is how will it go again to start of function and prompt again for the number.
Share Improve this question asked Jun 6, 2015 at 9:02 SamSam 5013 gold badges9 silver badges23 bronze badges 3- 1 call table(0) again after the alert() ..see recursion – Amitd Commented Jun 6, 2015 at 9:04
- so i should call the method table(); inside the if statement again?? after alert. – Sam Commented Jun 6, 2015 at 9:07
-
1
what to you need the function parameter
num
for? Why do you copy it tox
– PhilLab Commented Jun 6, 2015 at 9:07
4 Answers
Reset to default 4Recursion is your friend: call the method again
function table(num){
var num = prompt("please enter any number");
var x = num;
if (num <= 0){
alert("invalid number or Zero") ;
table(); // <----------------
} else {
for(var i=1; i <= 10; i++){
var y = x * i;
document.write(x + ' X ' + i +' = ' + y + '<br/>') ;
}
}
}
Apart from that, you might want to consider a slight refactoring, since there are some useless variables/parameters
As has been mentioned before, you'll want to call the table
function again (recursion), after the alert()
.
You can also clean up the function a bit, since there's no need to copy num
to x
, and a num
parameter in the function isn't necessary, because you're initializing num
in the function itself, and you have no need to pass it as a parameter:
function table(){
var num = prompt("please enter any number");
if (num <= 0){
alert("invalid number or Zero") ;
table();
} else {
for(var i = 1; i <= 10; i++){
var y = num * i;
document.write(num + ' X ' + i +' = ' + y + '<br/>');
}
}
}
table();
You could use a while loop as well.
Have a fiddle: https://jsfiddle/hmea6rLx/
JS code:
function table(num){
var num=0;
while (num <= 0){
num = prompt("please enter any number");
var x = num;
$('#error').html("invalid number or Zero") ;
}
for(var i=1; i <= 10; i++){
var y = x * i;
$('#error').html(x + ' X ' + i +' = ' + y + '<br/>') ;
}
}
table();
Why the parameter (num
)?
Your function rewritten in two ways, reusing itself:
document.querySelector('#table').addEventListener('click', start);
document.querySelector('#table2').addEventListener('click', start);
function start() {
return window[this.id]();
}
function table(){
var num = prompt("please enter any number");
if (num <= 0){
log(num + ": invalid number or zero");
return table();
}
for(var i = 1; i <= 10; i += 1){
log(i + ' * ' + num + ' = ' + (i * num));
}
}
// just for fun: more functional
function table2(){
var num = prompt("please enter any number");
var reslt = num <= 0 ? [num + ": invalid number or zero\n"] :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(
function(v, i) {
return v + ' * ' + num + ' = ' + (v * num);
}
);
log(reslt.join('\n'));
return reslt.length < 10 ? table2() : true;
}
function log(str) {
log.el = log.el || document.querySelector("#result");
log.el.textContent += str+'\n';
}
<button id="table">exec table</button>
<button id="table2">exec table2</button>
<pre id="result"></pre>