So I´m trying to compare a character stored within an object with the four cardinal points but it ain´t working. In this case, this.position.orientation value is E, but it jumps directly to the default option. I think it must be something related to trying to compare an array that is part of an object bu I have no idea how to solve it. Any help would be highly appreciated.
NOTE: the this.position.orientation was filled using previously using array.split(' ');, and it always stores just one character
switch (this.position.orientation) {
case 'N':
this.position.orientation = 'E';
break;
case 'S':
this.position.orientation = 'W';
break;
case 'E':
this.position.orientation = 'S';
break;
case 'W':
this.position.orientation = 'N';
break;
default:
break;
}
This is not a question, basically, check what is this.position.orientation before entering the switch statement and you´ll realise what the problem is about.
My Suggestion is check the DataType of this.position.orientation to make sure that its an String.
I am not sure, but you are doing comparison with char. Try to use case "E" instead case 'E'.
Try use ToString() command in this.position.orientation before compare.
If it comes from an Array, make sure that it isnt an array with only one position doing this.position.orientation[0] in switch clause.
Related
In this action I am trying to say...
if VS_WRNG is Blank and if VS_WRNG is DSC or DSQ then set the two items
but when I click on VS_WRNG in my web app and select DSC it didn't autofill the BOB_TYPE answer with LIN.
Did I screw up the code or is it the ! not working like that and do I need to do a switch (true) & case (VS_WRNG != "")?
//Set BoB Type [TEMPORARY]
switch (!VS_WRNG) {
case "":
switch (VS_WRNG) {
case "DSC":
case "DSQ":
switch (VS_BTYP) {
case "":
setValue('BOB_TYPE',"LIN");
VS_BTYP = "LIN";
break;
}
break;
}
break;
}
Thanks
The primary issue you're having is that !VS_WRNG always results in a boolean. It can never result in an empty string "". So the case "": will never run. But even if it did, your code still won't work:
In this action I am trying to say... if VS_WRNG is Blank and if VS_WRNG is DSC or DSQ then set the two items
VS_WRNG cannot be simultaneously Blank and DSC or DSQ. You need to rethink your logic - the requirements are incompatible right now and quite illogical.
This is also quite an abuse of switch statements. If your switch statement has one case, it's an if statement. Just use an if statement like this:
if(VS_WRNG === "") { ... }
Your middle switch statement can be implemented also with an if statement, since you've written it to just check for two conditions:
if(VS_WRNG == "DSC" || VS_WRNG == "DSQ") { ... }
I have a script in my project that will generate notifications, though, these notifications thus far have the same title and description through their pre-defined variables:
notificationTitle: "Notification",
notificationDescription: "This is a notification.",
This is rather boring, really. And whilst I can go and set the amount of notifications* I want the script to generate, along with enough case statements to facilitate them, this is not reasonable. In the environment where we intend to have multiple different notifications for multiple different users, I don't want to have to write a case statement for each one, like so:
function setNotificationDescription(iteration) {
"use strict";
switch (iteration) {
case 1:
values.notificationDescription = "One";
break;
case 2:
values.notificationDescription = "Two";
break;
case 3:
values.notificationDescription = "Three";
break;
case 4:
values.notificationDescription = "Four";
break;
case 5:
values.notificationDescription = "Five";
break;
case 6:
values.notificationDescription = "Six";
break;
default:
values.notificationDescription = "Seven";
}
}
*This would be filled by a count of objects in a JSON file in the future, but for my example I set it myself.
Live Example: http://moonsquads.com/scriptbase/notification-generation/
Is there a way to generate these case statements automatically?
Instead of setting the values.notificationDescription inside the function, I have moved it outside for demonstration purposes (compare the "case-version" with the "array-version"):
https://jsfiddle.net/6j98sxue/1/
This avoids switch-cases by using an array since numeric index is concerned, and avoids a big change to your original code.
In addition to the above, I would suggest a Code Review post and people there would give detailed comments on refactoring the existing code. Modifying the value object and getting values from it will cause problem, e.g. it is hard to test the code as we cannot do dependency injection.
My question divide in three questions:
1.Is it even possible ?
2.If yes can we do it with the default value ?
3.Or could with do it outside the switch statement ?
Example for questions 2:
switch(stuff) {
case 'something':
some event;
break;
case 'the case that could be add by the default element':
some event that could happen only after the code was executed
default:
magic code that would add another case element
}
Example for question 3:
switch(stuff) {
case 'something':
some event;
break;
case 'the case that could be add by the magic code':
some event that could happen only after the code was executed
default:
some default event
}
magic code that would be executed after the switch and that would add a case
You can't really code JavaScript so that it will modify itself, but you can code a switch statement such that certain cases will be ignored initially and then "turned on" later:
var enableCase = false;
switch(true) {
case stuff === 'something':
// some code;
break;
case enableCase && stuff === 'the case not initially enabled':
// some code
break;
default:
// turn on previous case:
enableCase = true;
break;
}
Having said that, I don't really recommend doing it. There is almost certainly a more sensible way to implement this depending on the underlying problem you are trying to solve. Perhaps with an if/if else/else block that tests a flag set elsewhere.
Hello (yet again) a question about javascript, this one has to be an easy one but I just can't see what I did wrong, I think something really stupid:
I have a textbox and a button. When i click the button the value wil be passed to a variable called activiteitn. Code:
activiteitn = $("#m2activiteitn").val();
It gets the value, so when I filled in the number 1, activiteitn = 1.
After that I have a switch that looks like this:
switch(activiteitn) {
case 1: activiteitn = 1.2;
break;
case 2: activiteitn = 1.375;
break;
case 3: activiteitn = 1.55;
break;
case 4: activiteitn = 1.725;
break;
case 5: activiteitn = 1.9;
break;
default: alert("hoi");
break;
}
The problem is that even if I fill in the number 1, it jumps to the default case, causing it to alert. If I place a alert at case 1 that alert does not show up. The first thing that came in my mind was that the activiteitn was not 1, so right after the switch I placed another alert:
alert (activiteitn);
Now, first what happends is I get the alert "hoi", then I get the second alert of activiteitn that says: 1. So after that I thought, "Maybe its a string", so I changed the cases to strings like: case "1" & case ("1") but that did not work either. The problem, it always goes to the default case and I can not figure out why that is, I hope someone sees what I did wrong and can help me out.
.val() returns a string, not a number. So after the line:
activiteitn = $("#m2activiteitn").val();
the variable activiteitn actually equals "1", not 1. Since case...switch uses strict equality, your test will always fail.
To fix, simply change the above line to:
activiteitn = +$("#m2activiteitn").val(); // convert to a number
or
activiteitn = parseInt($("#m2activiteitn").val(),10); // convert to an integer
or
activiteitn = parseFloat($("#m2activiteitn").val()); // convert to a float
Use parseInt(activiteitn, 10); to turn your String into an Integer. Switch-case statements in Javascript don't play nicely with Strings half the time.
I'm trying to use the following switch statement in an ajax success callback:
success: function(datain) {
switch (datain)
{
case "ERROR. No ID. Try again":
$(".errors").append('There was an error.');
break;
case "ERROR. Wrong captcha. Try again":
$(".errors").append('There was an error.');
break;
}
}
datain is a string (typeof datain returns string) and it does indeed contain the same text, capitalization and punctuation as the case so why would it not match either of the cases?
console.log(datain) and console.log("ERROR. No ID. Try again") match exactly and both return a typeof of string so why does my case never get matched?
Solution and cause
The solution is offered by palswim below $.trim(datain). The cause was visible in Firebug and it was the fact that the string had a newline at the end while my switch case did not... so I was getting "foo\n" and trying to match "foo". Given that js uses === in the switch this, naturally (now that I see it), is why it failed.
Try trimming your strings.
In jQuery:
switch($.trim(datain))
{ //...
But trying to match strings that long is fraught with peril!
No clue as to why neither are called based on what you say. At the risk of suggesting the obvious though: Put in a "default" statement at the end of the switch to make sure you're function is being called, and in the default statement do some if comparisons: datain === "ERROR:...".
One other thing that might be easy to miss is if (possibly) there should be a "." at the end of "...Try again."
You can also do this:
success: function(datain) {
switch (datain)
{
case `"ERROR. No ID. Try again"`:
$(".errors").append('There was an error.');
break;
case `"ERROR. Wrong captcha. Try again"`:
$(".errors").append('There was an error.');
break;
}
}
A character before " is `. It is right before 1 on my keyboard.