JavaScript comma separation not working with switch case - javascript

In case of comma separation not working with swtich case. is there any way to do this?
switch(test)
{
case 0,1://not working it take last one (1)
"test"
break;
case 2,3://not working it take last one (3)
"test2"
break;
case 4,5://not working it take last one (5)
"test3"
break;
}
How can i add further number with each case?

switch(test)
{
case 0: case 1:
"test"
break;
case 2: case 3:
"test2"
break;
case 4: case 5:
"test3"
break;
}

You should fall through (which is considered a bad practice)
switch(test)
{
case 0:
case 1://not working it take last one (1)
"test"
break;
case 2:
case 3://not working it take last one (3)
"test2"
break;
}

Related

Javascript switch statement check object keys [duplicate]

These two switch statements are the same, other than the use of console.log.
When outputting the result to the chrome console I get two different results.
The first one outputs:
this is the one
while the second outputs:
1
this is the one
Why is that?
const q = 1;
switch (q) {
case '1':
answer = "one";
case 1:
answer = 1;
case 2:
answer = "this is the one";
break;
default:
answer = "not working";
}
console.log(answer);
const q1 = 1;
switch (q1) {
case '1':
console.log("one");
case 1:
console.log(1);
case 2:
console.log("this is the one");
break;
default:
console.log ("not working");
}
I put a star(*) on the lines that executed.
The switches need breaks, or else one hit can execute multiple sections.
*const q = 1;
*switch (q) {
case '1':
answer = "one";
case 1:
* answer = 1;
case 2:
* answer = "this is the one"; //changed the value of answer
* break;
default:
answer = "not working";
}
*console.log(answer); //the first line of output.
*const q1 = 1;
*switch (q1) {
case '1':
console.log("one");
case 1: //where the hit occurred.
* console.log(1); // the second line of output.
case 2:
* console.log("this is the one"); //the third line of output.
* break;
default:
console.log ("not working");
}
This is because you are not using break; at the end of each case block. Use something like this:
const q = 1;
switch (q) {
case '1':
answer = "one";
break;
case 1:
answer = 1;
break;
case 2:
answer = "this is the one";
break;
default:
answer = "not working";
}
console.log(answer);
const q1 = 1;
switch (q1) {
case '1':
console.log("one");
break;
case 1:
console.log(1);
break;
case 2:
console.log("this is the one");
break;
default:
console.log ("not working");
}
break; just means that stop the execution here and don't move to the block below, just like in your case
A switch is not like a if/else if/ else. In an if structure it only runs the code in the block that follows it.
In a switch a case statement falls through to the next. If you do not want it to fall through you add a break.
function countDown(start) {
console.log('called with: ', start);
switch (start) {
case 3:
case 'three':
console.log(3);
case 2:
case 'two':
console.log(2);
case 1:
case 'one':
console.log(1);
}
}
countDown(3); // 3 2 1
countDown('two'); // 2 1
Now when you add a break it will not fall through
function countDown(start) {
console.log('called with: ', start);
switch (start) {
case 3:
case 'three':
console.log(3);
break;
case 2:
case 'two':
console.log(2);
break;
case 1:
case 'one':
console.log(1);
break;
}
}
countDown(3); // 3
countDown('two'); // 2
If we execute each line of code or verify each line code, you can see, in first switch case is executed case 1 and thencase 2, which makes value = this is the one now the break is executed and we are out of switch case 1.
Now,console.log(answer); hits and prints this is the one
Next, q1 =1
case 1:
console.log(1);
case 2:
console.log("this is the one");
Both of the above statement executes since, case 1 is correct match for switch case number 2, but there is no break so case 2 is also executed.
Hence, you see this output.
You can also refer these links also: Why is Break needed when using Switch?
https://javascript.info/switch
You need to add break to skip the next switch case.
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
conosle.log('case is 1 to 12');
break;
case 13:
console.log('case 13');
break;
const q = 1;
switch (q) {
case '1':
answer = "one";
case 1:
answer = 1;
case 2:
answer = "this is the one";
break;
default:
answer = "not working";
}
console.log(answer);
switch case default behaviour is that when a case will get true value then the following case statements will run without checking the condition . in this case case 1 is implemented as true so the next cases will not evaluate .
const q1 = 1;
switch (q1) {
case '1':
console.log("one");
case 1:
console.log(1);
case 2:
console.log("this is the one");
break;
default:
console.log ("not working");
}
in your second code , case 1 will evaluated as true so the compiler will not evaluate your case 2 . it will directly go to the case 2 block and console that until compiler find a break s

