Home > database >  Why i can't calculate big numbers in React.js?
Why i can't calculate big numbers in React.js?

Time:12-22

error on calculating big numbers

calculates small numbers

import { useState } from 'react';
import './App.css';
import Button from './components/Button';
import { Input } from './components/input';
import * as math from 'mathjs';
import Header from '../src/components/Header';

const App=()=> {
  const buttons= [
 {id:7,symbol:"7"},{id:8,symbol:"8"},{id:9,symbol:"9"}]

  const buttons2=[{id:4,symbol:"4"},{id:5,symbol:"5"},{id:6,symbol:"6"},{id:' ',symbol:" "}]
  
  const buttons3=[{id:1,symbol:"1"},{id:2,symbol:"2"},{id:3,symbol:"3"},{id:'-',symbol:"-"}]

  const buttons4=[{id:'.',symbol:"."},{id:0,symbol:"0"},{id:'/',symbol:"/"},{id:'*',symbol:"x"}

]


  const[text,setText]=useState("");
  const[result,setResult]=useState("");

  const addToText=(val)=>{
    setText((text)=>[...text, val " "]);
  }

  const resetInput=()=>{
    setText("");
    setResult("");
  }   

 

  const calculateResult=()=>{
    try{
  const input=text.join("");
  setResult(math.evaluate(input));

  
}catch(err){
setResult("Error")
}
  }

I have tried differents method to solve the problem, but still have no idea why it doesn't work. Plaese Help. Project from frontend mentor. If you need more code let me know. Thankss you

CodePudding user response:

import {evaluate} from 'mathjs';

then try some static strings. for example

//console.log(text.join(""));
evaluate('sin(45 deg) ^ 2')    // 0.5
evaluate('9 / 3   2i')         // 3   2i

if the examples worked fine then the problem is in your 'text' string but if the examples didn't work then the problem is in your import and module

CodePudding user response:

const addToText=(val)=>{ setText((text)=>[...text, val " "]); } the mistake was in this code. I wrote val " " insteaf of val "". Therefore it didn't calculate big numbers(example:55 58)

  • Related