I want to calculate the factorial of 10 using a while loop, an if branch, and a break statement.
I manage to get the factorial of 10 with the while loop, but how can I get it using the if and break statement?
let x = 10;
var i = x-1;
while(i > 1){
x *= i;
i--;
}
console.log(x);
What you should do is invert the condition of the while and use it in an if at the end of the iteration, if it is fulfilled we execute the break, now for the while to be executed you must always put as a condition or evaluation something that is always "True", In this case, as long as x is not passed a negative value, it will work as true in a condition.
I must emphasize that this is a bad practice when programming.