Function returns integer number between values from & to .
function randomNumFromTo(from, to) {
from = parseInt(from)
to = parseInt(to)
if (from === to && from === 0) [from, to] = [1, 100]
if (isNaN(from) || isNaN(to)) [from, to] = [1, 100]
if (from > to) [from, to] = [to, from]
return Math.floor(Math.random() * (to - from + 1) + from);
}
import React, { useState } from 'react';
import randomNumFromTo from '../../../helpers/functions/randomNumFromTo';
import useInput from '../../../helpers/functions/useInput';
const style = { width: '50px', marginRight: '10px' };
function Component() {
const [valState, setValState] = useState(0);
const [inputFromState, bindInputFrom] = useInput()
const [inputToState, bindInputTo] = useInput()
const clickHandler = () => {
setValState(randomNumFromTo(inputFromState, inputToState))
turnAnimationOn()
}
return (
<>
<input placeholder='from' style={style} {...bindInputFrom} />
<input placeholder={'to'} style={style} {...bindInputTo} />
<button onClick={clickHandler}>Get random integer</button>
<div> Random number: {valState}</div>
</>
)
}