What strategies can be implemented to transform a lengthy if-else statement into a more optimized

I have a piece of code where I am setting the status of two scope variables based on an AND operation.

Depending on the key, I call the relevant method. The only difference between the two methods is checking prop3.

I believe the code is quite redundant and I'm unsure how to optimize it. Do you have any ideas on how I can achieve my objective with less code?

if(key =='White')
    _checktests1();
else
    _checktests2 ();

var _checktests1 = function () {
    if ($scope.test.Prop1 == "one" && $scope.test.Prop2 == "two")
        $scope.checkWhiteStatus = true;
    else
        $scope.checkWhiteStatus = false;

    if ($scope.test.Prop1 == "three" && $scope.test.Prop2 == "four" )
        $scope.checkGreenStatus = true;
    else
        $scope.checkGreenStatus = false;
}
var _checktests2 = function () {
    if ($scope.test.Prop1 == "one" && $scope.test.Prop2 == "two" && $scope.test.Prop3 == "five")
        $scope.checkWhiteStatus = true;
    else
        $scope.checkWhiteStatus = false;

    if ($scope.test.Prop1 == "three" && $scope.test.Prop2 == "four" $scope.test.Prop6 == "six")
        $scope.checkGreenStatus = true;
    else
        $scope.checkGreenStatus = false;
}

Answer №1

Here are a couple of conditional checks you can perform:

$scope.checkWhiteStatus = ($scope.test.Prop1 == "one" && $scope.test.Prop2 == "two" && (key =='White' || $scope.test.Prop3 == "five"));

$scope.checkGreenStatus = ($scope.test.Prop1 == "three" && $scope.test.Prop2 == "four" && (key =='White' || $scope.test.Prop6 == "six"));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions on extracting a JWT token from an external API for user authentication, followed by saving the user's name and email address into the database

After researching numerous articles and Stack Overflow questions, I have identified my problem and outlined my requirements below: Upon accessing the Angular application, I require immediate user authentication to retrieve their name and email. This auth ...

Understanding the mechanism of callback function in NodeJS within the context of routes and controllers

Trying to grasp the concept of callbacks and puzzled by the recurring issue TypeError: callback is not a function Here's my router setup: // getPriceRouter.js router.post('/getPrice', function(req, res) { priceController.getPrice(req, ...

Angular with D3 - Semi-Circle Graph Color Order

Can someone assist me with setting chart colors? I am currently using d3.js in angular to create a half pie chart. I would like to divide it into 3 portions, each represented by a different color. The goal is to assign 3 specific colors to certain ranges. ...

A more detailed explanation of Angular's dot notation

I came across a solution for polling data using AngularJS here on stackoverflow. In this particular solution (shown below), a javascript object is used to return the response (data.response). I tried replacing the data object with a simple javascript arra ...

Difficulty with formatting decimal points in JavaScript

I seem to be having an issue with decimal places in my code. Currently, it's showing the result as 123 123 12 but I actually need it to be displayed as 12 312 312. Is there anyone who can assist me with formatting this correctly? Here is the section ...

Node Express for Advanced Routing

I'm currently developing a web application that will handle CRUD operations on an array within a collection. Here is the structure of the collection model: var mongoose = require('mongoose'); var website = require('./website'); ...

When deploying, an error is occurring where variables and objects are becoming undefined

I've hit a roadblock while deploying my project on Vercel due to an issue with prerendering. It seems like prerendering is causing my variables/objects to be undefined, which I will later receive from users. Attached below is the screenshot of the bui ...

Sequentially loading Bootstrap columns as the page loads

Is there a way to load columns one by one with a time gap when the page is loaded? Here's the code snippet that can achieve this: setTimeout(function() { $("#box1").removeClass("noDisplay"); },1000); setTimeout(function() { ...

Update the collapse panel content using ng-repeat

Managing a list of projects with multiple tasks can be quite challenging. One approach is to utilize a collapse panel where each project serves as the title, and the tasks are displayed within the content section. However, one common issue faced in this se ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...

HighCharts velocity gauge inquiry

I recently integrated a highcharts speedometer with PHP and MYSQL on my website. Everything seemed to be working smoothly until I added the JavaScript code for the speedometer, causing it not to display. There are no error messages, just a blank screen whe ...

Emailer: Missing Salutation

While attempting to send emails using Node with Nodemailer (https://github.com/nodemailer/nodemailer), the sendMail call from the Nodemailer transporter is throwing an error message of Greeting never received when connected to an Ethereal test email accoun ...

AJAX function in Chrome console is throwing an error message stating "Unexpected Token }"

Dealing with this issue has been quite unusual for me. I've spent the last 3 days trying to troubleshoot it, but now it's no longer bothering me. The situation involves a button and a textbox that sends the data from the textbox to a PHP page whe ...

Exploring the Ins and Outs of Debugging JavaScript in Visual Studio Using

I encountered a peculiar issue while testing some code. When the program is executed without any breakpoints, it runs smoothly. However, if I introduce a breakpoint, it halts at a certain point in the JSON data and does not allow me to single-step through ...

Not quite sure about the best way to showcase the results // using JavaScript

My code is posted below. I am trying to achieve a functionality where, upon clicking the 'Calculate Price' button, the results showing the number of cars, type of cars, and their respective prices are displayed beneath the button. Despite this be ...

Fetch information that was transmitted through an ajax post submission

How can I retrieve JSON formatted data sent using an ajax post request if the keys and number of objects are unknown when using $_POST["name"];? I am currently working on a website that functions as a simple online store where customers can choose items m ...

Fixed position scrollable tabs with Material UI

I recently implemented material-ui scrollable tabs in my project, referencing the documentation at https://mui.com/material-ui/react-tabs/#scrollable-tabs. Here is a snippet of the code I used: <Tabs value={value} onChange={handleChange ...

Error message: Encountered JavaScript heap out-of-memory error during Azure DevOps React Container Production Build

I am facing challenges in building a React production Docker container with Azure DevOps pipelines. Despite upgrading my build environment and code, the pipeline failed to run successfully. After conducting some research, I attempted to add the "--node-fla ...

What methods can a controller use to verify the legitimacy of the scope?

I'm a bit perplexed when it comes to validation in angular. It seems like all of the validation is connected to the form. But what happens when the controller needs to ascertain if the model is valid or not? Here's an example I quickly whipped u ...

Tips for resolving an Angular 504 Error Response originating from the backend layer

I am currently facing an issue with my setup where I have an Angular application running on localhost (http) and a Spring Boot application running on localhost (https). Despite configuring the proxy in Angular to access the Spring Boot APIs, I keep receivi ...