Why my OR Operator in angularjs is not working? - angularjs

I am using a if condition in angularjs to check is a status is != Delivered or status is != Canceled then alert a message
if($scope.itemDeliveredStatus != "Canceled" || $scop.itemDeliveredStatus != "Delivered"))
{
alert("Please set item status to Delivered! or Canceled!");
return false;
}

Typo on the second condition, you put $scop instead of $scope :
$scop.itemDeliveredStatus
and an extra ) at the end of that line which is not necessary

First you do have a typo, but that is only one small issue. The other is a logic problem.
Your logic is wrong with the use of OR. Simple example below shows you that.
var foo = "A";
if (foo !== "A" || foo !== "B") {
console.log("WHY?")
}
Why? Because you are saying that in order for this if statement to be true, the variable foo has to be equal to two things.
You want it to say if foo does not equal A AND foo does not equal B, than show error.
var foo = "A";
if (foo !== "A" && foo !== "B") {
console.log("WHY?")
} else {
console.log("Better!!!")
}
So with the two issues fixed, your code would look like:
if ($scope.itemDeliveredStatus != "Canceled" && $scope.itemDeliveredStatus != "Delivered") { /*.. code */ }

Related

how do i rewrite this ternary operator in C?

How do I write in a loop instead of a ternary operator ?
temp->status = (inStore ? waiting : called);
would it be like:
if (inStore){
return waiting;
}
else (
return called;
}
I'm unsure becauseI get an error doing this, I'm using it in a void function
The problem is here: else (. Change the ( to { and your compile should be clean.
The ternary operator is simply an if-then-else statement in a shortcut syntax, with the assignment statement presented as its single lvalue. So:
temp->status = (inStore ? waiting : called);
is translated to:
if(inStore == true)
{
temp->status = waiting;
}
else
{
temp->status = called;
}
Note that there is nothing wrong with your syntax (except maybe the "(" here: else (). In a function, it might be preferable to use return statements if the function required no clean up before leaving.
You have to assign waiting or called to temp->status variable instead of returning it, also in else you used parenthesis wrongly. Whole thing should be:
if (inStore)
temp->status = waiting;
else
temp->status = called;
I am not sure why you asked for a loop in that case, as there is no need to use one.
if(inStore){
temp->status = waiting;
} else { // there was a ( instead of a {
temp->status = called;
}

annidate variable undefined - angular

I have a annidate variable like that.
$scope.a = { b: {c : 1} };
var test = $scope.a.b.c;
// test == 1
$scope.a = {}
var test = $scope.a.b.c;
// ERROR
I want test variable will be null or undefined.
How can I fill test variable without error?
some advice?
I'm looking for a smart way
not only
if(angular.isDefinied($scope.a) && angular.isDefinied($scope.a.b) && angular.isDefinied($scope.a.b.c))
You probably want to do something like that:
var test = ($scope.a && $scope.a.b && $scope.a.b.c) ? $scope.a.b.c : undefined;
If you are not defined the value before using it, then definitely, it will throw an error. You must define or you must check whether it is defined or not. Otherwise you can't resolve the error. var test will get a value only if there is no error. Now, its up to you to decide and use a smart way
var test = $scope.a?((typeof($scope.a.b)=="object" && $scope.a.b.c)?$scope.a.b.c:(typeof($scope.a)=="object" && $scope.a.b)?$scope.a.b:null):null

How to check if value is number

I have this function :
$scope.SearchTicketEvent = function (ticketPinOrEvent)
{
if (ticketPinOrEvent != undefined)
{
if (ticketPinOrEvent.length == 10)
{
$scope.PinTicketSearch(ticketPinOrEvent);
}
}
}
How can i check if ticketPinOrEvent is number ? I tried with angular.isNumber(ticketPinOrEvent) but i dont get anything?
If you want to use angular.isNumber
if ( !isNaN(ticketPinOrEvent) && angular.isNumber(+ticketPinOrEvent)) {
}
You might use the typeof to test if a variable is number.
if (typeof ticketPinOrEvent === 'number') {
$scope.PinTicketSearch(ticketPinOrEvent);
}
Or might try this:
if (!isNaN(ticketPinOrEvent) && angular.isNumber(ticketPinOrEvent)) {
$scope.PinTicketSearch(ticketPinOrEvent);
}
Testing against NaN:
NaN compares unequal (via ==, !=, ===, and !==) to any other value --
including to another NaN value. Use Number.isNaN() or isNaN() to most
clearly determine whether a value is NaN. Or perform a
self-comparison: NaN, and only NaN, will compare unequal to itself.
In Angular 6 this works without using any prefix.
Example:
if(isNumber(this.YourVariable)){
// your Code if number
}
else {
// Your code if not number
}

AngularJS Nested IF

I'm trying to add a bit of logic to my controller with a couple of nested If statements. By themselves they work fine, but after nesting them, i'm not getting any results.
This is a working statement:
if (typeof object["Frequency"]!='undefined' && object["Frequency"]=='yearly' && ('now' <= 'upcoming')) {
$scope.summary[segment].totalLateRelationships++;
$scope.summary[segment].lateRelationships.push(object);
}
This is working:
if (!(object["nextmeetingowner"].length) || !(object["nextmeetingowner"].length) ) {
$scope.summary[segment].totalLateRelationships++;
$scope.summary[segment].lateRelationships.push(object);
}
This is what I'm trying to accomplish:
if (!(object["primaryaccountability"].length) || (!(object["nextmeetingowner"].length))) {
if (typeof object["Frequency"]!='undefined' && object["Frequency"]=='yearly' && ('now' <= 'upcoming'))
{
$scope.summary[segment].totalLateRelationships++;
$scope.summary[segment].lateRelationships.push(object);
}
}
That third code block is checking something different than the first two. It's evaluating
!(object["primaryaccountability"].length)
...whereas the earlier code is evaluating
!(object["Last Meeting Date"].length)

what is the difference between angular.isUndefined(value) and not !(value)?

I've tried:
if(angular.isUndefined(value)){
// something
}
and
if(!(value)){
// something
}
Is there a difference between the two?
Is there a use-case to choose one instead of the other?
var foo = false;
if(!foo) {
// will log
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will not log as foo is defined
console.log("foo is undefined")
}
another example without define foo
if(!foo) {
// will throw exception "Uncaught ReferenceError: foo is not defined "
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will log
console.log("foo is undefined")
}
so effective angular.isUndefined(foo) does nothing else than evaluating
if(typeof foo == "undefined")
wrapped for saving 1 character yeah.
while !-operator checks if a defined variable evaluates to false
so
if(!foo)
is the same like
if( foo != true)
UPDATE:
As stated in comments, when i write "evaluates to false" there is false null undefined NaN ""(empty string) and 0 included
! is the logical not operator in JavaScript while angular.isUndefined(value) checks if the reference is undefined
Which one to use completely depends on what you are trying to do in the end.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators and https://docs.angularjs.org/api/ng/function/angular.isUndefined

Resources