2

I create a function for this. Maybe exists a better solutions for this?

function writeLog(operationIdentifier, prevResult, operationNumber, newResult) {
const logEntry = {
    operation: operationIdentifier,
    prevResult: prevResult,
    number: operationNumber,
    result: newResult,
};
logEntries.push(logEntry);
console.log(logEntries);

From this:

function add() {
    const enteredNumber = getUserNumberInput();
    const initialResult = currentResult;
    currentResult += enteredNumber;
    createAndWriteOutput('+', initialResult, enteredNumber);
    writeLog('ADD', initialResult, enteredNumber, currentResult);
}
Eugene
  • 53
  • 1
  • 5
  • 1
    Please try this answer below: https://stackoverflow.com/questions/18713415/user-activity-tracking-or-logging-with-javascript – Elman Huseynov Mar 28 '20 at 09:45

1 Answers1

2

You can try to use Proxy for logging

const increment = a => a + 1;

const proxy = new Proxy(increment, {
  apply(target, thisArg, args) {
    console.log(`Incrementing the value "${args[0]}"`);
    const result = target(...args);
    console.log(`Result: "${result}"`);
    return result;
  },
});

increment(5); 
/* nothing logs here */

proxy(5);
/* Incrementing the value "5".
Result: "6" */
Max Starling
  • 967
  • 8
  • 8