0
import React, { Component } from 'react'
export default class Ccomponent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            first: 0,
            second: 0,
            Otv: 0
        };
        this.firstChange = this.firstChange.bind(this);
        this.secondChange = this.secondChange.bind(this);
        this.Otvet = this.Otvet.bind(this);
        this.ConvertToInt = this.ConvertToInt.bind(this);
    }
firstChange(event){
    this.setState({
        first: event.target.value
    });
    this.Otvet()
}
secondChange(event){
    this.setState({
        second: event.target.value
    });
    this.Otvet()
}
Otvet(event){   
    this.setState({
        Otv: this.state.first+this.state.second
    });
    this.ConvertToInt();
}
ConvertToInt(event){
    var a = this.state.first;
    var b = this.state.second;
    let v = a+b //i can try use parceInt, error - 'parceInt' is not defined  no-undef
    console.log(v);
}
    render() {
        console.log(this)
        return(
            <div>
               <input type='number' value = {this.state.first} onChange={this.firstChange}/>
               <input type='number' value = {this.state.second} onChange={this.secondChange}/>
               <h3>Summa: {this.state.first}+{this.state.second}</h3>
               <h1>Otvet: {this.state.Otv}</h1>
            </div>
        )
}}

My input send value to object in string format, how i can convert str to int, if parceInt doesn't worked. Maybe there is a way to get the value in the form int? Or maybe react have internal function for converting string to integer?

4
  • You've got a typo. But, there are three ways to convert a string to an int: 1) parseInt(), 2) Number(), 3) +"1" Commented Mar 21, 2021 at 20:56
  • @RandyCasburn Where did I make a typo? Commented Mar 21, 2021 at 21:00
  • In the comment, parceInt !== parseInt Commented Mar 21, 2021 at 21:02
  • @RandyCasburn thank you, I should have slept more :D Commented Mar 21, 2021 at 21:25

1 Answer 1

1

parceInt() isn't spelled correctly. Small typo 😅. It's spelled like parseInt() Try that out.

For example,

let str = "123"
let num = parseInt(str)
console.log(num) // 123
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.