Tips for streamlining a conditional statement with three parameters

Looking to streamline this function with binary inputs:

export const handleStepCompletion = (userSave: number, concur: number, signature: number) => {
    if (userSave === 0 && concur === 0 && signature === 0) {
        return {completed: false, active: true, error: false};
    }
    if ((userSave === 1 && concur === 0 && signature === 0) ||
        (userSave === 1 && concur === 1 && signature === 0)) {
        return {completed: true, active: true, error: false};
    }
    if (userSave === 1 && concur === 0 && signature === 1) {
        return {completed: false, active: false, error: true};
    }
};

Answer №1

If you're just starting out, a structure like this might help:

const determineStepStatus = (userSave: number, concur: number, signature: number) => {
    return {
        completed: (userSave && concur && signature),
        active: (userSave && !signature && !concur),
        error: (signature && !concur)
    }
};

Remember, this is a basic outline and may not cover every scenario. Feel free to enhance the logic as needed.

Answer №2

const checkStepCompletion = (user: number, concurrence: number, sign: number) => {

    if (user === 0 && concurrence === 0 && sign === 0) {
        return {completed: false, active: true, error: false};
    }
    if(user === 1 && concurrence === 0 || sign === 0){
        return {completed: true, active: true, error: false};
    }
    if (user === 1 && concurrence === 0 && sign === 1) {
        return {completed: false, active: false, error: true};
    }
}

Give this a try! I combined two conditions into one for more clarity.

Answer №3

Here is my solution, concise and efficient

const processStep = (saveStatus, confirmation, completedSignature) => {

    return {completed: Boolean(completedSignature) ? false : Boolean(saveStatus), 
           active: Boolean(completedSignature) ? false : !Boolean(confirmation), 
            error: Boolean(completedSignature)};
 
};

const output = processStep(0, 0, 0);
console.log(output)

Answer №4

What are your thoughts on this? I have removed the parameter types for it to be displayed correctly in the comment section, and included all potential scenarios that I could think of.

const evaluateStepStatus1 = (userSave, concur, signature) => {
 return {
 completed: (userSave === 1 && concur === 0 && signature === 0) ||
        (userSave === 1 && concur === 1 && signature === 0),
        active: (userSave === 0 && concur === 0 && signature === 0) ||  (userSave === 1 && concur === 0 && signature === 0) || (userSave === 1 && concur === 1 && signature === 0),
        error: (userSave === 1 && concur === 0 && signature === 1)
        };

};

 const evaluateStepStatus2 = (userSave, concur, signature) => ({
    completed: !!(userSave && !signature),
    active: !!(!(!userSave && concur) && !signature),
    error: !!(userSave && !concur && signature),
  });
  
 console.log(evaluateStepStatus1(0,0,0), evaluateStepStatus2(0,0,0))
console.log(evaluateStepStatus1(1,0,0), evaluateStepStatus2(1,0,0))
console.log(evaluateStepStatus1(0,1,0), evaluateStepStatus2(0,1,0))
console.log(evaluateStepStatus1(0,0,1), evaluateStepStatus2(0,0,1))
console.log(evaluateStepStatus1(0,1,1), evaluateStepStatus2(0,1,1))
console.log(evaluateStepStatus1(1,1,1), evaluateStepStatus2(1,1,1))
console.log(evaluateStepStatus1(1,1,0), evaluateStepStatus2(1,1,0))
console.log(evaluateStepStatus1(1,0,1), evaluateStepStatus2(1,0,1))

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

The interaction between a JavaScript function call and C# is not functioning properly

