what are the appropriate way of writing a function for a delete button for a calculator?

Viewed 31

I am a react beginner! have been trying to write a code inside deletehandler function wish was assign to delete button, whereby if the button is clicked, one or more value on the calculator screen should be erase or removed.

How can I get this done?

This is my code so far;

const deleteClickHandler = () => {

    let num = calc.num ? toLocaleString(removeSpaces(-calc.num)) : 0;
    let res = calc.res ? toLocaleString(removeSpaces(-calc.res)) : 0;

    setCalc({
        ...calc,
        num: (num /= splice(0, -1)),
        res: (res /= splice(0, -1)),
        sign: "",
    });
}
1 Answers

If you are removing the last index in an array, for instance, you can use pop()

let numbers = [71, 6, 3 , 45, 5]; 
numbers.pop();

you can also apply this kind of approach with your res KEY

// [71, 6, 3, 45]
Related