Retrieve the value of one of the two variables that undergo a change in value

In my current project, I am dealing with a function that takes in two variables. These variables are subject to change at any point during the execution of the program. My requirement is to identify which variable has changed so that further operations can be performed based on that specific variable.

I am looking for a method that can differentiate between the two types of variables (integer and string) and provide me with the information about which variable has changed in order to manipulate it for subsequent operations.

 private checkValues(param1: number, param2: string) {

    // Check values in array by Test
    for(let test of this.valuesT){
      if(test === param2 ){
        this.flagT = true;
        // console.log(this.flagT);
      }
    }
    // Check values in array by testNumber
    for(let testN of this.valuesTN){
      if(testN === param1){
        this.flagTN = true;
      }
    }
    
    // First validation every
    if(this.flagT && this.flagTN){
      return param1
    }
    // Return testNumber if exist changes
    if(this.flagT && !this.flagTN){
      this.valuesTN.push(param1)
      return param1
    } 
    // Return test if exist changes
    if(this.flagTN && !this.flagT){
      this.valuesT.push(param2)
      return param2
    }
    
  }

I have implemented a method that tracks the changes in variables based on their previous states stored in an array. However, this approach restricts the ability to revert back to a previous state as the data is already stored in the array.

Initially, both variables are set to the same value and are stored in the array. As a result, initially, both flags will be true.

Do you have any suggestions for improving this implementation?

Answer №1

What do you think about trying this approach?

function argumentChecker(initialArg1, initialArg2) {
  let savedArg1 = initialArg1;
  let savedArg2 = initialArg2;

  return function(newArg1, newArg2) {
    if (newArg1 !== savedArg1 && newArg2 !== savedArg2) {
      savedArg1 = newArg1;
      savedArg2 = newArg2;
      //....
      return savedArg1;
    }
    
    if (newArg1 !== savedArg1) {
      savedArg1 = newArg1;
      //....
      return savedArg1;
    }

    if (newArg2 !== savedArg2) {
      savedArg2 = newArg2;
      //....
      return savedArg2;
    }

    return savedArg1;
  };
}

// create checker
const myArgumentChecker = argumentChecker(1, 'hello');

// test checker
console.log(myArgumentChecker(1, 'world')); // output: world
console.log(myArgumentChecker(5, 'world')); // output: 5
console.log(myArgumentChecker(7, 'world')); // output: 7
console.log(myArgumentChecker(7, 'goodbye')); // output: goodbye
console.log(myArgumentChecker(7, 'hello')); // output: hello
console.log(myArgumentChecker(8, 'world')); // output: 8 - always returns first entry
console.log(myArgumentChecker(8, 'hello')); // output: hello - ensure last update was valid
console.log(myArgumentChecker(8, 'hello')); // output: 8 no change occurred

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

"Vue allows developers to define components by providing both template and script through the Vue()

I'm currently facing an issue while setting up a component within my global Vue() initialization. Surprisingly, I've managed to define the template for the component, but I'm struggling to specify the actual class that is executing the opera ...

Tips for showcasing an image on an HTML page

I am encountering an issue with my HTML page that includes a Button. When clicked, it is supposed to display an image using an Ajax call. The functionality works properly in Postman Rest Client. However, when accessed through a browser, it only shows raw ...

Issue with Sliding Transition in React Material UI Drawer Component