Attempting to invoke the JavaScript function from CodeBehind ( C# ) : function scrollToBottom() { window.scrollTo(0, document.body.scrollHeight); } The function successfully executes when directly called from my asp.net application. However, ...

What are some ways to improve performance in JavaScript?

Can you help me determine which approach would be more efficient - using native functions of the language that involve 2 iterations or a simple for loop? The goal is to locate the index in an array of objects where the property filterId matches a specific ...

Ensuring form field accuracy through server-side validation using Ajax - Mastering the art of Ajax

I am in need of implementing Ajax to validate my form fields, currently I have a javascript validation set up. Is it possible to reuse my existing javascript code and integrate Ajax for form validation? HTML Form <form method="post" action="ajax/valid ...

Tips for expanding button width in 'react-native-swipeout' to enable swipe action on ListView row (React Native)

Currently, I am exploring how to implement the component found here: https://github.com/dancormier/react-native-swipeout My goal is to have the row swiped all the way. Is there a method to increase the button width so that it covers the entire width of th ...

Creating a dedicated class or module specifically designed for handling import and export tasks is a highly efficient approach towards stream

Exploring different ways to import multiple classes into a single class file: import myClass1 'pathto1'; import myClass2 'pathto2'; import myClassn 'pathton'; Seeking a simpler method using one file (class export) with al ...

Will the .replaceWith() method alter the code visible to search engines?

After successfully modifying the content of specific H1 elements to not return the value from a global variable using the following code; <script type="text/javascript"> $(document).ready(function() { $("H1").filter(function() { return $(this).text ...

The Angular error message InvalidValueError is thrown when the Map function expects a mapDiv of type HTMLElement, but instead receives a

When attempting to initialize Google Maps, I encountered a particular problem. In this div, I am trying to load the map but consistently receiving the same error message. I attempted to use ngAfterViewInit() in case the view wasn't fully loaded befo ...

Having trouble setting a JavaScript variable with a PHP session variable

In my PHP file, I have a session variable named $_SESSION['SESS_USER_TYPE'] set to a 2-character string. After the PHP script redirects to an HTML file, I need to retrieve this session variable in a JavaScript variable. This is how I am attempti ...

Tips for incorporating a visible marker beside the dropdown arrow

When developing my React application with React hooks forms, I have a select component where I need to include a clear indicator 'x' icon next to the dropdown icon. The current select component code is: <Form.Control size="sm" as=&q ...

Is there a way for me to move the dot icon along the dotline?

Here is the code snippet: jsFiddle I have successfully implemented a click event to change the style of selected dots in dotline. Now, my query is : How can I enable dragging functionality for the dot icon within dotline? Your assistance on this matte ...

Adjust the colors of two elements by clicking a button's onclick event

As stated in the title, I have a button in the books component. When this button is clicked, the color of a div within the books component and another div in the navbar component should change. - Below is the code for the books component : export class Bo ...

Issues with React's onSelect event functionality are persisting

Is there an event handler in React for the onSelect statement? For example, when a user highlights some text within a paragraph using their mouse, is there a way to trigger an event that extracts the highlighted text? import React, { Component } from &apo ...

Guide to importing a markdown document into Next.js

Trying to showcase pure markdown on my NextJS Typescript page has been a challenge. I attempted the following: import React, { useState, useEffect } from "react"; import markdown from "./assets/1.md"; const Post1 = () => { return ...

Using jQuery to loop through a table and retrieve the button value from every row

I have a challenge with dynamically created buttons in a table. My goal is to loop through the table, identify the rows that have a checked checkbox, and retrieve the value of a button within that row. I then need to store these values in an array. Unfortu ...

`Is it possible to modify the background color of InputAdornment in Material-UI TextField?`

For the first 10% of the text field, one background color is used, while for the remaining 90%, another background color is applied. <TextField className={classes.root} type="text" placeholder="username" var ...

Stop unauthorized pages from hijacking login cookies

I have a website called where users can create their own HTML pages accessible through the link if the username is "USR" and project is "PROJECT". However, there is a security concern... Currently, I store users' login tokens as cookies. But what ...

Utilizing ExtJS and its control feature

I am facing an issue with the following code block: run: function(e, row){ var me = this; var container = Ext.getCmp('centercontainer'); try { container.removeAll(); } catch(e) { } // The code snippet below is act ...

Unable to obtain a response even after providing the correct API key in a Node.js POST request

Can you please assist me in troubleshooting the POST request code below? Here is a snippet of the code: const express = require("express"); const bodyParser = require("body-parser"); //const request = require("request"); const https = require("https"); c ...

After retrieving a value from attr(), the object does not have the 'split' method available

I need to implement the split method on a variable fetched using attr. This is the code snippet I am attempting: $(document).ready(function() { $('.some_divs').each(function() { var id = $(this).attr('id'); var ida = ...

What is the best way to retrieve data in Vue from Node.js?

app.js router.get('/', (req, res) => { var cart = req.cookies.cart; res.sendFile(path.join(__dirname,'../../src/components/cart.html'),cart); }) shoppingCart.html <body> <div id="shop" class="container mt-3"&g ...