Switch statement not returning values

Pbculley

My first time using switch and I'm having some trouble returning anything. As a test I'm taking a string and, based on the character that is tested, console logging some string output.

function pairElement(str) {
switch (str) {
    case "A":
    console.log("some things")
    break;
    case "G":
    console.log("some more things")
    break;
}
}
pairElement("ACG"); 

The cases are of the same value type so I'm not sure what I'm doing wrong here. Any help would be much appreciated.

Akrion

Your test is not valid based on the values you are handling in your switch statement. You only handle cases for A and G but you passed ACG. Switch had no way to go if any of the cases specified do not match since you also are missing the default case. Your test would be valid if:

function pairElement(str) {
switch (str) {
    case "A":
    console.log("some things")
    break;
    case "G":
    console.log("some more things")
    break;
}
}
pairElement("A"); // some things - valid
pairElement("G"); // some more things - valid
pairElement("ACG"); // switch case and no default - NOT valid

Adding a default would give you:

function pairElement(str) {
switch (str) {
    case "A":
    console.log("some things")
    break;
    case "G":
    console.log("some more things")
    break;
    default:
    console.log("something ELSE")
}
}
pairElement("A"); // some things  - valid
pairElement("G"); // some more things - valid
pairElement("ACG"); // something ELSE - valid

Now there is also the question what exactly did you expect when you tested for a multi character string vs single one. Handling the single chars in your switch kind of eludes to you expecting the string passed to your function to be tested char by char and if so you need to state that since that changes the question/requirement.

Updating for the scenario where you want char by char:

function pairElement(str) {
	str.split('').forEach(function(v) {
		switch (v) {
			case "A":		
			console.log("some things")
			break;
			case "G":
			console.log("some more things")
			break;
			default:
			console.log("something ELSE")
		}
	}
)
}
pairElement("ACG"); 
// some things
// something ELSE
// some more things

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python switch case statement returning 2 values per case but unsorted

Returning values from a function using a switch statement to call the fnctions

Switch statement is returning no value

Switch statement returning undefined

Switch statement returning wrong result

My switch statement is not returning anything?

Switch statement returning incorrect value

Returning a value from switch statement

String statement returning null from switch statement

NOT IN Select statement returning 0 values

SQL statement not returning all values

If Statement Returning Multiple True Values

SQL statement not returning any values

Why is Javascript returning "undefined" in Switch statement?

switch statement within function returning undefined

Are multiple values possible in a switch statement?

Groovy Switch statement with list of values

Java switch case statement is returning different type of values(different data-type) and is assigned to same variable of type var

Golang switch for a types returning boolean values

Returning wrong values with if in python in DEF() statement

Using two values for one switch case statement

In C++ can there be a range of values in a switch statement?

Javascript Switch Statement multiple cases return the values

Change variable values with one switch statement

Javascript Switch Statement Sharing Array Values

Switch statement jQuery not working for radio button values

java use switch statement with values retrieved by locale

Throwing exception vs returning null value with switch statement

How to handle returning two objects within switch case statement