Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The snippets of JavaScript below contain syntax errors and/or logic errors. Iden

ID: 3672273 • Letter: T

Question

The snippets of JavaScript below contain syntax errors and/or logic errors. Identify the errors and insert your corrected code.

a. function main()

{

     var score;

     var total;

     for(i = 0; i < 5; i++)

     {

            score = getScore(score);

            total = computeTotal(total, score);

     }

                   console.log("The total of all the exams is " + total);

}

function getScore (score)

{

     score = Number(prompt("Enter your exam score"));

     return score;

}

function computeTotal (total, score)

{

     total = total + score;

     return total;

}

main();

b.function main()

{

     var num;

     var endProgram = 0;

     while (endProgram = 0)

     {

                num = Number(prompt("Enter a number to be squared"));

                num = num * num;

                console.log("Your number squared is " + num);

                               

                endProgram = Number(prompt("Enter y to square another number"));

     }

}

main();

c.function main()

{

var avg;

              var total = 0;

var num;

var ctr = 0

do

{

                                                   num = Number(prompt("Enter a number"));

                                                  total = total + num;

}

while (ctr < 5)

avg = total / ctr;

console.log("The average of your numbers is " + avg);

}

main();

d.function main()

{

     for (i = 0, i < 10, i++)

                                 {

        console.log ("The value of i is " + i);

                                 }

}

main();

e.function main()

{

                    var billAmt = 0;

                    var billTotal = 0;

                      var moreBills = 1;

                      do while(moreBills == 1)

                        {

                               billAmt = Number(prompt("Enter the amount of your bill"));

                               billTotal = billTotal + billAmt;

                               moreBills = Number(prompt("Enter 1 if you have another bill to process"));

                      }

                      console.log("Your bill total is " + billTotal);

               }

               main();

Explanation / Answer

Please find the errors as in bolded :

a. function main()

{

     var score;

     var total; // it should be initialized to 0; otherwise it will give NaN will adding

     for(i = 0; i < 5; i++)

     {

            score = getScore(score);

            total = computeTotal(total, score);

     }

                   console.log("The total of all the exams is " + total);

}

function getScore (score)

{

     score = Number(prompt("Enter your exam score"));

     return score;

}

function computeTotal (total, score)

{

     total = total + score;

     return total;

}

main();

---------------------------------

b)

function main()

{

     var num;

     var endProgram = 0;

     while (endProgram = 0) // this is asign statement and it is assigning 0, so while(0) won't execute at all

     {

                num = Number(prompt("Enter a number to be squared"));

                num = num * num;

                console.log("Your number squared is " + num);

                               

                endProgram = Number(prompt("Enter y to square another number"));

     }

}

main();

------------------------------------------------------------------------------------------

c.function main()

{

var avg;

              var total = 0;

var num;

var ctr = 0

do

{

                                                   num = Number(prompt("Enter a number"));

                                                  total = total + num;

}

while (ctr < 5) // here ctr is not increamenting so, it will always have the value 0, thus the loop will be goes infinte

avg = total / ctr;

console.log("The average of your numbers is " + avg);

}

main();

-------------------------------------------------------------------------------------------

d.function main()

{

     for (i = 0, i < 10, i++) //here comma is used, it should be semi-colon ';'

                                 {

        console.log ("The value of i is " + i);

                                 }

}

main();

----------------------------------------------------------------------------------------------------------------

e.function main()

{

                    var billAmt = 0;

                    var billTotal = 0;

                      var moreBills = 1;

                      do while(moreBills == 1) // do..while syntax is wrong

                        {

                               billAmt = Number(prompt("Enter the amount of your bill"));

                               billTotal = billTotal + billAmt;

                               moreBills = Number(prompt("Enter 1 if you have another bill to process"));

                      }

                      console.log("Your bill total is " + billTotal);

               }

               main();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote