Question

In: Computer Science

-create a magic 8 ball program in Javascript. -use a loop to ask for the question...

-create a magic 8 ball program in Javascript.
-use a loop to ask for the question
-use a random number to get the answer let randAnswer = Math.round(Math.random()*10);
-must have at least10 different answers
-must use elseif or switch statement
-must outputs both answers and user to input the console
-program should repeat indefinitely until either blank input or cancel is selected

Solutions

Expert Solution

Complete Code:

<html>
<head>
<script>
<!-- JavaScript code -->
var responses = ['Please ask again later...', 'Yes', 'No','It appears to be so','Yes, definitely','What is it you really want to know?', 'My sources say no','Signs point to yes', 'Don\'t count on it', 'Can`t predict now']; <!-- different responses to questions-->
var repeated; <!-- to check if question is repeated -->
function change()
{
var ques = document.getElementById('ques_id').value; <!-- to store question -->
if(ques.endsWith("?")) <!-- check whether question ends with a question mark (?) or not -->
{
if(repeated==ques) <!-- compare current question with the previous question -->
{
alert("Please ask different question..");
}
else
{
var rand = responses[Math.round(Math.random() * responses.length)]; <!-- generate response using random function -->
document.getElementById('ans_id').innerHTML = rand; <!-- set answer -->
repeated = ques; <!-- question -->
}
}
else
alert("Please enter a question with mark '?'"); <!-- input question-->
}
</script>
</head>

<body>
<center>

<h1>Magic 8 Ball</h1>
<h4>What would you like to know?</h4>
<textarea rows="1" cols="50" id=ques_id></textarea></br></br>
<input onclick="change()" type="button" value="Ask the 8 Ball" id=myButton1"></input>
<h4>The 8 Ball says :</h4>
<i><p id="ans_id">Ask the 8 Ball a question...</p></i>

</center>
</body>
</html>

OUTPUT:


Related Solutions

Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the user places the ball face down, asks a yes-or-no question and turns the ball face up to reveal the answer. The standard magic 8 ball has 20 standard answers shown below, where 10 are positive (green), 5 are non-committal (yellow), and 5 are negative (red) (credit: Wikipedia) Write a program that does the following: 1. Stores all the responses in a String array using...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
Write a program that displays a weekly payroll report. A loop in the program should ask...
Write a program that displays a weekly payroll report. A loop in the program should ask the user for the employee number, gross pay, state tax, federal tax, and FICA withholdings. The loop will terminate when 0 is entered for the employee number. After the data is entered, the program should display totals for gross pay, state tax, federal tax, FICA withholdings, and net pay. Input Validation: Do not accept negative numbers for any of the items entered. Do not...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number...
Write a JAVA program that emulates a Magic Eight Ball. Start by generating a random number and then use a switch statement to display the message. Use this website as a resource on possible answers a Magic Eight Ball gives: https://en.wikipedia.org/wiki/Magic_8-Ball
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances...
1. The demand curve for the Magic 8-Ball toy is P = 15 – 0.5Q. Frances currently has a patent on the concept of predictive billiards accessories, and is thus the only person able to sell Magic 8-Balls. For now, Frances is a monopoly. Her costs are C(QF) = 2QF a. Calculate the (monopoly) market price, quantity produced, and Frances’s profit.   b. Frances’s patent is about to expire, and another producer (Simon) is planning on entering the market. Simon has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT