4
Came to me a question related to the use of a switch within a for
. I saw that it works for my purpose but this is correct to do in the world of programming?
There is a better way to get the result without using a switch
within the for
, from the code below?
for (var index = 1; index <= 10; index++) {
switch (index) {
case 1:
console.log(`Repeating because one is less than ten !`)
break;
case 2:
console.log(`Repeating because two is less than ten !`)
break;
case 3:
console.log(`Repeating because three is less than ten !`)
break;
default:
console.log(`Repeating because ${index} is less than ten !`)
break;
}
}
Two negatives why??? The code refactored of the AP is exactly this!!!
– Augusto Vasques
I know who, I don’t need to say that they weren’t due to error or something like that. Each had a reason.
– Maniero
In this case, when it would be advisable to use a Switch instead of an IF, ELSE and ELSE, for example ?
– Gato de Schrödinger
@Gatodeschrödinger this has already been answered in https://answall.com/q/58192/101 and supplements for specific questions and applies only to some languages: https://answall.com/q/190736/101 and https://answall.com/q/82667/101 and https://en.stackoverflow.com/q/346695/101 and https://answall.com/q/258024/101 and https://answall.com/q/139313/101 and https://answall.com/q/139313/101 and https://pt.stackoverflowcom/q/211841/101 and http://answall.com/q/176675/101 and https://en.stackoverflow.com/q/240410/101.
– Maniero