admin管理员组

文章数量:1327997

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.

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.

Share Improve this question asked Jul 28, 2018 at 20:45 PbculleyPbculley 799 bronze badges 3
  • 3 It isn't returning because ACG does not equal A or G. switches need to use exact matches. – Get Off My Lawn Commented Jul 28, 2018 at 20:46
  • Switch matches each case via === (strict parison). – Andrew Li Commented Jul 28, 2018 at 20:47
  • What did you expect to get? – Jonas Wilms Commented Jul 28, 2018 at 20:51
Add a ment  | 

4 Answers 4

Reset to default 6

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

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"); 

 

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

Switch uses strict parison (===, 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

本文标签: javascriptSwitch statement not returning valuesStack Overflow