Question

In: Computer Science

Assume user will enter letters or numbers that are out of range. When user inputs invalid...

Assume user will enter letters or numbers that are out of range. When user inputs invalid values, show an alert message and ask user to enter a valid value again. Validate first and then proceed with the program. isNaN() method may be useful. Must validate user input.

1. Suggested Filename: fortune.html & fortune.js
Write a program that simulates a fortune cookie. The program should display one of five unique fortunes, depending on user input. The user must enter a number between 1 and 5 only.

2. Suggested Filename: number.html & number.js

Implement “Guess my Number” game. Here is a link to the game

https://www.funbrain.com/games/guess-the-number

Implement the basic game where you tell the user if their guess is higher or lower than the random number. You are welcome to add other constraints such as the number of chances a user gets.

Generating a random number between 1 and 100 using the random method:

var num = Math.floor((Math.random() * 100) + 1);

Remember, any user input is a string- you must convert it to an integer or a float before comparing it to other numbers.

Solutions

Expert Solution

1) fortune.html

<!DOCTYPE html>
<html>
<head>
   <title>Fortune Cookie</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="fortune.js">
</script>
</head>
<body>

<h2>Fortune Cookie</h2>
<form onSubmit="formvalidation();" id="fortuneform">
   Enter any number from 1 to 5
   <input type="text" name="fortuneNum">
<input type="submit" value="Submit">

</form>

  
</body>

</html>

fortune.js

function formvalidation()
{
var inpval = parseInt(document.getElementById("fortuneform").elements.namedItem("fortuneNum").value);
   if(!isNaN(inpval) && inpval>=1 && inpval<=5)
   {
   var inpval =parseInt(document.getElementById("fortuneform").elements.namedItem("fortuneNum").value);
switch(inpval)
{
case 1 : alert("Will have lucky day");
   break;
     
case 2 : alert("Bonus salary");
   break;

case 3 : alert("Promotion in job ");
   break;

   case 4 : alert("Meet old friend");
   break;      
     
case 5 : alert("New love life");
   break;
}
          
   }
   else
   {
   alert("Invalid Input! Please enter again")
  
   }  
}  

2). number.html

<!DOCTYPE html>
<html>
<head>
   <title>Guess Number</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="number.js">
</script>
</head>
<body>

<h2>Guess number</h2>
<form onSubmit="guessnum();" id="guessnumform">
   Enter any number from 1 to 100
   <input type="text" name="guessnum">
<input type="submit" value="Submit">

</form>

  
</body>

</html>

number.js

function guessnum()
{
var inpval = parseInt(document.getElementById("guessnumform").elements.namedItem("guessnum").value);
var lucknum = Math.floor(Math.random() *100) + 1;

if(inpval>lucknum)
{
alert("your number greater then lucky number");
}
else if(inpval<lucknum)
{
alert("your number less then lucky number");
}
else
{
alert("your number is equal to luc number");
}
}


Related Solutions

In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
Using Java, design a program to let user enter two lists of numbers within the range...
Using Java, design a program to let user enter two lists of numbers within the range [0, 9] from keyboard, you could either use flag to denote end of the list or ask user to enter the list size first and then enter the list numbers. Each of these two lists represents a set (keep in mind that duplicate elements are not allowed in the same set but are allowed between sets), so we have two sets A and B....
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
The program will first prompt the user for a range of numbers. Then the game will...
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the...
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Design a modular program which asks the user to enter a list of numbers. The numbers...
Design a modular program which asks the user to enter a list of numbers. The numbers must be stored in an array. The program then finds the index of the first occurrence of the smallest element in the array and the last occurrence of the largest element in the array. The program displays the position and value of each of these items.
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT