Validation with If-Else statement [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to put add a validation condition to a combobox. I have been able to get it to work on other combo boxes, but here I am trying to essentially add 2 validaitons on one combobox. I am not familiar with how the whole validation process works and the order of operation. My code has become convoluted and need help sorting it out.
This is the code on the validation that I am working with:
functionvalidateSLBox(v){
if(storeSpringLync.findExact('disp',
v)>-1)returntrue;elsereturn'Notvalid';else{
if(v=='DC'){
cbSLBox.enable();
}else{
cbSLBox.disable();
}
}
}

When you return from a function, you exit at that point. Nothing else after it in the function executes, so you will never reach the second half of your function.
Also, one else matches to just one if. You have two elses here for one if.
You probably want something like this:
functionvalidateSLBox(v){
if(v=='DC'){
cbSLBox.enable();
}else{
cbSLBox.disable();
}
if(storeSpringLync.findExact('disp',v) > -1){
return true;
}else{
return 'Not valid';
}
}
This will allow you to both enable cbSLBox (whatever that is) while also returning true or Not valid... If this isn't what you want, you could use a switch statement or just nest the if statement. It really depends on what you want to do precisely, which is hard to tell from your code sample and description.

Related

JS concatenate numbers with operator sign [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])

Why is 'Bring an umbrella' the correct answer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using the Grasshopper app on my phone and I do not understand an example they gave me for if then statements.
They give you the solution because I answered incorrectly, but I do not understand why the solution given is correct.
var todayWeather = 'rainy';
var tommorrowWeather = 'cloudy';
if (todayWeather === 'rainy') {
print('Bring an umbrella');
}
if (todayWeather !== 'rainy') {
print('Maybe the sun will come out');
}
They say the correct answer is 'Bring an umbrella'. But why is this what this code will produce if it is run?
translating the code to english:
create a variable named todayWeather and set it to rainy
create a variable named tommorowWeather and set it to cloudy
if the variable todayWeather is rainy (true) then
print to the screen Bring an umbrella
(close if conditional)
if the variable todayWeather is not rainy (false, because it is set to rainy) then
print to the screen Maybe the sun will come out
(close code conditional)
Note the print statements only execute if the condition is met.
Also the variable values are never changed by this code after they are set.

For loop skipping index in JavaScript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I wrote a for loop in JavaScript which is simple and straight forward. But, it is skipping the 2nd index. I don't understand why?
Below is my code snippet :
if($scope.usersChoice.length == $scope.correctAnswers.length){
for(var p=0;p<$scope.usersChoice.length;p++) {
if($scope.usersChoice[p] == $scope.correctAnswers[p]){
$scope.score++;
}
}
}
Here the length is 10.
How do you know it is skipping the 2nd index? Because it should'nt.
Can you show us your arrays ?
By the way, you can use forEach instead of for loop in this case, since you don't seem to need to break your loop:
if($scope.usersChoice.length == $scope.correctAnswers.length){
$scope.usersChoics.forEach($choice,$index=>{
if($choice === $scope.correctAnswers[$index]) $scope.score++;
});
}

Where to store return messages for javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Where should I store message types javascript function is returning? I can just return string messages, but it seems like a bad idea. Are there any best practices?
For example in this kind of function (of course real functions make more sense then this)
function isFooValid(foo){
if(foo.length < 4){
return "TOO_SHORT";
} else if(foo.length >100) {
return "TOO_LONG";
} else if(foo.charAt(1) == 'A'){
return "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A";
}
}
You can create a variable for that. For example you can have:
myMessages ={
"secondChar": "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A",
"tooShort": "TOO_SHORT"
}
Then you can retrieve them as myMessages.tooShort, for example.
Even, if you want to have something similar to the string resources in android, you can store all the strings in a json file. If you load it before you js script, you are going to have the myMessages var available.

Should I wrap my function in if clause or use the opposite if condition and return prematurely? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If the title isn't clear enough, which is probably the case, here's what I mean :
Is it better to do this :
function example() {
if (condition) {
//The whole function code
}
}
or
function example() {
if (!condition) {
return;
}
//The whole function code
}
What is the cleanest way ?
Edit : I am not asking for your opinion. I'm wondering if there's any rule/convention/performance improvement.
I prefer the second way, "golden path" style.
Since I started to write all of my code like that I've found it to be a lot easier to read later. Perform checks in the order that makes sense, and return as soon as anything unexpected happens.
Wrapping code in a new block also adds indentation, which is kind of a waste of screen real-estate.

Categories