Reputation: 79
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.
Upvotes: 4
Views: 2026
Reputation: 18525
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
Upvotes: 6
Reputation: 45830
Switch uses strict comparison (===, in other words it has to match exactly) so in this case "ACG" does not exactly match "A" or "G" and since nothing exactly matches the switch does nothing.
Add a case for "ACG" to get the switch to execute something when "ACG" is passed. You can also add a default case and the switch will execute that case if nothing else matches:
function pairElement(str) {
switch (str) {
case "A":
console.log("some things")
break;
case "G":
console.log("some more things")
break;
case "ACG":
console.log("ACG exactly matches this case")
break;
default:
console.log("nothing matched so default executed");
break;
}
}
pairElement("A"); // some things
pairElement("G"); // some more things
pairElement("ACG"); // ACG exactly matches this case
pairElement("this won't match anything"); // nothing matched so default executed
Upvotes: 1
Reputation: 111
You are passing in a string of length three, there is the first thing to modify. The second is just to must put a default evaluation to in case of any of your cases matches your desired criteria which is the case right now.
function pairElement(str) {
switch (str) {
case "A":
console.log("some things");
break;
case "G":
console.log("some more things");
break;
default:
console.log("default");
break;
}
}
Check MDN Switch information
Upvotes: 2
Reputation: 222682
You should add a default
case since you are passing "ACG" which is not of any case. Switches in any programming language needs a matching case.
DEMO
function pairElement(str) {
switch (str) {
case "A":
console.log("some things")
break;
case "G":
console.log("some more things")
break;
default:
console.log("some things + some more things")
}
}
pairElement("ACG");
Upvotes: 4