JS - Evaluation with function within switch-case

I have objects with ids like alternative1, alternative2 etc. I want to style all of these the same way so I'm trying:
switch (feature.id) {
case 'marker':
...
break;
case feature.id.includes('alternative'):
case 'connectionMarker':
... //add styling
break;
This does not seem to work. How could I do this?
You can use a simple trick:
switch (feature.id) {
case 'marker':
...
break;
case (feature.id.includes('alternative') ? feature.id : !feature.id):
// your code here
break;
case 'connectionMarker':
...
break;
}
Since feature.id is always a string value, !feature.id will always return false. Hence, if feature.id includes "alternative" this line:
case (feature.id.includes('alternative') ? feature.id : !feature.id):
becomes:
case feature.id:
which will always be the case (as feature.id === feature.id). Otherwise, the line evaluates to:
case 0:
which will be skipped by the interpreter.

Switch multiple conditions

I hava a switch statement like this:
switch(intCount){
case 0:
run();
break;
case 1:
run();
break;
case 2:
stop();
break;
case 3:
stop();
break;
}
All I want to do is abbreviate to this:
switch(intCount){
case 0 || 1:
run();
break;
case 2 || 3:
stop();
break;
}
Not sure how to do it though. Thought || would do "or", but it's not. Also I don't want to do case < 1: As I want to be able to group random numbers.
Do this:
switch(intCount){
case 0:
case 1:
run();
break;
case 2:
case 3:
stop();
break;
}

Is it possible to do something on all switch cases by default without repeating?

Is there a way to repeat a sentence inside of a switch for every case but for default? Without repeating the action inside every case, of course.
I have this:
this.set_estate = function(state){
switch(state){
case 'LOADING':
current= est[state];
break;
case 'WRITING':
current= est[state];
//statements_2
break;
case 'WAITING':
current= est[state];
//statements_3
break;
default:
console.log('tried to set state: '+state);
break;
}
}
And I'd like to know if it could be shifted into something similar to:
this.set_estate = function(state){
switch(state){
case 'LOADING':
case 'WRITING':
case 'WAITING':
current= est[state];
continue;
case 'LOADING':
//statements_1
break;
case 'WRITING':
//statements_2
break;
case 'ESPERANDO':
//statements_3
break;
default:
console.log('tried to set state: '+state);
break;
}
}
Write another switch statement before this to do just the common lines. This would prevent you from writing the code multiple times, if that is your concern.

Switch with random not working

This is my current jScript code:
(modified STILL doesn't work)
function changeBG(){
document.getElementById('imgbox').src = switch(eval(rand())){
case 1: "img/img3.jpg";
break;
case 2: "img/img2.jpg";
break;
case 3: "img/img3.jpg";
break;
case 4: "img/img4.jpg";
break;
case 5: "img/img5.jpg";
break;
default: "img/background.jpg";
break;
}
}
function rand(){
return (Math.ceil((Math.random()*10)/2));
}
my HTML:
<input id="BGchange" type="button" onclick="changeBG()" value=">"/>
If I do something like this:
document.getElementById('imgbox').src = "img/img1.jpg";
It just works: I click on my button, the image changes, no problem. Fact is I want the image to change to a random one between 5 each time the button is clicked; to do this I wanted to use a switch that works with a random choice inside it.
What is wrong with my coding?! :( (I cant use jQuery or anything, just pure javascript, html and css, thats what the professor said at least)
Thank you <3
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
The switch-statement "executes" your Strings ("img/imgX.jpg") instead of returning them, so you may want put the switch in a function and then do
case 1: return "img/img3.jpg";
...
and keep in mind that each case of a switch statement must be terminated with break, otherwise the following case-Blocks will be executed as well!
Your function return an integer, while your switch case is looking for a string.
Remove quotes from the switch cases and everything should work.
function changeBG(){
document.getElementById('imgbox').src = switch(rand()){
case 1: "img/img3.jpg";
break;
case 2: "img/img2.jpg";
break;
case 3: "img/img3.jpg";
break;
case 4: "img/img4.jpg";
break;
case 5: "img/img5.jpg";
break;
default: "img/background.jpg";
}
}

Categories