AngularJS Nested IF - angularjs

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)

Related

How to filter a list based on multiple conditions?

I am trying to filter a List with multiple conditions. The main thing is, I must use the condition if it is true and not if it is false. If the condition is false I should not use that to filter. Below is my code
void performFiltering(bool homeVisits, bool onTheSpotServices)
{
//Check for home and on the spot filtering
if(homeVisits==true)
{
filteredOrganizationList = orgList.where((org) => org.homeVisits==true);
}
else if(onTheSpotServices==true)
{
filteredOrganizationList = orgList.where((org) => org.onTheSpotService==true);
}
else if(homeVisits==true && onTheSpotServices==true)
{
filteredOrganizationList = orgList.where((org) => (org.onTheSpotService==true) ||(org.homeVisits==true) );
}
}
here I have made simple if-else statements. Nothing serious. But I can't do this when there are more conditions. Luckily it is just 2 conditions, but I have much more to come.
Also carefully notice that I have used OR Command in the last statement. That means get results where either homeVisits=true or onTheSpotServices=true
Whats the most effective way of handing this?
there is no need for a cascade of multiple if-elses
instead use a single where with a custom test function:
filteredOrganizationList = orgList.where((org) =>
homeVisits && org.homeVisits ||
onTheSpotServices && org.onTheSpotService ||
... // rest of your filter tests
);

onClick conditional rendering for three conditions

I have onClick that I want to hit a certain function depending on the following logic:
onClick={
approve && this.handleApproveClick,
!approve && !releaseHold && this.handleDeclineClick,
releaseHold && this.handleReleaseHoldClick
}
Oddly enough the last this.handleReleaseHoldClick works, while the others do not. What is the correct way to do this? Or do I really need to create a separate button?
Why does only the last work?
It's a basic comma operator case, where the "comma operator evaluates each of its operands (from left to right) and returns the value of the last operand".
What is the correct way to do this?
Since it's a triple-condition function, I'd suggest you to create a class method and simply secure each of possible cases.
handleSomeClick = () => {
if (approve) {
this.handleApproveClick();
} else if (!approve && !releaseHold) {
this.handleDeclineClick();
} else if (releaseHold) {
this.handleReleaseHoldClick();
}
}
and inside JSX:
onClick={this.handleSomeClick}

Why my OR Operator in angularjs is not working?

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 */ }

Jump out of for loop in kotlin

I have this simple loop and condition, but you see below I can't jump out of loop :
rwloop# for (z in rendered_words.size-1 downTo 0 )
{
var css_=rendered_words[z].node.attr("class")
css_?.let {
if (css_=="classzero") {
break#rwloop
}
}
}
But I receive this error in break#rwloop :
break' or 'continue' jumps across a function or a
class boundary
Drop the let lambda since the #rwloop label is not visible inside it and use this:
rwloop# for (z in rendered_words.size-1 downTo 0 )
{
var css_=rendered_words[z].node.attr("class")
if (css_ != null) {
if (css_=="classzero") {
break#rwloop
}
}
}
https://kotlinlang.org/docs/reference/inline-functions.html#non-local-returns
It states that
break and continue are not yet available in inlined lambdas, but we are planning to support them too.
So, you should
Wait until its support comes
Or use local return statement instead,
How?
The lambda is a function itself, so you can return from it, this (if it is the last thing in the for loop like your case) will make the same effect of continue
rwloop# for(z in rendered_words.size-1 downTo 0 ) {
var css_=rendered_words[z].node.attr("class")
css_?.let {
if (css_=="classzero") {
return#let
}
}
}
Kotlin considers the lambda as a boundary (it is not an inner class because it is inlined), so you can't cross it by break nor continue till now.

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
}

Resources