Skip to content

Decimal as first button causes error #1

@jesseparent

Description

@jesseparent

When the calculator is first loaded or AC/All Clear, if you press the decimal(".") button first, an error occurs on line 27 because the currentOperand is null

TypeError: Cannot read properties of undefined (reading 'includes')
reducer
C:/mystuff/UofU Bootcamp/projects/calculator/src/App.js:27
  24 | if (payload.digit === "0" && state.currentOperand === "0") {
  25 |   return state
  26 | }
> 27 | if (payload.digit === "." && state.currentOperand.includes(".")) {
     | ^  28 |   return state
  29 | }
  30 | 

This can be fixed by using optional chaining on the currentOperand on line 27:

if (payload.digit === "." && state.currentOperand?.includes(".")) {

STEPS TO REPRODUCE
Load Calculator or Press AC button
Press "."

EXPECTED RESULTS
The calculator display shows "0."

ACTUAL RESULTS
Browser displays error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions