0

I'm just learning. Why does the 'tarifComm' value that I pass to the 'calcCondition' function become NaN. it doesn't let me calculate. how can i fix this problem?

input data

const input = 700000, const step = 3, let tarifComm = 0, let tempInput = input

array

const mass = [
{
  "step": "1",
  "min": "0",
  "max": "150000",
  "percent": "0.75",
  "fix": "79"
},
{
  "step": "2",
  "min": "150001",
  "max": "500000",
  "percent": "1.5",
  "fix": "79"
},
{
  "step": "3",
  "min": "500001",
  "max": "2000000",
  "percent": "2.5",
  "fix": "79"
},
{
  "step": "4",
  "min": "2000001",
  "max": "99999999",
  "percent": "4",
  "fix": "79"
}]

function dividing the calculation into parts

function calcStep() {
for(let i = 0; i < step; i++) {
    console.log(`комиссия в начале пути  ${tarifComm}`)
    console.log(`создан ${tempInput}`)
    if (tempInput > mass[i].max) {
        tarifComm += calcCondition(i, tarifComm)
        console.log(`комиссия на ${i+1} шаге: ${tarifComm}`)
        tempInput = tempInput - mass[i].max
        console.log(`изменен в макс ${tempInput}`)
    } else if (tempInput < mass[i].max) {
        console.log(`комиссия в мин: ${tarifComm}`)
        console.log(`остаток в мин ${tempInput}`)
        tarifComm += tempInput / 100 * mass[i].percent
    }
}
tarifComm += Number(mass[step].fix)} calcStep()

and calculation function

function calcCondition(i, tarifComm) {
console.log(`комиссия попавшая в расчет ${tarifComm}`)
if (mass[i].free && !mass[i].percent) {
    tarifComm += 0
} else if (!mass[i].free && mass[i].percent) {
    tarifComm += mass[i].max / 100 * mass[i].percent
}}
  • 1
    Your `calcCondition` has no return value, so it implicitly returns `undefined`. When you do `tarifComm += calcCondition(i, tarifComm)`, you're doing `tarifComm += undefined`, which results in `NaN`. Add a `return tarifComm` to your `calcCondition`. – T.J. Crowder Jan 27 '22 at 10:51
  • Obviously, you can use any indentation and bracing style in your own code that you want, but when writing code for other people to read (such as in a question here on SO), please use any of the standard styles so that people can read it. Hiding your `}` away at the end of the last statement of the block is ***very*** hard to read, as is not indenting blocks consistently. – T.J. Crowder Jan 27 '22 at 10:53

0 Answers0