All Questions
Tagged with switch-statement javascript
1,496
questions
0
votes
2
answers
40
views
Utilizing array items as an entire case in a switch statement
I would like to generate cases in a switch statement from an array. Here is how the array is generated:
pointObjects = [];
for (var i = 0; i < 2; i++) {
// Generate cases.
var url;
var ...
-1
votes
2
answers
75
views
How to execute multiple other cases inside of a case
So it is possible to fall-through the case to another case:
switch(variable) {
case 1:
// do something 1
case 2:
// do something 2
break;
case 3:
// do something 3
break;
}
...
0
votes
1
answer
209
views
React.js switch case always returns default case
I am trying to use <select> tags to update information between components with the help of useContext(). The problem that I am facing is that the default case is always returned despite the ...
1
vote
6
answers
73
views
Javascript Switch case statement : fall-through all cases when expression is 'all'
How can I implement this behavior in JavaScript Switch Statements
expression = 'all' or '1' or '2'
switch (expression)
{
case "all" : print case 1 and case 2
case "1&...
1
vote
1
answer
55
views
Is using an object with identical keys & values in a switch statement bad/pointless code?
So I've come across some of my old code, which is something like :
const valStore = {
val1: "val1"
val2: "val2"
val3: "val3"
}
And it then is called like so:
...
0
votes
2
answers
56
views
Use nested switch commands
Trying to use nested switch commands to generate correct outputs on specific fields. In this specific example, the heading value of basement, main floor or 2nd floor of a house depends on the number ...
-1
votes
1
answer
90
views
How does switching on computed values change runtime complexity?
I have a switch statement which runs logic depending on the current url path. It has a default case which considers computed values (such as regex matches) if none of the other cases are matches:
...
0
votes
1
answer
55
views
unable to print result from switch case in java script
I took one input om(obtained marks) and another input mm(total marks), I am trying to calculate the percentage and grade. I got the output for percentage marks but couldn't get the output for grade ...
1
vote
0
answers
32
views
Add a switch statement in JS based on a HTML <select> element [duplicate]
I am learning how to integrate JavaScript into HTML documents and want to build a simple calculator app.
The idea is that there will be two elements from which the app takes the arguments and a ...
0
votes
1
answer
91
views
How to set a default parameter value if no argument is passed through using the switch statement condition?
I'm doing a lab exercise on VScode for an assignment and I'm unable to pass one of the Mocha test requirements. This test requires "takes in two arguments, a name and a language, and language ...
0
votes
0
answers
61
views
Not displaying correct string/ value from switch statement after update and after build
Problem - Nextjs app not displaying correct string/ value from switch statement after build
Not sure if this helps but this switch statement worked before I updated from Nextjs 12 to 14.
I have a ...
0
votes
1
answer
102
views
Typescript nested switch alternatives
I have various outputs for a variety of combinations of 3 different variables. The best solution I can come up with is nested switch statements. I was simply wondering is there a more efficient/...
0
votes
1
answer
52
views
Why my Switch Statement is returning Undefined . As it should be behaving like this? Weird JS [duplicate]
function reccursion(){
let random=Math.floor(Math.random()*9);
if(squares[random]==null){
// Here I'm getting the random between 0-8 range in console but in switch statement sometimes ...
0
votes
1
answer
41
views
How to use use destructuring in the same variable multiple times
I have an old code with several if statements, and on each one of them i need to get the HTTP status code. Something like this:
if (x == 1) {
const { statusCode } = await request(httpUrl...)
...
}
...
0
votes
1
answer
35
views
logical expression not working when used as a case value in a switch statement
I'm new to JavaScript and trying to build a simple box movements controlling project.
I'm trying to use a switch statement as an alternative to lines of if statements.
In my code, I added the keydown ...