How to write case in Switch else statement in javavscript? - javascript

//This is my Problem
Complete the getLetter(s) function in the editor. It has one parameter: a string, , consisting of lowercase English alphabetic letters (i.e., a through z). It must return A, B, C, or D depending on the following criteria:
If the first character in string is in the set {aeiou}, then return A.
If the first character in string is in the set {bcdfg}, then return B.
If the first character in string is in the set {hjklm},, then return C.
If the first character in string is in the set {npqrstvwxyz}, then return D.
I am trying to implement the above scenarion and to dthis I have written the following code.
//Here is my code
function getLetter(s) {
let letter;
// Write your code here
switch (s) {
case s.match(/[aeiou]/g).includes(s[0]):
letter = 'A'
break;
case s.match(/[bcdfg]/g).includes(s[0]):
letter = 'B'
break;
case s.match(/[hjklm]/g).includes(s[0]):
letter = 'C';
break;
case s.match(/[npqrstvwxyz]/g).includes(s[0]):
letter = 'D';
break;
default:
console.log("hello")
}
return letter
}
The code is showing error. Would any help me to figure it out? How can I implement the above task using switch statement.

I don't know that much switch case, but maybe you can use parenthesis for the switch statement and if "s.match(/[aeiou]/g).includes(s[0])" return true or false
And you forgot ";" after the two first assignment

the way you wrote the switch case may be partially correct in GoLang. but in Javascript, it is not possible to achieve this using Switch-Case statement.
Better try using if-else-if statement something like below
if(s.test(/^[aeiou]/)) {
return 'A';
} else if(s.test(/^[bcdfg]/)) {
return 'B'
}

Check out this: Switch statement for string matching in JavaScript. The issue is that match returns null, which breaks the switch statement.
Use
switch(s){
case str.match(/^xyz/)?.input:
//code
break;
This stops the match from returning null.

Based on how you're using this, you actually want to switch (true) and not switch (s)
i.e. when you have a case y in a switch (x), the test being done is x === y; so you want to do true === some.test()
const s = 'a';
switch (true) {
case /a/.test(s):
console.log('first match');
break;
case /b/.test(s):
console.log('second match');
break;
default:
console.log('no match');
break;
}
Will log first match because
/a/.test('a') is true
and true /* from switch */ === true /* from case test */
Note that in most cases switch can also be written as if..else if..else.
Base your decision on which to use by which is the most readable (err on the side of if patterns)
The above example written as an if might look like this
const s = 'a';
if (/a/.test(s)) {
console.log('first match');
} else if (/b/.test(s)) {
console.log('second match');
} else {
console.log('no match');
}

Related

DIfferent between break and return in swith loop JS ? And what using in my logic ( looking code ) [duplicate]

This question already has answers here:
Difference between Return and Break statements
(14 answers)
Closed 2 years ago.
And i don't know is this good solution to using return or to use break?
switch (arg.seriesName) {
case 'Test':
return 'Test'
case 'One':
return 'One'
case 'two':
return 'two'
default:
break;
}
break will allow you the function to continue processing, whereas return will end the function. Returning out of the switch is fine if that's all you want to do in the function.
E.g.
Return
function foo(x) {
switch(x) {
case 'y':
return;
}
// This will never run if x === 'y'
}
Break
function foo(x) {
switch(x) {
case 'y':
break;
}
// This will still run if x === 'y'
}
Just to clarify, that one is a switch statement, not a loop.
Anyway, break is used to stop fall-through: that is, when one of the cases match, it prevents the other from matching as well.
The difference between return and break depends on your context: if you're inside a function, return will stop the execution of the function altogether, whereas break will make the switch statement only match one case and then go on with the execution.
A case clause used to match against expression. If the expression matches the specified valueN, the statements inside the case clause are executed until either the end of the switch statement or a break.
The break stops to execution the other cases.
In your example you return inside the case so it will left the function and return the value so there is no need for the break;
Here with the breaks in each case prints only the case which is hitting
const expr = 'Papayas';
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log(`Sorry, we are out of ${expr}.`);
}
But if you forget the break it also prints out the cases after the hitting one
const expr = 'Papayas';
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
default:
console.log(`Sorry, we are out of ${expr}.`);
}

