Homework Solution: Code Example 5-2 var getAvg = function () { var count = 1; var itemTotal = 0; var item = 0; while (…

    Code Example 5-2 var getAvg = function () { var count = 1; var itemTotal = 0; var item = 0; while (count < 4) { item = parseInt(prompt("Enter item cost: ")); itemTotal += item; count++; } var averageCost = parseInt(itemTotal/count); alert("Total cost: $" + itemTotal + ", Average cost: $" + averageCost); }; (Refer to Code Example 5-2) Suppose you tested the getAvg function by entering the values 5, 10, and 15 when prompted and the alert() method displayed “Total cost: $30, Average cost: $7”. What type of error is this?

    Expert Answer

     
    Error Type:-It is a logical error What is Logical error:-

    Method Example 5-2 var achieveAvg = duty () { var enumerate = 1; var individualTotal = 0; var individual = 0; timeliness (enumerate < 4) { individual = parseInt(prompt(“Penetrate individual cost: “)); individualTotal += individual; enumerate++; } var meanCost = parseInt(itemTotal/count); nimble(“Total cost: $” + individualTotal + “, Mean cost: $” + meanCost); }; (Refer to Method Example 5-2) Suppose you tested the achieveAvg duty by penetrateing the values 5, 10, and 15 when quicked and the nimble() process displayed “Total cost: $30, Mean cost: $7”. What character of hallucination is this?

    Expert Computeer-argument

     

    Hallucination Character:-It is a argumentative hallucination

    What is Argumentative hallucination:-

    Logic hallucinations can be the most up-hill character of hallucinations to footprint down. These hallucinations are referable the effect of a syntax or runtime hallucination. Instead, they supervene when you form a reach in the logic that drives your script and you do referable achieve the effect you expected.

    —————————————————-

    EXPLANATION:-

    —————————————————

    1.Initially enumerate=1

    2.In your method timeliness loop is general until enumerate is main than 4[while(count<4)]

    3.For primary timeliness loop enumerate=1,and quick the user penetrate input and enumerate increases(now enumerate=2)

    4.For avoid timeliness loop enumerate=2,and quick the user penetrate input and enumerate increases(now enumerate=3)

    5.For third timeliness loop enumerate=3,and quick the user penetrate input and enumerate increases(now enumerate=4)

    6.NOW enumerate=4.here timeliness loop seal.

    7.And combine of user penetrateed values procure stored in IndividualCost

    8.NOW mean procure be conducive using (ItemCost/count)

    9.here user penetrateed input values(ItemCost) “3” times.And enumerate=4(save enumerate should beseem 3 as user penetrateed 3 times)

    10.Here logic is damage.so this is a argumentative hallucination

    ——————————————————–

    Final JavaScript method:-

    ——————————————————–

    var achieveAvg = duty ()
    {
    var enumerate = 0;
    var individualTotal = 0;
    var individual = 0;
    timeliness (enumerate < 3)
    {
    individual = parseInt(prompt(“Penetrate individual cost: “));
    itemTotal += individual;
    count++;
    }
    var meanCost = parseInt(itemTotal/count);
    alert(“Total cost: $” + individualTotal + “, Mean cost: $” + meanCost);
    };
    getAvg();

    —————————————————–

    Output:-

    ——————————-

    —————————————————-

    EXPLANATION:-

    —————————————————

    1.Initially enumerate=0

    2.In your method timeliness loop is general until enumerate is main than 3[while(count<3)]

    3.For primary timeliness loop enumerate=0,and quick the user penetrate input and enumerate increases(now enumerate=1)

    4.For avoid timeliness loop enumerate=1,and quick the user penetrate input and enumerate increases(now enumerate=2)

    5.For third timeliness loop enumerate=2,and quick the user penetrate input and enumerate increases(now enumerate=3)

    6.NOW enumerate=3.here timeliness loop seal.

    7.And combine of user penetrateed values procure stored in IndividualCost(5+10+15=30)

    8.NOW mean procure be conducive using (ItemCost/count)

    9.here user penetrateed input values(ItemCost) “3” times.And enumerate=3.

    10.Finally using (ItemCost/count) we can count mean as (30/3)=10.