The thing is i want is the user to enter the limit of the numbers he or she wants to divide and then i can divide them . Like the user says limit is 4 and then enters numbers like 60/2/3/5 to give 2.
case 'division':
function division(){
let num = 0;
let division = 1;
let div=division
let i = 0;
window.ask=prompt('Enter the limit of the numbers to be multiplied');
while(i<window.ask){
let num=parseInt(prompt('Enter the numbers one by one '));
division= num/division;
if(division=num){
div=div/num;
}
i++;
}
alert("The result is "+div);
}
division();
break;
I tried this expecting that after entering the first number it would divide by 1 and if the division was equal to the number stored in the variable 'num' then div would divide the division result with the value stored in 'num' that is entered in the next round by the user but i got stack as the result of 60/2/3/5 was 0.0005 and in addition i want to be using while loop to be able to calculate any limit the user can enter not just like 3 0r 4 numbers.
So there are couple of mistakes in your code. Let's look at them one by one.
division
for the first time.Below is the code that will address the above issue to give you the expected output.
<!DOCTYPE html>
<html>
<body>
<script>
switch('division'){
case 'division':
function division(){
let division = 1;
let i = 0;
window.ask = prompt('Enter the limit of the numbers to be divided');
while(i < window.ask){
let num = parseInt(prompt('Enter the numbers one by one '));
if(i === 0){ // <-------- Note this condition. You can use ternary as well. Just for clarity I used if
division = num;
}else{
division = division / num;
}
i++;
}
alert("The result is "+ division);
}
division();
break;
}
</script>
</body>
</html>
EDIT:
Well, as per the code that you have shared, initially
division
is 1. Now, for the first number that will be input by the user, there is no divisor since we have only one number, sodivision
will be equal to that input number. Second time onwards we have the dividend(previous value of division i.e.60
as per your example) and divisor(the newly provided input i.e.2
as per your example), so from next time the division will take place as per the expectation.