Functions vs Arrow Function

Could anyone explain to me why I can’t get my array to fill with values when using a “normal” function script but I can when using the arrow function script?

I am a beginner and am trying to learn my way!

Function Script:

function tipAmountA (billValueB) {
    if (billValueB >=50 && billValueB <=300) {
    tipValue = billValueB * usualTip15
    } else {
        tipValue = billValueB * diffTip20 
}
}

console.log (tipAmountA(10))

const billsArrayB = [40,299,301]
console.log (tipValue)
const tipsArrayB =[tipAmountA (billsArrayB[0]),tipAmountA (billsArrayB[1])]
console.log (billsArrayB, tipsArrayB)

Arrow Function Script:

function tipAmountB (billValueB) {
    return billValueB >=50 && billValueB <=300 ? tipValue = billValueB * usualTip15 :
    tipValue = billValueB * diffTip20
}

console.log (tipAmountB(10))

const billsArrayB = [40,299,301]
console.log (tipValue)
const tipsArrayB =[tipAmountB (billsArrayB[0]),tipAmountB (billsArrayB[1])]
console.log (billsArrayB, tipsArrayB)
1 Like

If is the same as

And if that condition is met then the ? is the output, else would be same as :

It seems like your are not returning anything with the if statement.

Try this instead

if (billValueB >=50 && billValueB <=300) {
   return tipValue = billValueB * usualTip15;
    } else {
       return tipValue = billValueB * diffTip20;
}

or try this

return if (billValueB >=50 && billValueB <=300) {
   return tipValue = billValueB * usualTip15;
    } else {
       return tipValue = billValueB * diffTip20;
}

The reason I think this one works

return billValueB >=50 && billValueB <=300 ? tipValue = billValueB * usualTip15 :
    tipValue = billValueB * diffTip20

Is because you can write it in one line

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.