Conditional statements, loops and functions in a JavaScript script

Programming

Lab 3

Objective: Use conditional statements, loops and functions in a JavaScript script

Description:

For this lab, we are going to create a quiz. The quiz questions will be stored in one array and the corresponding answers will be stored in a second array. We will loop through the question array to prompt the user for the answer. When they enter an answer, we will compare that to the answers array. If it matches they get the points. If not, then they get 2 more tries to answer it correctly. If they get it right in 3 tries, they get points. If not, then no points. As soon as they answer it correctly or if the 3 tries expire, then move on to the next question. We will be using conditional statements (if), for and while loops and functions in this lab.

Requirements:

  • Create a web page named lab3.html. You can format the page any way that you like. Give the page a title and heading.
  • Create an external JavaScript file with the following:
    • Create an array of at least 3 trivia questions. You can use the array from lab 2 if you like.
    • Create a second array that contains the answers to the trivia questions in the same position as the questions.
    • Initialize a score variable to 0
    • Create a for loop that will use a counter from 0 to 2 (to correspond to the arrays). Inside the loop you should:
      • Call the quiz function (see below) passing the loop counter as a parameter.
      • The quiz function will return a point value. Save the point value in a variable and add it to the score.
      • close the loop
    • After the for loop is complete, write out the score to the page.
    • Create the quiz function that accepts one parameter which is the counter.
      • Initialize a guesses variable to 3. This will be used to count down the number of attempts the user has to answer the question correctly.
      • Create a while loop to run as long as guesses > 0. Inside the while loop:
        • prompt the user to answer the trivia question using the counter parameter as the array index.
        • compare the answer entered by the user to the corresponding answer in the answers array
          • If they answer correctly
            • return the guesses variable (Note: if the user gets it right on the first guess, it will return 3 for the points. If it’s the second guess, it will return 2 and the third guess 1)
          • else
            • subtract 1 from guesses
        • close the loop
      • After the while loop, return 0 to the calling script. (Note: if the user never got it right, we are return a point value of 0.
  • Be sure to link the external JavaScript file to the web page.
  • Upload your files to the server. Test and submit the assignment.
Lab Requirement Point Value
Create a Web page named Lab3.html. You can format the page any way that you like

5 points

Create an external JavaScript file and link it to your page 5 points

Create the questions and answers arrays with 3 elements each

5 points

Create the for loop to call the quiz function and add points to the score

10 points

Write out the score to the page

5 points

Create the quiz function with 1 parameter 10 points
Create the while loop based on guesses

10 points

Prompt the user with the question from the array 5 points
Use an if statement to compare the user’s answer to the answer array 10 points
Return a point value 5 points
Script executes correctly on the server

5 points

Total:

75 points

#write essay #research paper #blog writing #article writing #academic writer #reflective paper #essay pro #types of essays #write my essay #reflective essay #paper writer #essay writing service #essay writer free #essay helper #write my paper #assignment writer #write my essay for me #write an essay for me #uk essay #thesis writer #dissertation writing services #writing a research paper #academic essay #dissertation help #easy essay #do my essay #paper writing service #buy essay #essay writing help #essay service #dissertation writing #online essay writer #write my paper for me #types of essay writing #essay writing website #write my essay for free #reflective report #type my essay #thesis writing services #write paper for me #research paper writing service #essay paper #professional essay writers #write my essay online #essay help online #write my research paper #dissertation writing help #websites that write papers for you for free #write my essay for me cheap #pay someone to write my paper #pay someone to write my research paper #Essaywriting #Academicwriting #Assignmenthelp #Nursingassignment #Nursinghomework #Psychologyassignment #Physicsassignment #Philosophyassignment #Religionassignment #History #Writing #writingtips #Students #universityassignment #onlinewriting #savvyessaywriters #onlineprowriters #assignmentcollection #excelsiorwriters #writinghub #study #exclusivewritings #myassignmentgeek #expertwriters #art #transcription #grammer #college #highschool #StudentsHelpingStudents #studentshirt #StudentShoe #StudentShoes #studentshoponline #studentshopping #studentshouse #StudentShoutout #studentshowcase2017 #StudentsHub #studentsieuczy #StudentsIn #studentsinberlin #studentsinbusiness #StudentsInDubai #studentsininternational