JS Calculator Failing Test 13

I’m failing test 13 and I know why, I just don’t know how to fix it with the code I have.

class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.previousOperandTextElement = previousOperandTextElement
    this.currentOperandTextElement = currentOperandTextElement
    this.clear()
  }

  clear() {
    this.currentOperand = '0'
    this.previousOperand = ''
    this.operation = undefined
  }

  delete() {
    this.currentOperand = this.currentOperand.toString().slice(0, -1)
  }

  appendNumber(number) {
    if (number === '.' && this.currentOperand.includes('.')) return
    this.currentOperand = this.currentOperand.toString() + number.toString()
  }

  chooseOperation(operation) {
    if (this.currentOperand === '') return;
    if (this.previousOperand !== '') {
      this.compute();
    }
    this.operation = operation;
    this.previousOperand = this.currentOperand
    this.currentOperand = 0
  }

  compute() {
    let computation
    const prev = parseFloat(this.previousOperand)
    const current = parseFloat(this.currentOperand)
    if (isNaN(prev) || isNaN(current)) return
    switch (this.operation) {
      case '+':
        computation = prev + current
        break
      case '-':
        computation = prev - current
        break
      case '*':
        computation = prev * current
        break
      case '÷':
        computation = prev / current
        break
      default:
        return
    }
    this.currentOperand = computation
    this.operation = undefined
    this.previousOperand = ''
  }

  getDisplayNumber(number) {
    const stringNumber = number.toString()
    const integerDigits = parseFloat(stringNumber.split('.')[0])
    const decimalDigits = stringNumber.split('.')[1]
    let integerDisplay
    if (isNaN(integerDigits)) {
      integerDisplay = ''
    } else {
      integerDisplay = integerDigits.toLocaleString('en', { maximumFractionDigits: 0 })
    }
    if (decimalDigits != null) {
      return `${integerDisplay}.${decimalDigits}`
    } else {
      return integerDisplay
    }
  }

  updateDisplay() {
    this.currentOperandTextElement.innerText =
      this.getDisplayNumber(this.currentOperand)
    if (this.operation != null) {
      this.previousOperandTextElement.innerText =
        `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`
    } else {
      this.previousOperandTextElement.innerText = ''
    }
  }
}


const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')

const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)

numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText)
    calculator.updateDisplay()
  })
})

operationButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.chooseOperation(button.innerText)
    calculator.updateDisplay()
  })
})

equalsButton.addEventListener('click', button => {
  calculator.compute()
  calculator.updateDisplay()
})

allClearButton.addEventListener('click', button => {
  calculator.clear()
  calculator.updateDisplay()
})

deleteButton.addEventListener('click', button => {
  calculator.delete()
  calculator.updateDisplay()
})

Welcome to the community @CoreyDunn! Specifically, what test is failing?

The sequence "5 * - 5" = should produce an output of "-25" : expected '-5' to equal '-25'
AssertionError: The sequence "5 * - 5" = should produce an output of "-25" : expected '-5' to equal '-25'
    at Proxy.c (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:525:1889)
    at Proxy.u (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:407:130)
    at n.strictEqual (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:550:655)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:178063
    at l (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:79440)
    at Generator._invoke (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:79193)
    at Generator.next (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:79799)
    at r (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:1:1054)
    at s (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:1:1265)

@CoreyDunn would you mind pasting the markup code as well?

It’s done through react, so once the App() is exported it is just ddisplayed… but here it is as well.

import './App.css';
import React from 'react';
window.onload = import('./Calculator.js');

<script src="Calculator.js"></script>

function App() {
  return (
    <div className="calculator-grid">
      <div id='display' className="output">
        <div data-previous-operand className="previous-operand"></div>
        <div data-current-operand className="current-operand"></div>
      </div>
      <button id="clear" data-all-clear className="span-two">AC</button>
      <button id="delete" data-delete>DEL</button>
      <button id="divide" data-operation>÷</button>
      <button id="one" data-number>1</button>
      <button id="two" data-number>2</button>
      <button id="three" data-number>3</button>
      <button id="multiply" data-operation>*</button>
      <button id="four" data-number>4</button>
      <button id="five" data-number>5</button>
      <button id="six" data-number>6</button>
      <button id="add" data-operation>+</button>
      <button id="seven" data-number>7</button>
      <button id="eight" data-number>8</button>
      <button id="nine" data-number>9</button>
      <button id="subtract" data-operation>-</button>
      <button id="decimal" data-number>.</button>
      <button id="zero" data-number>0</button>
      <button id="equals" data-equals className="span-two">=</button>
    </div>
  )
}

export default App;

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.