Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

percentage with react

import React, { useState } from "react";

export default function App() {
  const [pointsGiven, setPointsGiven] = useState(0);
  const [pointsPossible, setPointsPossible] = useState(0);
  const [percentage, setPercentage] = useState(0);

  const calculate = (e) => {
    e.preventDefault();
    const formValid = +pointsGiven >= 0 && +pointsPossible > 0;
    if (!formValid) {
      return;
    }
    setPercentage((+pointsGiven / +pointsPossible) * 100);
  };

  return (
    <div className="App">
      <form onSubmit={calculate}>
        <div>
          <label>points given</label>
          <input
            value={pointsGiven}
            onChange={(e) => setPointsGiven(e.target.value)}
          />
        </div>

        <div>
          <label>points possible</label>
          <input
            value={pointsPossible}
            onChange={(e) => setPointsPossible(e.target.value)}
          />
        </div>
        <button type="submit">calculate</button>
      </form>
      <div>percentage:{percentage}</div>
    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript record video in browser 
Javascript :: recaptcha v3 js 
Javascript :: mongoose express js post 
Javascript :: polyfill for call 
Javascript :: react native measure 
Javascript :: css javascript 
Javascript :: google maps address autocomplete in angular npm 
Javascript :: reactstrap search bar 
Javascript :: es6 foreach dom element 
Javascript :: react native asyncstorage getItem example 
Javascript :: toast js 
Javascript :: react-timeago npm 
Javascript :: online password generator 
Javascript :: Progress bar loader angular 
Javascript :: find the largest array from an array in javascript 
Javascript :: style through javascript 
Javascript :: storybook global decorator 
Javascript :: jquery with svelte 
Javascript :: object destructuring es6 
Javascript :: how to fetch data redux 
Javascript :: how to change Mime type of a file express 
Javascript :: upload file angular rest api 
Javascript :: form changes button enable reactive forms 
Javascript :: jquery accordion toggle close open 
Javascript :: unshift javascript 
Javascript :: axios 
Javascript :: match if 
Javascript :: javascript reflect 
Javascript :: button clicker code 
Javascript :: js get variable from url 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =