[Solved] javascript passing functions nan and undefined errors


This is the corrected version of your code.

var takeOrder = function(topping, crustType) {
    console.log('Order: ' + crustType + ' pizza topped with ' + topping);
    return 1;
}

var getSubTotal = function(orderCount) {
    var subTotal = (orderCount * 7.5);
    console.log('Subtotal of ' + subTotal.toFixed(2) + ' with item count ' + orderCount);
    return subTotal;
}

var getTax = function(subTotal) {
    var tax = subTotal * 0.06;
    console.log('Tax is ' + tax.toFixed(2));
    return tax;
}

var getTotal = function(subTotal, tax) {
    var total = subTotal + tax;
    console.log('Total is ' + total.toFixed(2));
    return total;
}

var orderCount = 0;
orderCount += takeOrder(orderCount, 'bacon', 'thin crust');
orderCount += takeOrder('cheese', 'thick crust');
orderCount += takeOrder('pepperoni', 'medium crust');

var subTotal = getSubTotal(orderCount);
var tax = getTax(subTotal);
var total = getTotal(subTotal, tax);

Summary of corrections

  1. Made functions return Number rather than String

  2. Now all functions are almost pure functions (don’t cause side-effects other than logging)

  3. Formatted numbers prior to printing them by rounding them to the second decimal

  4. Fixed typographic errors (tatal rather than total).

  5. Added parenthesis to function invocations (e.g: getTax() rather than getTax

  6. Removed redundant invocations

  7. More

solved javascript passing functions nan and undefined errors