Всем привет, ниже приведен код, где функции valueOfFirstNum и valueOfSecondNum индентичны, но если пытаюсь функцию, куда аргументом передаю firstNum или secondNum функция entryValues перестает работатюь, в чем проблема?
function entryValues() {
if ((secondNum === '') && (!operations.includes(enteredValue)) && (operator === '') && (firstNum.length < 10) && (allowedValues.includes(enteredValue))) {
valueOfFirstNum();
} else if (operations.includes(enteredValue)){
operatorValue();
} else if ((firstNum !== '') && (operator !== '') && (!operations.includes(enteredValue)) && (allowedValues.includes(enteredValue)) && (secondNum.length < 10)) {
valueOfSecondNum()
}
console.log(firstNum,secondNum,operator)
}
entryValues();
function valueOfFirstNum() {
screen.classList.remove('divideByZero');
firstNum += enteredValue;
screen.value = firstNum;
if ((firstNum.indexOf(0) === 0) && (enteredValue !== '%') && (enteredValue !== '.') && (enteredValue === '0')) {
firstNum = '0';
screen.value = firstNum;
} else if ((firstNum.indexOf(0) === 0) && (enteredValue !== '0') && (!firstNum.toString().includes('.'))) {
firstNum = firstNum.substring(1);
screen.value = firstNum;
} else if ((firstNum.indexOf('.') === 0)) {
firstNum = `0${firstNum}`;
screen.value = firstNum;
}
}
function operatorValue() {
operator = enteredValue;
screen.value = firstNum;
if ((operations.includes(enteredValue)) && (secondNum !== '') && (res !== 0)) {
screen.value = res;
secondNum = '';
firstNum = res;
} else if ((res === 0)) {
firstNum = '';
secondNum = '';
operator = '';
res = '';
screen.value = 0;
} else if ((secondNum === '') && (enteredValue === '=')) {
firstNum *= 2;
screen.value = firstNum;
}
}
function valueOfSecondNum() {
secondNum += enteredValue;
screen.value = secondNum;
if ((secondNum.indexOf(0) === 0) && (enteredValue !== '%') && (enteredValue !== '.') && (enteredValue === '0')) {
secondNum = '0';
screen.value = secondNum;
} else if ((secondNum.indexOf(0) === 0) && (enteredValue !== '0') && (!secondNum.toString().includes('.'))) {
secondNum = secondNum.substring(1);
screen.value = secondNum;
} else if (secondNum.indexOf('.') === 0) {
secondNum = `0${secondNum}`;
screen.value = secondNum;
}
}
Переделываю вот так:
function valueNum(num) {
num += enteredValue;
screen.value = num;
if ((num.indexOf(0) === 0) && (enteredValue !== '%') && (enteredValue !== '.') && (enteredValue === '0')) {
num = '0';
screen.value = num;
} else if ((num.indexOf(0) === 0) && (enteredValue !== '0') && (!num.toString().includes('.'))) {
num = num.substring(1);
screen.value = num;
} else if (num.indexOf('.') === 0) {
num = `0${num}`;
screen.value = num;
}
}
function entryValues() {
if ((secondNum === '') && (!operations.includes(enteredValue)) && (operator === '') && (firstNum.length < 10) && (allowedValues.includes(enteredValue))) {
valueNum(firstNum);
} else if (operations.includes(enteredValue)){
operatorValue();
} else if ((firstNum !== '') && (operator !== '') && (!operations.includes(enteredValue)) && (allowedValues.includes(enteredValue)) && (secondNum.length < 10)) {
valueNum(secondNum)
}
console.log(firstNum,secondNum,operator)
}
entryValues();