Switch Case statement for Regex matching in JavaScript

So I have a bunch of regexes and I try to see if they match with another string using this If statement:
if (samplestring.match(regex1)) {
console.log("regex1");
} else if (samplestring.match(regex2)) {
console.log("regex2");
} else if (samplestring.match(regex3)) {
console.log("regex3");
}
But as soon as I need to use more regexes this gets quite ugly so I want to use a switch case statement like this:
switch(samplestring) {
case samplestring.match(regex1): console.log("regex1");
case samplestring.match(regex2): console.log("regex2");
case samplestring.match(regex3): console.log("regex3");
}
The problem is it doesn't work like I did it in the example above.
Any Ideas on how it could work like that?
You need to use a different check, not with String#match, that returns an array or null which is not usable with strict comparison like in a switch statement.
You may use RegExp#test and check with true:
var regex1 = /a/,
regex2 = /b/,
regex3 = /c/,
samplestring = 'b';
switch (true) {
case regex1.test(samplestring):
console.log("regex1");
break;
case regex2.test(samplestring):
console.log("regex2");
break;
case regex3.test(samplestring):
console.log("regex3");
break;
}
You can use "not null":
switch(samplestring){
case !!samplestring.match(regex1): console.log("regex1");
case !!samplestring.match(regex2): console.log("regex2");
case !!samplestring.match(regex3): console.log("regex3");
}

javascript: check if variable is a specific letter

Very basic javascript, i'm a complete beginner trying to construct an if statement to check if a variable is a specific letter
more specifically, if a user input for a variable is "P", "M" or "G"
i'm using
if (variable = P) {
statement
}
but doesn't seem to be working
thanks in advance
In addition to forgetting the quotes around the letter "P", you've used the assignment operator =, instead of the comparator ==.
Check this out:
var name = "Carolyn";
console.log(name);
# "Carolyn"
var name_is_carolyn = (name == "Carolyn");
console.log(name_is_carolyn);
# true
Changing your code to this should help:
if (variable == "P") {
// statement
}
if(variable == 'P' || variable == 'G' || variable == 'M') {
// do something
}
take care, it's case sensitive without using toLowerCase or toUpperCase on the string.
anther way is:
switch(variable.toLowerCase()) {
case 'p':
// do something for p
break;
case 'm':
case 'g':
// do something for m and g (no break; in m case)
break;
default:
// no match, so do something else
}
Here's how you can do it in JS:
TextLetter=function(txt,letter){
try{
var TXT=txt.split(letter);
if(TXT.length>0)
return true;
else
return false;
}catch(err){
return false;
}
}
TextLetter(prompt("Type in text","TEXT"),prompt("Look for the character or phrase...","?"));
I didn't test this yet, but should be helpful

how to switch-case with inconsistent variable format

