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

Guide on organizing json object by various elements with distinct sorting patterns

Below is a JSON Object that needs to be sorted based on different conditions: var data1 = [ {id: "1", name: "b", lastname: "y", marks: "10"}, {id: "1", name: "a", lastname: "x", marks: "20"}, {id: "2", name: "a", lastname: "x", marks: "30" ...

Modifying the class of multiple images within an array

I am excited to share my first post here and express my gratitude for all the solutions I have found on this platform. I encountered an issue with using .removeClass and .addClass in my recent program. I am loading multiple pictures into an array called F ...

Displaying Various Items Based on the Language Selected in the Google Translate Widget

Currently, I am in the process of developing a website complete with a shopping cart that will feature different products based on the country of the customer. The client has requested the use of Google Translate to allow for language changes. To accommod ...

Parent controller was not in command before Child执行

When working with a parent view and a child view, I encounter an issue with accessing a json file. In the parent view, I retrieve the json file using the following code: $scope.services = Services.query(); factory('Services', function($reso ...

Guide on creating a custom directive called 'ng-let' to associate an expression with a variable

If I want to create a custom directive that functions like ng-repeat but assigns a name to a single variable: instead of writing code like this: ng-repeat="summary in data.accounts.all.summaryAsArray()" I could write something similar to this: ng-let=" ...

The UI Bootstrap Datepicker requires a second click in order to update the month

I am currently using the UI Bootstrap Datepicker component. An issue arises when I try to select the next month - the datepicker itself updates correctly, but the displayed month name does not change (refer to picture 1 and 2). Interestingly, after select ...

Hey there, what exactly does 'TypeError: Cannot access the 'scopedFn' property of an undefined object' mean?

Having trouble implementing RadListView with Nativescript-Vue. I am attempting to utilize a v-template for the header followed by another v-template for the list itself. 1) The header does not seem to be recognized, as only the standard v-template is disp ...

What is the best way to display a button only when a different element is in focus?

I am working on a feature where each row consists of two text inputs and a button. The goal is to show the button when a user focuses on one of the input fields, and hide it again when they lose focus. I have come up with this solution: const Input = ({inp ...

AngularJS json parsing error

After making an ajax request, I have received the following json data: { "status" : "success", "xplist" : [ { "rank" : "1", "username" : "test1", "xp" : "2500" }, { "rank" : "2", "username" : "test2", "xp" : "2200" }, { " ...

Ways to set a random value to a data attribute from a specified array while using v-for in Vue.js?

In my main Vue instance, I have a data attribute that consists of an array containing 4 items: var app = new Vue({ el: '#app', efeitosAos: ['fade', 'flip-up', 'slide-up', 'zoom-in'] ...

Tips for structuring a news thread with a staggered approach

On my Drupal 8 website, I have set up a newsfeed. How can I display the news items in staggered rows? I would like the first item to align on the left and the second item to be on the right, repeating this pattern for all subsequent items. Currently, I am ...

Selecting keys of an object in Angular 2

Attempting to fetch data from an API using key selection but encountering retrieval issues. File: app/app.component.ts import {Component, OnInit} from 'angular2/core'; import {Http} from 'angular2/http'; import {httpServiceClass} fro ...

Guide to launching my application by clicking on a file from a local device using React Native

Currently, I am working with React Native and Expo. I have a file with a unique extension (let's say: SomeFileName.xxx) which contains some text. My goal is to open this file from the device in such a way that it launches the application (either on An ...

remove a section from the main body

body { display: flex; justify-content: center; align-items: center; background: #0e1538; } <canvas id="spaceholder" width="804" height="604"></canvas> </div> <div class="MenüCenter"> <canvas id="canvas" width="800" ...

Leveraging server-side data with jQuery

When my client side JQuery receives an array of JSON called crude, I intend to access and use it in the following way: script. jQuery(function ($) { var x = 0; alert(!{JSON.stringify(crude[x])}); ...

Incorporate innerHTML by appending instead of overwriting

Just a quick question, can we actually add content to a <div id="whatEverId">hello one</div> by utilizing: document.getElementById("whatEverId").innerHTML += " hello two"; Is there a way to INSERT content into the div without replacing it??? ...

Automatically generated VueJS function

Creating a logging system for my Javascript Project using VueJS and Vuex To make logging methods accessible to all components, I am utilizing a global Mixin : import { mapState, mapActions } from 'vuex' import LogLevel from '@/enums/logger ...

Ways to refresh the DOM while making an asynchronous request

Consider the following async function: async function predict_from_model() { $("#loading").html("first update") const model = await tf.loadModel('model.json'); $("#loading").html("second update") //delayed for (var i = 0; i < 100; i++ ...

Why is my Laravel function retrieving outdated session data?

Currently, I am in the process of developing a shopping cart feature using session. My goal is to create a function that can update the quantity of an item in the shopping cart and refresh both the subtotal and the list of items in the cart displayed on th ...

Type into the asp:TextBox and then click on my button, where it triggers a JavaScript function

So here's my issue: <asp:TextBox runat='server' /> <button id='b2'>hi</button> <script> $('#b2').click(function(e){ e.preventDefault(); alert('you clicked the button'); }); </script ...