JavaScript - Number-guesser Script?
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> Similar TutorialsI 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?
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 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? 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! Hi All, I have two scripts which I want to try and integrate. I am using a nice gallery script to show thumbnails which are appended to a an image wrapper which on click of the thumbnail shows the larger image in the image wrapper, I am trying to implement cloud zoom which is a plugin which uses image srcs to then point to an anchor href to show another larger zoom image either in the same place.. which is what I am trying to do or in another div beside. I have had to set me img srcs up in a certain way to easily enter some product details. and I need to try an manipulate the code to make it work to suit my file layout. I am using a var= images [ with a series of file locations and info such as below { src: 'romanticabride/thumbs/tn_Shauna.jpg', srcBig: 'romanticabride/images/Shauna.jpg', title: 'Shauna', longDescription: '<b><H1>Shauna</H1></b><br><b>Romantica Of Devon <br><br><h2>Sizes Available:</h2><br> 6 - 32.<b><br><b><br><b><b><b><H2>Colours Available:</h2><b><br>Various<br>Please Enquire Below<br><br><br><br><a href="mailto:tracy@cherishbridal.co.uk?subject=Web Enquiry Regarding Romantica Shauna Bridal Gown"class="enquiry rose glow" >Click To Enquire About This Item </a>' }, what I need is for cloud zoom to work when the main image wrapper is hovered over which means it will need to add a class or when the whichever srcBig: is hovered over it gets wrapped by href to make the script work . one of my pages is http://www.cherishbridal.co.uk/romaticabride.html the cloud zoom script is at http://www.professorcloud.com/mainsite/cloud-zoom.htm.. I am happy to share a jsfiddle with someone or explain further or post some code. Thank you in advance I have a script that works in seamonkey(my html editor) but when I use it in IE8 it says errors happen. Here's the code (the first line is on line 7 of the html file): Code: <script type="text/javascript"> function enlarge(imageNum) { var numToString = ""; if(parseInt(imageNum) < 10){ numToString = "0" + imageNum; } else { numToString = imageNum + ""; } window.open("images/LgScreenshot"+numToString+".jpg","Screenshot "+imageNum,"status=0,height=675,width=900,resizable=0"); } </script> And the errors: Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB0.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; InfoPath.3; .NET CLR 3.0.30729) Timestamp: Wed, 10 Feb 2010 14:58:16 UTC Message: Object expected Line: 150 Char: 1 Code: 0 URI: http://samssc2site.co.cc/Features.html Message: Invalid argument. Line: 18 Char: 1 Code: 0 URI: http://samssc2site.co.cc/Features.html does any expert know how to pass parameters in the <script ..> tag? for instance; Code: <script type="text/javascript" src="script.js ?param1=val1¶m2=val2&etc "> in the javascript script.js, how would we read the params after the question mark? for example, google this; google shopping cart /v2_2/cart.js I need to assign a key in the javascript to actually make the javascript work,. I have a bookmark in chrome , a javascript , which actually works when clicked on it .,. but how can i edit it so that i can actually make it work on click a key or combination of keys. i want to declare the key or keycombo in the script itself .,. the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,., So I want it to work it this way ., select the text press a key and it opens a new tab (or window) with an xact search .,. i want to declare the key or keycombo in the script itself .,. the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,., So I want it to work it this way ., select the text press a key and it opens a new tab (or window) with an xact search .,. Thanks in advance ., Nani 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 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 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) 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! 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 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. 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? 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! 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 |