I developed a custom drawer component for my React application const CustomSidebar = () => { return ( <Drawer open={drawerOpen} onClose={() => setDrawerOpen(false)} > <Box> <Navigator / ...

Create a new chart using completely unique information

I am currently working on implementing the example found at http://bl.ocks.org/mbostock/1093130. The goal is to have the "update" function redraw the graph with a completely different dataset each time a button on the DOM is pressed. I have made some modif ...

Optimizing the retrieval of large volumes of data from AWS S3 using Node.js, implementing pagination and efficiently delivering records to the frontend

Below is the code to fetch data from a CSV file in AWS S3 using Node.js backend. The challenge here is handling more than 200k records without consuming too much memory and efficiently returning it to the frontend. AWS.config.update({ accessKeyId: ...

Transferring information from a Nuxt module to a plugin

I have been working on passing data from my module options to a plugin. Here is an example of how I am doing it: module.exports = function (moduleOptions) { const options = { ...this.options.moduleName, ...moduleOptions } this.addPlugin({ ...

Create an animation effect where a div increases in height, causing the divs beneath it to shift downward in a

My aim is to create columns of divs where the user can click on a div to see more content as the height expands. I've managed to set this up, but it seems there's an issue with the document flow. When I click on a div in the first column, the div ...

Tips for determining what elements are being updated in terms of style

I need assistance with modifying the functionality of a webpage's dropdown menu. Currently, it displays when the mouse hovers over it, but I want to change it to appear on click using my own JavaScript. Despite setting the onmouseout and onmouseover e ...

When using the POST method with npm http-server, an error 405 is thrown

I'm attempting to send an Ajax request to execute a php file on a local http server. However, the browser console shows Error 405: method not allowed. Even after trying solutions from similar questions and enabling CORS on the http-server, I still ca ...

What is the process of incorporating external links into an angular application?

Attempting to embed an external URL within my Angular app using an iframe has resulted in the following issue: https://i.sstatic.net/liSmX.png The error message reads as follows: https://i.sstatic.net/u9GWw.png Below is the template where I am trying t ...

Encountering an unidentified JSON path, while the data is clearly visible in the console.log output

I just started using Redux Toolkit and I'm having an issue with rendering data in the UI. The data is showing up successfully in the console, but when I try to display it in the UI, I'm getting an undefined JSON path {${weather[0].description} ${ ...

Angular view showcasing a JSON array

After retrieving data from the Laravel API, I used the following method: this.dataService.getData().subscribe(res=>{ this.contacts=res }); Upon receiving a JSON array response from Laravel like the one below, I attempted to iterate throu ...

Encountering a 'MODULE_NOT_FOUND' error when using the Svelte `node scripts/setupTypeScript.js` command

I encountered a problem in my Svelte project: Although my files display no errors in VSCode, when I run npm run dev --, all Typescript syntax is flagged as erroneous, and the server fails to start. To address this issue, I attempted removing all node_mod ...

Checkbox in Meteor always returns false after the template has been rendered

I've created a navigation bar with 2 options. One option is a checkbox and the other is a dropdown with a button (code provided below). The checkbox has the ID "inputMode" and the button has the ID "addNewObject" <div class="collapse navbar-colla ...

When you extend a service with another service in Angular, the service is instantiated twice

Within my Angular project, I have established a configuration that utilizes multiple services. One of these services contains certain functions that I wish to restrict access to, ensuring they can only be utilized by a specific other service. To accomplis ...

Show two columns horizontally using JavaScript

Hey, I'm working on a project where I need to display data from an array in an HTML output table. However, I'm facing an issue with displaying the table on the same page after clicking the submit button. Using document.write redirects me to anoth ...

Encountering the error message "No 'Access-Control-Allow-Origin' header is found on the requested resource."

While working with Angular on the frontend and Java Spring on the backend, I encountered an error message stating: 'No 'Access-Control-Allow-Origin' header is present on the requested resource.'. Even though CORS is enabled in my config ...

Implementing AJAX functionality to dynamically show a message on a webpage following an HTTP POST request

After submitting the form, I want to remain on the current page and show a message indicating whether the submission was successful or not. The HTTP POST request is processed like this: app.post('/insertdb', function(request, response) { // in ...

Tips and tricks for storing and retrieving form state using local storage with jQuery in JavaScript

I'm trying to save the form state in localstorage, I am able to save it successfully but I'm encountering an issue where I am unable to save the input typed data Desired outcome: If I type doggy inside the input box, I want that value to be ret ...

Assistance in changing an onClick function in JavaScript to onLoad

I've been working on updating a JavaScript function to trigger both on page load and window resize instead of just a click event. In the code snippet below, I've made adjustments by commenting out the section related to the click event, added "wi ...