figbertmath

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

consecNum.js (4366B)


      1 import React from 'react';
      2 import PropTypes from 'prop-types';
      3 import { CalcOut } from "../lowLevel/calcOut";
      4 import { Pad } from "../lowLevel/pad";
      5 
      6 export class ConsecNumCalc extends React.Component {
      7     constructor(props) {
      8         super(props);
      9         this.state = {
     10             output: '',
     11             amount: '',
     12             sum: '',
     13             type: 'Mixed'
     14         };
     15         this.onChange = this.onChange.bind(this);
     16         this.onTypeClick = this.onTypeClick.bind(this);
     17     }
     18 
     19     onChange(object) {
     20         const value = object.target.value,
     21             name = object.target.name;
     22         if (name === 'Amount') {
     23             this.setState({ amount: value }, () => {
     24                 if (this.state.amount !== '' && this.state.sum !== '') {
     25                     this.solveConsec()
     26                 }
     27             });
     28         } else if (name === 'Sum') {
     29             this.setState({ sum: value }, () => {
     30                 if (this.state.amount !== '' && this.state.sum !== '') {
     31                     this.solveConsec()
     32                 }
     33             });
     34         }
     35     }
     36 
     37     onTypeClick() {
     38         if (this.state.type === 'Mixed') {
     39             this.setState({ type: 'Even' }, () => {
     40                 if (this.state.amount !== '' && this.state.sum !== '') {
     41                     this.solveConsec()
     42                 }
     43             });
     44         } else if (this.state.type === 'Even') {
     45             this.setState({ type: 'Odd' }, () => {
     46                 if (this.state.amount !== '' && this.state.sum !== '') {
     47                     this.solveConsec()
     48                 }
     49             });
     50         } else {
     51             this.setState({ type: 'Mixed' }, () => {
     52                 if (this.state.amount !== '' && this.state.sum !== '') {
     53                     this.solveConsec()
     54                 }
     55             });
     56         }
     57     }
     58 
     59     solveConsec() {
     60         const amount = Number(this.state.amount),
     61             sum = Number(this.state.sum),
     62             type = this.state.type,
     63             getSum = (total, num) => {
     64                 return total + num;
     65             };
     66         let smallestNum,
     67             step,
     68             addend = 0,
     69             numAmount = parseInt(amount, 10),
     70             numSum = parseInt(sum, 10),
     71             allNums = [];
     72         if (type === 'Odd') {
     73             smallestNum = numSum > 0 ? 1 : -1;
     74             step = numSum > 0 ? 2 : -2;
     75         } else if (type === 'Even') {
     76             smallestNum = 0;
     77             step = numSum > 0 ? 2 : -2;
     78         } else if (type === 'Mixed') {
     79             smallestNum = 0;
     80             step = numSum > 0 ? 1 : -1;
     81         }
     82         for (let a = 0; a < numAmount; a++) {
     83             allNums.push(smallestNum + addend);
     84             addend += step;
     85         }
     86         if (numSum > 0) {
     87             while (
     88                 allNums.reduce(getSum) !== numSum &&
     89                 allNums.reduce(getSum) < numSum
     90                 ) {
     91                 for (let a = 0; a < allNums.length; a++) {
     92                     allNums[a] += step;
     93                 }
     94             }
     95         } else if (numSum < 0) {
     96             while (
     97                 allNums.reduce(getSum) !== numSum &&
     98                 allNums.reduce(getSum) > numSum
     99                 ) {
    100                 for (let a = 0; a < allNums.length; a++) {
    101                     allNums[a] += step;
    102                 }
    103             }
    104         }
    105         if (allNums.reduce(getSum) > numSum || allNums.reduce(getSum) < numSum) {
    106             this.setState({
    107                 output: 'ERROR'
    108             })
    109         } else {
    110             this.setState({
    111                 output: allNums.toString()
    112             })
    113         }
    114     }
    115 
    116     render() {
    117         return (
    118             <div>
    119                 <CalcOut mode={this.props.mode} output={this.state.output} />
    120                 <Pad
    121                     buttonValues={['amount', 'sum', 'type', 'select']}
    122                     displayValues={['Amount', 'Sum', this.state.type, '…']}
    123                     mode={this.props.mode}
    124                     onChange={this.onChange}
    125                     onClick={this.onTypeClick}
    126                     onModeClick={this.props.onModeChange}
    127                     textValues={[this.state.amount, this.state.sum]}
    128                     type={'large'}
    129                 />
    130             </div>
    131         );
    132     }
    133 }
    134 ConsecNumCalc.propTypes = {
    135     mode: PropTypes.string.isRequired,
    136     onModeChange: PropTypes.func.isRequired
    137 };