• Home
  • Blog
  • use conditional statements loops and functions in a javascript script

use conditional statements loops and functions in a javascript script

0 comments

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

 
Do you need a similar assignment done for you from scratch? We have qualified writers to help you. We assure you an A+ quality paper that is free from plagiarism. Order now for an Amazing Discount! Use Discount Code “Newclient” for a 15% Discount!NB: We do not resell papers. Upon ordering, we do an original paper exclusively for you.

The post use conditional statements loops and functions in a javascript script appeared first on Top Premier Essays.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}