JavaScript - Is There A Script For Counting A Number I Want Down To Another Number That Resets?
I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
Similar TutorialsI have a function below where every time a question is submitted, it will add a new row in the table with a textbox which allows numbers entry only. My question is that I don't know how to code these features in this function: 1: I want the text box to be between 0 and 100, so if text box contains a number which is above 100, it will automatically change the number to the maximum number which is 100. Does any one know how to code this in my function below in javascript: Code: function insertQuestion(form) { var row = document.createElement("tr"); var cell, input; cell = document.createElement("td"); cell.className = "weight"; input = document.createElement("input"); input.name = "weight_" + qnum; input.onkeypress = "return isNumberKey(event)"; cell.appendChild(input); row.appendChild(cell); } When I used toFixed() method on a number, I thought that this method round a number to a specified approximation, but I got a surprising result, the number became string! 15.23689 .toFixed ( 2 ) ==> "15.24" So does it convert the number into string? Hey there. Firstly, I am a total beginner to Javascript. I know basic HTML and some CSS, but Javascript seriously confuzzles me; even just basic stuff like calling functions. :S I've joint this forum in the hope of learning more about Javascript and how to use it properly, in terms of writing my own code, etc. My problem is this. I've come across this number-guessing script: Code: <script language="javascript" type="text/javascript"> var num = Math.floor(Math.random() * 100) + 1; var tries = 0; function Guess() { var g = document.form1.guess1.value; tries++; document.form1.tries.value = tries + "/10"; if (g < num) document.form1.hint.value = "Nope, try guessing higher."; if (g > num) document.form1.hint.value = "Nope, try guessing lower."; if (g == num) { window.alert("Correct! Congratulations, you guessed it in " + tries + " tries."); location.reload(); } if (tries == 10) { window.alert("Sorry, you've ran out of guesses! The number was " + num + "."); location.reload(); } } </script> ... which I love. I understand the basics of it. However, I want to try and improve on it. For example, I'd like to implement a Give Up button, so that when you press this button the script reveals the number for you instead (in an alert message) and when this alert message clears, the page is automatically reloaded/cleared and a new game begins. Suggestions, also, for how it can be improved and how I can go about doing it, are much appreciated. I know it may be a lot to ask, but I am seriously in need of help. Here is the HTML by the way: Code: <html> <body> <h1>Guess The Number</h1> <hr/> <p>I'm thinking of a number between 1 and 100. Try to guess it, but be warned - you only have 10 tries!</p> <form name="form1"> <b>Guess:</b> <input type="text" name="guess1" size="5" maxlength="3"/> <input type="button" value="Guess" onclick="Guess();"/> <br/> <input type="text" size="25" name="hint" disabled="disabled"/> <br/> <b>Tries Used:</b> <input type="text" size="4" name="tries" disabled="disabled"/> <br/> </form> </body> </html> Hello All, I am not too good with javascript so I apologise in advance! I have a website form where people input their contact details and I need a script which allows the form to be submitted if the telephone number is correct and returns an error if the number is invalid, (preferably in a pop-up box - not on another page) which says something like "The telephone number you have entered is invalid. Please enter a valid UK landline or mobile number". For the number to be valid, it has to either start with an "01", an "02" or an "07" and be either 10, or 11 digits long. The user should also be able to enter space without an error (ie, the script should ignore spaces). Any suggestions would be much appreciated. Regards, AC I am delving into the coding world and while I understand the basic principle of cookies, conditional statements, arrays, etc... I am still learning how to properly implement them. Any asistance with the following situation would be greatly appreciated and help with my learning as I try to reverse engineer the logic. I have looked around the web and this forum with little success. If the situation below is too complicated, I would really appreciate even a shove in the right direction regarding the logic. ----------------- How could I show a preset counter which counts up from a preset, beginning number toward a preset, end number? I imagine the increment and speed is set be the difference between the two numbers and a timeframe. Assumptions: I would rather not set the increment but edit the end number to show a steady increase. As I update that number, the increment adapts dynamically. I would want the number/script to be useful, so it should not refresh to the beginning number on each page load (i.e. num=0). When a visitor comes to the page, it must seem like the counter has been steadily been increasing in their absence. Coke or Pesi did something similar one time regarding cans sold to date (doubt it was plugged into a DB somewhere but rather based on a steady sales figure) and it was pretty cool. All the best! Hello, I need to be able to get a number to 2dp without rounding e.g. 14.756 needs to be 14.75 I tried .toFixed(2) but this rounds. I thought of converting to a string and performing string functions but it seems like over kill if there is an easier way. thanks I need to write something like this: http://justsheri.com/webscripting/wsassignment/1049950/ If you look at the code there is 52 of these <img src = './images/blank1.png' onClick='clickCard("qh",0)'/> where clickcard("numchanges", increments by 1) how is this done? Thanks Danny Hi All, I have a problem in javascript. When i am doing rounding of the value i am getting the o/p like 2.2e4. I have to display this value in decimal value(2.0002) not in exponential. Can any one please help me to resolve this issue.? Regards, VIjai I can generate a random number between 1 and 9, ( var rand_no = Math.floor( 1 + Math.random() * 9 ); ) but I want to add that random number to a value that is enterred into an inputbox and get a total. How can I do this using Javascript and put the result into a hidden field. I will also need to use that same random number in another place on another form . Thanks Hi All, Im a total novice with coding other than html/css. I have a video xml which, in which i need a new feature, could you help me with that? Basically my video is being entered with a time of it - for example 30sec. I have start and end trackers firing for the video but now i want to add impression trackers at 25%, 50%, 75%. I managed to do it using specifying seconds for the calls, but it becomes a pain to recalculate seconds for every video. Any ideas I could follow? Thanks, AD I have a dynamic form that uses Code: var input_box = $("<input type=\"text\" id=\"ac\" >") I need to have a unique id for each input that is displayed. How can I do this? I can't use vars or anything in there as variables don't work in this as it is? Ooook so this is probably the stupidest question ever. I know in java you can take advantage of a number being an int (not double) and divide by something without getting a remainder. But in javascript you dont declare what kind of variable something is. So my problem is: Given any double or triple digit number, how do i get all but the last digit. Like if i have 13, I need to make an int with 1. If it's 103 i need one with 10. Thanks!! I am using a JavaScript price calculator. Right now it does everything I need except for one thing. If you look at the "type" field, they can choose either woven or printed. Right now, if they choose "woven" it adds 6 cents to to the total as you can see in this portion of the code: if (theForm.select.value=="woven") { priceeach+=0.06; Rather than 0.06, how do I get it to add a percentage--say 20% The form is located he http://goo.gl/YlJWo Thank you! This is quite easy yet hard. I'm trying to apply something in my forums, where a member could post something let's say: "Hi, I am number [random]1, 6[/random]" And what would happen is that, a number from 1 to 6 would be the content, for example, it would be: "Hi, I am number 3" But if you refresh the page, it would still be the same number. Like it won't be randomized again. So how do I make a javascript/html code that would make a one time number randomizer. I need help on some code
Code: function makefarm(){ document.getElementById("farms").value ++ ; document.getElementById("pop").value -=2 ; document.getElementById("coin").value -=10 ; document.getElementById("lumber").value -=20 ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; document.getElementById("CIfarm").value ++ ; } function upgradefarm(){ document.getElementById("iron").value -=5 ; document.getElementById("coin").value -=20 ; document.getElementById("farmupgrades").value ++ ; } var food = function () { var f1 = document.getElementById('buttonhere'); var f2 = '<input type="button" value="Make Farm" onclick="makefarm()" /> Farms:<input type="text" id="farms" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <input type="button" value="Upgrade Farm" onclick="upgradefarm()" /> Farm Upgrades:<input type="text" id="farmupgrades" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/> <br/><br/><br/> Current Income:<input type="text" id="CIfarm" size="1" value=0 style="background-color:transparent;border:0px solid white;" READONLY/>'; f1.innerHTML = f2; } Code: Coin:<input type="text" id="coin" value=50 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Lumber:<input type="text" id="lumber" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Iron:<input type="text" id="iron" value=25 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> Food:<input type="text" id="food" value=100 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> POP:<input type="text" id="pop" value=10 size="1" style="background-color:transparent;border:0px solid white;" READONLY /> so if i press the makefarm() button it will add to the numbers but if i press it again they will got to zero how could i make it remember the numbers?(the input boxes are the second code) Hi. I'm trying to make a web page using javascript for validation, but after the last 2 functions I wrote, 2 functions don't work anymore. I attached the file so you can see for yourself. And I know there are a few things I could do to the html, but I'm saving those for last. Any idea why the scripts don't work is much appreciated. (the functions that don't work a "cont_pag1" and "cont_pag2"...btw...they worked just fine until I wrote the last few functions (the ones for the pag4 div))...And sorry for the really strange language...
Hi, I have a string stored in a variable var = "4.25 houses". I would like to only extract the currency value, ie "4.25" from this. Is this possible? Also I would like to divide and multiply the new variable I get... does this number need to be converted or something? Thanks in advance! How can i go about showing on screen a count from an initial value to an end value over a set time period. Initial value = 0 End value = 2000 Time = 25 seconds I cant find an example of this anywhere, so I'm trying here to see if somebody can help Hi, First, I'm a newbie in javascript and I need help to do something with a random numbers. I would like to put the result numbers in the "<td class="style1">#01</td>" section and an image in <td class="style1">img1</td> depending of the result number. Exemple : If the result number is #1 then the image Under the #01 will be an image associated to the number 1". Number 1 = "red circle.jpg", Number2 = "blue square.jpg", etc. Am I clear ? Here is my actual HTML canvas : -------------------------------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Result</title> <style type="text/css"> .a1{ position:relative; font-family:Verdana; font-size:20px; color:#888888; } .style1 { text-align: center; } </style> <script language="JavaScript"> /***************************************** * Lottery Picker (By Kurt at kurt.grigg@virgin.net) * Featured on/available at Dynamic Drive DHTML(dynamic html) & JavaScript code library * Modified by DynamicDrive.com for below config options * This notice must stay intact for use. *****************************************/ var totalnumbers=6 //input total numbers to generate var lowerbound=1 //input lower bound for each random number var upperbound=49 //input upper bound for each random number function lotto(){ B=' '; LottoNumbers=new Array(); for (i = 1; i <= totalnumbers; i++) { RandomNumber = Math.round(lowerbound+Math.random()*(upperbound-lowerbound)); for (j = 1; j <= totalnumbers; j) { if (RandomNumber == LottoNumbers[j]) { RandomNumber=Math.round(lowerbound+Math.random()*(upperbound-lowerbound)); j=0; } j++; } LottoNumbers[i]=RandomNumber; } LottoNumbers=LottoNumbers.toString(); X=LottoNumbers.split(','); for (i=0; i < X.length; i++) { X[i]=X[i]+' '; if (X[i].length==2) X[i]='0'+X[i]; } X=X.sort(); for (i=0; i < X.length; i++) { OutPut=B+=X[i]; } if (document.all)document.all.layer1.innerHTML=OutPut; if (document.getElementById)document.getElementById("layer1").innerHTML=OutPut; if (document.layers){ document.layers.layer1.document.open(); document.layers.layer1.document.write("<span style='position:absolute;top:0px;left:0px;font-family:Verdana;font-size:20px;color:#888888;text-align:center'> "+OutPut+"</span>"); document.layers.layer1.document.close(); } T=setTimeout('lotto()',20); //window.status=OutPut; } function StOp(){ setTimeout('clearTimeout(T)',1000); } //--> </script> </head> <body> <table border='0' width=250 style="height: 146px"> <tr valign='middle'> <td align='center'> <form name=form> <input type=button value='Lottery Number Picker' onClick="lotto();StOp()"> </form> <span id=layer1 class=a1>Result</span> </td> </tr> </table> <table style="width: 250px; height: 100px" cellspacing="0" cellpadding="0"> <tr> <td class="style1">#01</td> //result number 1 have to be here <td class="style1">#02</td> //result number 2have to be here <td class="style1">#03</td> //result number 3 have to be here <td class="style1">#04</td> //result number 4 have to be here <td class="style1">#05</td> //result number 5 have to be here <td class="style1">#05</td> //result number 6 have to be here <td style="height: 50px" class="style1">#06</td> </tr> <tr> <td class="style1">img1</td> //associated image of the result number 1 have to be here <td class="style1">img2</td> //associated image of the result number 2 have to be here <td class="style1">img3</td>//associated image of the result number 3 have to be here <td class="style1">img4</td>//associated image of the result number 4 have to be here <td class="style1">img5</td>//associated image of the result number 5 have to be here <td class="style1">img6</td>//associated image of the result number 6 have to be here </tr> </table> </body> </html> -------------------------------------- Thank you very much to helping me to understand how to do it !! Sylvano Reply With Quote 12-22-2014, 05:36 PM #2 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts This is hideously old and indeed crude code. document.all applied to IE4 and document.layers to Netscape 3. <script language="JavaScript"> and document.write() are also long obsolete. As is T=setTimeout('lotto()',20); I would suggest you get hold of a more up-to-date code. Simply rename your images 1.jpg, 2.jpg and so on, and display the appropriate one in its correct position. I'll give you a far better lottery number generator:- Code: <!DOCTYPE html> <head> </head> <body> <script type = "text/javascript"> function mylotto() { var rlts = document.getElementById("results"); rlts.value = ''; // clear results var srtrlts = document.getElementById("sortedresults"); srtrlts.value = ''; // clear results var hmany = document.getElementById("howmany").value; var max = parseInt(document.getElementById("maxnum").value); if (isNaN(max) || max <= hmany) { alert ("You must choose a number greater than " + hmany + " for the maximum number value!"); document.getElementById("maxnum").value = 49; return false; } var numArray = new Array(max); var chosen = new Array(hmany); var uniqueList = new Array(hmany); for (var i = 1; i <= max; i++) { // set up array 1 to max numArray[i-1] = i; } Array.prototype.shuffle = function() { var s = []; while (this.length) s.push(this.splice(Math.random() * this.length, 1)); while (s.length) this.push(s.pop()); return this; } numArray.shuffle(); // shuffle the array for (var i = 0; i < hmany; i++) { // take the first howmany items uniqueList[i] = numArray[i]; } rlts.value = uniqueList.join(" "); srtrlts.value = uniqueList.sort(sortNumber).join(" "); function sortNumber(a,b) { return a - b; } //alert ("The unsorted random numbers a - \n" + uniqueList) //alert ("The sorted unique random numbers a - \n" + uniqueList.sort(sortNumber)); } </script> <p>UNIQUE NUMBERS LOTTERY PICKER</p><br> <form> Select How Many Unique Numbers <select id="howmany"> <option value=1>1</option> <option value=2>2</option> <option value=3>3</option> <option value=4>4</option> <option value=5>5</option> <option value=6 selected>6</option> <option value=7>7</option> <option value=8>8</option> <option value=9>9</option> <option value=10>10</option> </select> <br> Enter the Maximum Number Value  <input type="text" id = "maxnum" value="49" size="2"><br><br> <input type="button" value="Here we go!" onclick="mylotto()"> <br><br> Unsorted Results:- <input type = "text" id = "results" size = "60"><br> Sorted Results:-     <input type = "text" id = "sortedresults" size = "60"> </form> </body> </html> Quizmaster: In which river did John the Baptist baptise Jesus? Contestant: The Amazon I'm having a hard time trying to get this script to work. I am trying to write a script that finds the highest of 5 numbers and places the highest number in the last textbox. Can anybody tell me what I am doing wrong? Code: <HTML> <HEAD> <TITLE>COMSC-100-1241 - Assignment #10 - Using "Greater Than" And "Less Than" Logic</TITLE> <SCRIPT TYPE="text/JavaScript"> function Go(code){ var number1 var number2 var number3 var number4 var number5 var highest var max number1 = parseFloat(document.getElementById("number1").value); number2 = parseFloat(document.getElementById("number2").value); number3 = parseFloat(document.getElementById("number3").value); number4 = parseFloat(document.getElementById("number4").value); number5 = parseFloat(document.getElementById("number5").value); max = parseFloat(document.getElementById("max").value); if (code == 0){ } if (max < number1){ max = number1 } if (max < number2){ max = number2 } if (max < number3){ max = number3 } if (max < number4){ max = number4 } if (max < number5){ max = number5 } document.getElementById("max").value = highest } </SCRIPT> </HEAD> <BODY> <TABLE BORDER="1" WIDTH="500" ALIGN="center"> <TR> <TD> <DIV ALIGN="left"> <B>Instructions: </B> Answer the three questions and press "GO". Your score will appear. <BR> <BR> <B>Input Values: </B> <BR> <B>First Number:</B> <INPUT id="number1"> <BR> <B>Second Number:</B> <INPUT id="number2"> <BR> <B>Third Number:</B> <INPUT id="number3"> <BR> <B>Fourth Number:</B> <INPUT id="number4"> <BR> <B>Fifth Number:</B> <INPUT id="number5"> <BR> <INPUT TYPE="Submit" VALUE="GO!" onClick="Go(0);"> <BR><BR> <B>Output Values: </B> <BR> <B>Highest Number:</B> <INPUT id="highest"> </FORM> </DIV> </TD> </TR> </TABLE> </BODY> </HTML> |