I have been trying for days to get this to work. I have the basic code but every time I try to get the last requirement added, I break the code. I'm sure I am missing a lot and that's why I can't add the alert box with the count of displays. So it asks the users name, the number of times they want to see and alert box and if they enter invalid answers they see alert boxes telling them to enter a correct value. The last part that I am stuck on is the value the user enters determines how many alert boxes show their name and the box needs to say which box it is. So, <Name> this is time number <count of displays> of <total time to display>. I was trying different things with limiting and FOR , with the user Y variable being the limiter. Any clues or help would be greatly appreciated.
<!DOCTYPE html>
<html>
<body>
<p>Please enter your name followed by how many times you would like to be alerted.</p>
<button onclick="myFunction()">Start</button>
<script>
function myFunction() {
var x;
var name = prompt("Please enter your name", "");
if (name == null || name == "") {
alert("Please input a name.");
return false;
}
else {
var y;
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
if (y > 10) {
alert("Please input a number between 1 and 10.")
var y = prompt("Please enter a number between 1-10");
}
if (y <= 0) {
alert("Please input a number great than zero.")
var y = prompt("Please enter a number between 1-10");
}
}
}
</script>
</body>
</html>
This gets rid of most of your alert statements and keeps prompt'ing until a valid value is submitted.
function myFunction() {
var x,y,name = "";
while(name.length < 1) {
name = prompt("Please enter your name", "");
}
while(!((y > 0) && (y <11))) {
y = prompt("Please enter a number between 1-10");
}
for(x = 0; x < y; x++) {
alert(name);
}
}
Working example:
http://jsfiddle.net/DwxmT/
Not much to change.
Try:
function myFunction() {
var x, y;
var name = prompt("Please enter your name", "");
if (name == null || name == "") {
alert("Please input a name.");
return false;
}
else {
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
while (y >= 10 || y <= 0) {
alert("Please input a number between 1 and 10.")
var y = prompt("Please enter a number between 1-10");
if (y == null || y == "") {
alert("Please input a number for the times to alert the name.");
return false;
}
}
}
for(var i = 0; i < y; i++) {
alert("this is an alert");
}
}
This checks for 1 <= y <= 10 in a loop.
See http://jsfiddle.net/tMXNM/2/
Related
try to add function in JS where alert box will show message Password or Username must be more than 6 :
Password :
Password must have atleast 6 characters
Username :
Username must have atleast 6 characters
So far I got function show alert if Pass or Username is NULL
JaveScript Code :
function validateForm() {
var x = document.forms["myForm"]["namapengguna"].value;
if (x == "" || x == null) {
alert("Nama must be filled out");
return false;
}
var x = document.forms["myForm"]["username"].value;
if (x == "" || x == null) {
alert("User must be filled out");
return false;
}
var x = document.forms["myForm"]["password"].value;
if (x == "" || x == null) {
alert("Pass must be filled out");
return false;
}
var x = document.forms["myForm"]["confirm_password"].value;
if (x == "" || x == null) {
alert("Confirm Pass must be filled out");
return false;
}
else {
alert('Application Has Been Registered');
location.assign("homeA.php");
}
}
but I couldn't figure out how can add alert message if name or password less than 6.
Any idea or solution are really appreatiate.
p/s: if my question is not good enough for you to understand, please tell me so I can improve it.
You can check string length by doing x.length. So:
if (x.length <= 6) {
// Do what you want
}
You may also want to make sure the user hasn't inputted any spaces at the start or end of the field.
how to tell javascript that the user enter a string in the prompt box and work with it in an if statment ? If the user enters a string of letters I want it to alert "You did not enter a number", and if they entered a string if digits then continue with the logic.
var userGess = prompt("guess a number");
var secretNumber = 7;
if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
} else if (userGess == toString(userGess)) {
alert("you didnt type a number")
}
You can use isNaN(userGess) to check if a given string userGess is non-numeric.
However, that returns false if userGess is empty string, so you have to explicitly check it. So your final condition becomes
userGess === "" || isNaN(userGess)
var userGess = prompt("guess a number");
var secretNumber = 7;
if(userGess === "" || isNaN(userGess)) {
alert("You didn't enter a number")
} else if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
}
I am making a little 'guess the number' game, and I need to find a way to repeat the function based on what it outputs. What I have right now is this:
var input = prompt("Please enter a number between 1 and 100!");
var ranNum = Math.floor(Math.random()*100);
function numCheck()
{
if (input < 1)
alert("Please enter a valid number!");
else if (input > 100)
alert("Please enter a valid number!");
else if (input > ranNum)
alert("Try a little lower!");
else if (input = typeof stringValue)
alert("That is not a number!");
else if (input < ranNum)
alert("Try a little higher!");
else
alert("You got it!");
}
numCheck();
I did this using the inspect option on chrome on the page about:blank. Now that I have the function, I think I need to use another if else statement. It would detect if numCheck() output is You got it!" and if it didn't, it would replay the numCheck function, all the way until the player got the correct number.
How would I do it like that? And if there is a simpler way, what is it?
I made a fiddle that will use alerts and prompt accessible inside the page (I am not sure this what you asked for or not - explain more if not):
HTML:
<div class="numberguess">
<input type="text" id="entry" value="" />
<input type="button" id="send" class="" value="submit" />
</div>
<div id="alert"></div>
JS:
var _alert = document.getElementById('alert');
var ranNum = Math.floor(Math.random()*100);
function numCheck(){
var input = document.getElementById('entry').value;
if (input < 1 && input > 100)
_alert.innerText = "Please enter a valid number!";
else if (input > ranNum)
_alert.innerText = "Try a little lower!";
else if (typeof input != 'string')
_alert.innerText = "That is not a number!";
else if (input < ranNum)
_alert.innerText = "Try a little higher!";
else
_alert.innerText = "You got it!";
}
numCheck();
var el = document.getElementById('send');
el.addEventListener('click', function(){
numCheck();
}, false);
Check this as live exp: Jsfiddle
you can use looping like this code below
var input; // var to hold user input
var ranNum = Math.floor(Math.random()*100);
var win = false; // var if we want to ask another input
var trycount = 0; // don't want to ask for input forever
function numCheck()
{
if (input*1 != input) // check if number first
alert("That is not a number!");
else if (input < 1) // check lower bound
alert("Please enter a valid number!");
else if (input > 100) // check upper bound
alert("Please enter a valid number!");
else if (input > ranNum) // tell a hint
alert("Try a little lower!");
else if (input < ranNum) // tell a hint
alert("Try a little higher!");
else{
alert("You got it!");
win = true; // end the user prompt
}
}
while (!win && trycount < 3){ // while wrong guesses and below 3 tries
input = prompt("Please enter a number between 1 and 100! (" + ranNum + ")");
trycount++; // count the try
numCheck();
}
You need a looping mechanism that will keep prompting the user to input another value.
var ranNum = Math.floor(Math.random()*100);
function numCheck()
{
var input = prompt("Please enter a number between 1 and 100!");
if (input === null)
return false; // user pressed "cancel"
if (input == ranNum) {
alert("You got it!");
return false;
}
if (input < 1)
alert("Please enter a valid number!");
else if (input > 100)
alert("Please enter a valid number!");
else if (input > ranNum)
alert("Try a little lower!");
else if (input < ranNum)
alert("Try a little higher!");
else
alert("That is not a number!");
return true; // continue looping
}
while(numCheck());
var noun;
var verb;
var adverb;
var adjective;
var pronoun;
var questions = 5; //this number can adjust
var paragraph;
var noun = prompt("Type Noun");
if (isNaN(noun) == "True"){
questions -= 1;
verb = prompt("Type Verb");
}else{
alert("You entered a number, please enter a Noun.");
}
if (isNaN(verb) == "True"){
questions -= 1;
adverb = prompt("Type Adverb");
}else{
alert("You entered a number, please enter a Verb.");
}
if (isNaN(adverb) == "True"){
questions -= 1;
adjective = prompt("Type Adjective");
}else{
alert("You entered a number, please enter a Adverb.");
}
if (isNaN(pronoun) == "True"){
questions -= 1;
}else{
alert("You entered a number, please enter a Pronoun.");
}
So the main idea is to have the user input a word.
For some reason it marks the boolean false and goes directly into the else statement.....
This is because isNaN("some text") returns a boolean true/false -- and true == "True" returns false. Your statements should read:
if (isNaN(noun) === true) {
}
....
I am trying to figure out a simple way to limit the amount of attempts on a field(s) through a while loop in javascript. Just doesn't seem to want to loop but the validation is still working. Here is the code I was trying to use:
function validateForm() {
x=document.forms["Form"]["name"].value;
var i=0;
var sum=0;
do {
sum += i;
i++;
}
while (i < 3)
if (x==null || x=="") {
alert("First name must be filled out");
return false;
}
}
Thanks in advance for any ideas.
You will have to store the variable outside of the function:
var numOfTries = 0;
function validateForm() {
if(++numOfTries > 3) {
alert('You have reached the maximum number of tries!');
location.replace("error_page.html"); // do something about it
}
x = document.forms["Form"]["name"].value;
if (x == null || x == "") {
alert("First name must be filled out");
return false;
}