|
@@ -45,19 +45,22 @@ function Addition() {
|
|
}else{
|
|
}else{
|
|
// add input number to result array
|
|
// add input number to result array
|
|
resArr.unshift(imdtRes);
|
|
resArr.unshift(imdtRes);
|
|
- if(nosLeft === 0){
|
|
|
|
- setShowAlert(true);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- const handleCarryChange = (e, noOfDigits, idx) => {
|
|
|
|
|
|
+ const handleCarryChange = (e, noOfDigits, idx, nosLeft) => {
|
|
if(typeof carryArr === "undefined"){
|
|
if(typeof carryArr === "undefined"){
|
|
setCarryArr(Array(noOfDigits).fill("-"));
|
|
setCarryArr(Array(noOfDigits).fill("-"));
|
|
}
|
|
}
|
|
let carryArrCopy = [...carryArr]
|
|
let carryArrCopy = [...carryArr]
|
|
carryArrCopy[idx] = e.target.value
|
|
carryArrCopy[idx] = e.target.value
|
|
setCarryArr(carryArrCopy);
|
|
setCarryArr(carryArrCopy);
|
|
|
|
+ let noCarry = carryArrCopy[0] === "0" || carryArrCopy[0] === undefined
|
|
|
|
+ if(nosLeft === 0 || (nosLeft === 1 && noCarry) ||
|
|
|
|
+ // stop after first iteration, numbers left is undefined at first
|
|
|
|
+ (nosLeft !== nosLeft && realResult.toString().length === 1 && noCarry)){
|
|
|
|
+ setShowAlert(true);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
const handleSubmit = (e) => {
|
|
const handleSubmit = (e) => {
|
|
@@ -95,6 +98,7 @@ function Addition() {
|
|
handleResChange(".", nosLeft)
|
|
handleResChange(".", nosLeft)
|
|
imdtResIdx = imdtResIdx - 1;
|
|
imdtResIdx = imdtResIdx - 1;
|
|
carryOffset = 1;
|
|
carryOffset = 1;
|
|
|
|
+ nosLeft = nosLeft - 1;
|
|
}
|
|
}
|
|
|
|
|
|
for (let n in numbers){ // iterate numbers
|
|
for (let n in numbers){ // iterate numbers
|
|
@@ -141,7 +145,7 @@ function Addition() {
|
|
{carryText}
|
|
{carryText}
|
|
</label>
|
|
</label>
|
|
<input
|
|
<input
|
|
- onChange={(e) => handleCarryChange(e, noOfDigits, imdtResIdx)}
|
|
|
|
|
|
+ onChange={(e) => handleCarryChange(e, noOfDigits, imdtResIdx, nosLeft)}
|
|
type="text" id="input_carry" size="2"
|
|
type="text" id="input_carry" size="2"
|
|
aria-labelledby="input_carry_label" aria-required="true"
|
|
aria-labelledby="input_carry_label" aria-required="true"
|
|
ref={carryInputField}/>
|
|
ref={carryInputField}/>
|