figbertmath

[RADIOACTIVE] miscellaneous math programs in website form
git clone git://git.figbert.com/figbertmath.git
Log | Files | Refs | README

commit 9a15cedcf76d629fd6627808c937d0261d33cad7
parent 525d9023c01f02b22655e267dd66f6dfc119274c
Author: therealFIGBERT <figbertwelner@gmail.com>
Date:   Thu, 28 Nov 2019 13:43:09 -0800

Add consecutive number solver

Diffstat:
Msrc/components/App.js | 3+++
Asrc/components/highLevel/consecNum.js | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/components/lowLevel/largeButton/index.js | 11+++++++++++
3 files changed, 151 insertions(+), 0 deletions(-)

diff --git a/src/components/App.js b/src/components/App.js @@ -2,6 +2,7 @@ import React from 'react'; import { ModeSelect } from "./highLevel/modeSelect"; import { SimpleCalc } from "./highLevel/simpleCalc"; import { AngSizeCalc } from "./highLevel/angSize"; +import { ConsecNumCalc } from "./highLevel/consecNum"; export class App extends React.Component { constructor(props) { @@ -24,6 +25,8 @@ export class App extends React.Component { return <SimpleCalc mode={this.state.mode} onModeChange={this.changeMode}/>; } else if (this.state.mode === 'angSize') { return <AngSizeCalc mode={this.state.mode} onModeChange={this.changeMode}/>; + } else if (this.state.mode === 'consecNum') { + return <ConsecNumCalc mode={this.state.mode} onModeChange={this.changeMode}/>; } else { return <ModeSelect mode={this.state.mode} onButtonPress={this.changeMode} />; } diff --git a/src/components/highLevel/consecNum.js b/src/components/highLevel/consecNum.js @@ -0,0 +1,136 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { CalcOut } from "../lowLevel/calcOut"; +import { Pad } from "../lowLevel/pad"; + +export class ConsecNumCalc extends React.Component { + constructor(props) { + super(props); + this.state = { + output: '', + amount: '', + sum: '', + type: 'Mixed' + }; + this.onChange = this.onChange.bind(this); + this.onTypeClick = this.onTypeClick.bind(this); + } + + onChange(object) { + const value = object.target.value, + name = object.target.name; + if (name === 'Amount') { + this.setState({ amount: value }, () => { + if (this.state.amount !== '' && this.state.sum !== '') { + this.solveConsec() + } + }); + } else if (name === 'Sum') { + this.setState({ sum: value }, () => { + if (this.state.amount !== '' && this.state.sum !== '') { + this.solveConsec() + } + }); + } + } + + onTypeClick() { + if (this.state.type === 'Mixed') { + this.setState({ type: 'Even' }, () => { + if (this.state.amount !== '' && this.state.sum !== '') { + this.solveConsec() + } + }); + } else if (this.state.type === 'Even') { + this.setState({ type: 'Odd' }, () => { + if (this.state.amount !== '' && this.state.sum !== '') { + this.solveConsec() + } + }); + } else { + this.setState({ type: 'Mixed' }, () => { + if (this.state.amount !== '' && this.state.sum !== '') { + this.solveConsec() + } + }); + } + } + + solveConsec() { + const amount = Number(this.state.amount), + sum = Number(this.state.sum), + type = this.state.type, + getSum = (total, num) => { + return total + num; + }; + let smallestNum, + step, + addend = 0, + numAmount = parseInt(amount, 10), + numSum = parseInt(sum, 10), + allNums = []; + if (type === 'Odd') { + smallestNum = numSum > 0 ? 1 : -1; + step = numSum > 0 ? 2 : -2; + } else if (type === 'Even') { + smallestNum = 0; + step = numSum > 0 ? 2 : -2; + } else if (type === 'Mixed') { + smallestNum = 0; + step = numSum > 0 ? 1 : -1; + } + for (let a = 0; a < numAmount; a++) { + allNums.push(smallestNum + addend); + addend += step; + } + if (numSum > 0) { + while ( + allNums.reduce(getSum) !== numSum && + allNums.reduce(getSum) < numSum + ) { + for (let a = 0; a < allNums.length; a++) { + allNums[a] += step; + } + } + } else if (numSum < 0) { + while ( + allNums.reduce(getSum) !== numSum && + allNums.reduce(getSum) > numSum + ) { + for (let a = 0; a < allNums.length; a++) { + allNums[a] += step; + } + } + } + if (allNums.reduce(getSum) > numSum || allNums.reduce(getSum) < numSum) { + this.setState({ + output: 'ERROR' + }) + } else { + this.setState({ + output: allNums.toString() + }) + } + } + + render() { + return ( + <div> + <CalcOut mode={this.props.mode} output={this.state.output} /> + <Pad + type={'large'} + buttonValues={['amount', 'sum', 'type', 'select']} + displayValues={['Amount', 'Sum', this.state.type, '…']} + textValues={[this.state.amount, this.state.sum]} + onClick={this.onTypeClick} + onChange={this.onChange} + onModeClick={this.props.onModeChange} + /> + </div> + ); + } +} +ConsecNumCalc.propTypes = { + mode: PropTypes.string.isRequired, + onModeChange: PropTypes.func.isRequired +}; +\ No newline at end of file diff --git a/src/components/lowLevel/largeButton/index.js b/src/components/lowLevel/largeButton/index.js @@ -17,6 +17,17 @@ export class LargeButton extends React.Component { {this.props.displayValue} </button> ); + } else if (this.props.value === 'type') { + return ( + <button + type='button' + className={styles.button} + value={this.props.value} + onClick={this.props.onClick} + > + {this.props.displayValue} + </button> + ); } if (canBeDisabled) { if (!this.props.disabledTruth) {