switch (req.path)
{
case "/api/posts":
console.log("posts");
break;
case "/api/posts/tags/*": // the part * is always changing depending on user input
console.log("tags");
break;
case "/api/best":
console.log("best");
break;
default:
console.log("default");
}
the req.path gives my path
for example
/api/post/tags/asd,dsfd
/api/post/tags/1
/api/post/tags/12,123
how do you manage with this as efficiently as possible?
some frameworks provide path parsers that looks like
/*
this and any input after * is ignored and treated as the same. I am curious of its inner mechanism.
What about something like this (see the jsfiddle):
function route(req) {
if(req.path === '/api/posts') {
console.log('posts');
}
if(req.path.indexOf('/api/posts/tags') > -1) {
console.log('tags');
}
// etc.
}
route({path: '/api/posts'}); // => posts
route({path: '/api/posts/tags/hi'}); // => tags
route({path: '/api/posts/tags/cool'}); // => tags
You could also use a regular expression. Keep in mind that if some input conditions might match each other, you'll want to return at the end of each if statement, or simply use else if. Totally depends on the routes your looking for.
For the default case, you can string together everything with if... else if, and in the final else put console.log('default'). I've left my above solution very minimal so you can extend it as you see fit.
Output
You can just toss a regex into your switch statement which will handle all cases branching off of that route.
switch (req.path)
{
case "/api/posts":
console.log("posts");
break;
case req.path.match(/(\/api\/posts\/tags\/)/)[1]:
console.log("tags");
break;
case "/api/best":
console.log("best");
break;
default:
console.log("default");
}

Why is this switch statement not behaving as predicted?

I have the following switch block:
var str = 'matches[pw1]';
switch (str)
{
case (str.indexOf('matches') > -1) :
console.log('yes');
break;
default:
console.log(str.indexOf('matches') ) ;
console.log('no');
break;
}
What I want is, that if str contains the word 'matches', then it should run the first case block, otherwise the default block.
However when I run this, the output I get is '0', and then 'no', meaning the default block is running despite the conditions for the first case being met.
Any ideas what's wrong?
Your case is likely testing whether (str.indexOf('matches') > -1) == str.
EDIT:
It might be valuable to understand exactly what switch and case mean. One of Javascript's ancestors, C, commonly used switch to replace blocks of code where a primitive was being compared against a list of values (often from an enumeration, or a series of literals). So instead of:
if (type == ENABLE_FRAMISTAN)
{
enable_framistan();
}
else if (type == ENABLE_FROBSOSTICATOR)
{
enable_frobnosticator();
}
else if (type == DISABLE_BAZTICULATOR)
{
disable_bazticulator();
}
else
{
assert(false);
}
you could instead write:
switch (type)
{
case ENABLE_FRAMISTAN: enable_framistan(); break;
case ENABLE_FROBNOSTICATOR: enable_frobnosticator(); break;
case DISABLE_BAZTICULATOR: disable_bazticulator(); break;
default: assert(false); break;
}
...which might make it easier to digest (and or spot errors in) a large block of code which effectively mapped type values to functions being called (or some such). Your designated usage, checking to see whether a string matches any of a number of potential (exclusive with one another?) patterns, does not map as well to switch. If it were merely equality being tested, it would work well, but your condition is more sophisticated than switch was designed to express. Any way that you manage to preserve switch with your feature set will likely require less-than-obvious code.
Why not use an if statment? try something like this:
var str = 'matches[pw1]';
if(str.indexOf('matches') > -1) {
console.log('yes');
break;
}else{
console.log(str.indexOf('matches') ) ;
console.log('no');
break;
}
It should work since you don't have alot of cases anyways. I don't think you can do a comparing in cases.
What I want is, that if str contains the word 'matches', then it should run the first case block, otherwise the default block.
You cannot do that with a switch statement. A switch statement compares the result of evaluating the switch expression (in this case str) with the values of the case labels. The case labels can be expressions (as in your example), but if they are the expressions are evaluated and then compared against the value above using ===. (That's what the ECMAScript 5.1 spec says ...)
So what your code is actually doing for that case is (roughly speaking):
evaluate (str.indexOf('matches') > -1) which gives you true or false
compare true or false with the value of str ... which fails and the case body isn't executed.
Now I think you could make your approach work as follows:
case (str.indexOf('matches') > -1 ? str : '') :
console.log('yes');
break;
but that stinks from a code readability perspective (IMO).
You could do this:
console.log( str.indexOf( 'matches' ) > -1 ? 'yes' : 'no' );
You should know the basic usage of switch. I think you mistakenly use Switch. Try to use it as following:
var str = 'matches[pw1]';
str = str.indexOf('matches');
switch (str)
{
case -1 :
console.log('yes');
break;
default:
console.log(str.indexOf('matches') ) ;
console.log('no');
break;
}
Please look at the Following URL http://www.w3schools.com/js/js_switch.asp
If the above is not suitable for your logic, use if.. else if ...
http://www.w3schools.com/js/js_if_else.asp

Categories