PDA

View Full Version : function not executing properly



synonymous
Oct 17th, 2017, 12:09 PM
Hello World. I am trying to practice my newly learnt skills by trying to make a simple program where the browser prints a times table of any number the user chooses till any limit. I tried to
make it without the use of functions and it worked perfectly. But now I tried to program with functions but the code is not executing properly.

Below is the program:



let number = prompt("Choose a number");
let loop = prompt("How many times do you want to multiply your number?");

const limitCheck = () =>{
if(loop >=10000){
alert("Please choose a number under 10000. Refresh to proceed.");
};
};

const mainTable = (multiple,limit) => {
for(var i = 1; i<=limit ; i++){
return `${multiple} multplied by ${i} is equal to ${i*multiple}`;
};
};

limitCheck();
document.write(mainTable(number,loop));


Now the problem I encounter is that the function mainTable is supposed to loop continuously until the parameter limit is reached(which is determined by the variable loop). However, the loop only works once even if i set the limit above 100. I tried to debug but I could not understand the flaw. I will be grateful if someone helps.

sunfighter
Oct 17th, 2017, 08:24 PM
A fast explanation for you. When the loop executes the return statement executes and terminates the loop, your doc write executes. So everything happens on the first iteration.

sunfighter
Oct 18th, 2017, 12:31 AM
If, like you imply, in the final read out that you just want to multiply those two numbers together all you need to do in the last function is change it to this:


const mainTable = (multiple, limit) => {
return `${multiple} multplied by ${limit} is equal to ${limit*multiple}`;
};


But, if you want to multiply the first number by it's self a certain number of times and want a loop to to do it, then the function is this:


const mainTable = (multiple, limit) => {
var base = multiple;
for(var i = 1; i<=limit-1 ; i++){alert(i+' and '+base);
base = base * multiple;
}
return `${multiple} raised to the power of ${limit} is equal to ${base}`;
};


But JS has a pow() function that does that far more easily and the results would be to change the last function to:


const mainTable = (multiple, limit) => {
return `${multiple} raised to the power of ${limit} is equal to ${Math.pow(multiple, limit)}`;
};


FYI, I know of no programmer that writes functions like that.
They would write them like this:


function mainTable(multiple, limit){
return `${multiple} raised to the power of ${limit} is equal to ${Math.pow(multiple, limit)}`;
}


and you would place the functions after all the other code:


<script>
let number = prompt("Choose a number");
let loop = prompt("How many times do you want to multiply your number?");
limitCheck();
document.write(mainTable(number, loop));

function limitCheck(){
if(loop >=10000){
alert("Please choose a number under 10000. Refresh to proceed.");
}
}
function mainTable(multiple, limit){
return multiple +'raised to the power of '+ limit +' is equal to '+Math.pow(multiple, limit);
}
</script>


and they would not use ${ variable here }

Philip M
Oct 18th, 2017, 08:27 AM
and they would not use ${ variable here }

Nor would they use document.write().

synonymous
Oct 18th, 2017, 05:47 PM
OK so that means that return ends the loop immediately?


and they would not use ${ variable here }
Well, evidently, I am new to programming but the new version of Javascript demands you use this kind of interpolation. Or am I wrong? Please tell.


Nor would they use document.write().
I was checking out javascript on html but if you have a better idea i would love to hear it.

Philip M
Oct 18th, 2017, 06:58 PM
I was checking out javascript on html but if you have a better idea i would love to hear it.

document.write() has been obsolete since Netscape 3 passed away 12+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

You should use DOM methods to obtain input data from and give messages to the user.

synonymous
Oct 19th, 2017, 01:15 PM
Appreciate the help.
Other than that, i was also confused about the types of functions one can use as I was using a different kind of function than user sunfighter. Can you please tell which one is better?

sunfighter
Oct 19th, 2017, 05:12 PM
Appreciate the help.
Other than that, i was also confused about the types of functions one can use as I was using a different kind of function than user sunfighter. Can you please tell which one is better? Your function type const mainTable = (multiple, limit) => { works, but I have never seen it before. Had to look it up. Don't think I would use it.

Mine function mainTable(multiple, limit){ has been around for as long as JS exists and uses less characters and is easier to write. So, which one is better?


OK so that means that return ends the loop immediately?Yes.


.....but the new version of Javascript demands you use this kind of interpolation. This ${ variable here } looks more like a the framework Angular and not raw JS.

deathshadow
Oct 19th, 2017, 08:52 PM
My advice would be to STOP trying to make immutable constants, and ACTUALLY make functions. But then, trust this new fangled "let" and "const" crap in JavaScript about as far as I could throw the USS Iowa!

"LET" -- what is this, 4k TRS-80 Level 1 ROM BASIC?!? 1978 called, it wants its language syntax back.

sunfighter
Oct 19th, 2017, 10:59 PM
Holy crap DS, TRS-80 Level 1 ROM BASIC first really good language I learned. Better then apple or commodore basics. Was that really ROM? Don't remember.