JavaScript - Need Help: Multiplication Test In Javascript
Hello,
I am trying to write a javascript page that shuffles an array of questions ( 1x1 , 1x2, 1x3 - 1x12), prints them, then has the user type in the answer, then checks them to make sure the user answer is correct. I have looked up a lot of source code for javascript "quiz" but they all are multiple choice answers and the questions are not shuffled. This is as far as I have gotten, but now I am not sure how to take the user input text and compare them to the answer / question array to make sure to check. Code: <html> <head> <title> Slick Math </title> <script type="text/javascript"> var quest = new Array; var ans = new Array; var questprint = new Array; quest[0] = "1 x 1 "; quest[1] = "1 x 2 "; quest[2] = "1 x 3 "; quest[3] = "1 x 4 "; quest[4] = "1 x 5 "; quest[5] = "1 x 6 "; quest[6] = "1 x 7 "; quest[7] = "1 x 8 "; quest[8] = "1 x 9 "; quest[9] = "1 x 10 "; quest[10] = "1 x 11 "; quest[11] = "1 x 12 "; questprint[0] = quest[0]+"<input type='text' name='q0'></input><br/>"; questprint[1] = quest[1]+"<input type='text' name='q1'></input><br/>"; questprint[2] = quest[2]+"<input type='text' name='q2'></input><br/>"; questprint[3] = quest[3]+"<input type='text' name='q3'></input><br/>"; questprint[4] = quest[4]+"<input type='text' name='q4'></input><br/>"; questprint[5] = quest[5]+"<input type='text' name='q5'></input><br/>"; questprint[6] = quest[6]+"<input type='text' name='q6'></input><br/>"; questprint[7] = quest[7]+"<input type='text' name='q7'></input><br/>"; questprint[8] = quest[8]+"<input type='text' name='q8'></input><br/>"; questprint[9] = quest[9]+"<input type='text' name='q9'></input><br/>"; questprint[10] = quest[10]+"<input type='text' name='q10'></input><br/>"; questprint[11] = quest[11]+"<input type='text' name='q11'></input><br/>"; var score = 0; ans[0] = 1; ans[1] = 2; ans[2] = 3; ans[3] = 4; ans[4] = 5; ans[5] = 6; ans[6] = 7; ans[7] = 8; ans[8] = 9; ans[9] = 10; ans[10] = 11; ans[11] = 12; function shuffle() { return (Math.round(Math.random())-.5); } questprint.sort(shuffle); </script> </head> <body> <h1>Slick Math</h1> <form name="form"> <script type="text/javascript"> document.write(questprint[0]); document.write(questprint[1]); document.write(questprint[2]); document.write(questprint[3]); document.write(questprint[4]); document.write(questprint[5]); document.write(questprint[6]); document.write(questprint[7]); document.write(questprint[8]); document.write(questprint[9]); document.write(questprint[10]); document.write(questprint[11]); </script> </form> </body> </html> Thanks =] Similar TutorialsI am trying to allow the user to type in two numbers and then a multiplication table pops up below it. It works however I cannot figure out how to make the first row be 0,1,2,3,4,5..ect and same for the first column in each row going down the table. Here is my code.. Code: <html> <head> <title>Untitled</title> <script> function f(x,y) { if((x < 3) || (y < 3) || (x > 10) || (y > 10)){ alert("Please enter numbers between 3 and 7") }else{ s = "<table border = '1'>" var total; for (i=0; i<=x; i++){ s+= "<tr></tr>"; for (j=0; j <= y; j++){ total = i * j; s+="<td>"+ total +"</td>" } } s+="</table>" document.getElementById("Q").innerHTML+=s } } </script> </head> <body> [enter a number between 3 and 10] <form id = "form1"> <input id = "a" value = "6"> by <input id = "b" value = "8"><br> <input type = "button" onclick = "f(a.value,b.value)" value = "Click here when ready!"> <br> </form> <div id = "Q"></div> </body> </html> Hi, I am having a hard time to display a table with user preference of how many rows/columns to create a multiplication table. I think I have to user DOM but I am not sure and I am not sure how to use it. My question is, how do I get the user input to use as a dynamic array input in a for loop so I can create the multiplication table using CSS ? Here is my code: [CODE] Hi, I am having a hard time to display a table with user preference of how many rows/columns to create a multiplication table. I think I have to user DOM but I am not sure and I am not sure how to use it. My question is, how do I get the user input to use as a dynamic array input in a for loop so I can create the multiplication table using CSS ? Here is my code: Code: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title> Multiplication table web page </title> <style type="text/css"> h2 {text-align: center;} table {position:absolute;left:300px;} </style> </head> <body> <h2>Multiplication Table</h2> <script type="text/javascript"> <!-- var size; do { size = prompt("What size table do you want?","10"); } while ((size < 1) || (size > 15)); document.write("<table border=\"5\" cellpadding=\"5\" cellspacing=\"5\">"); document.write("<tr><th> *</th>"); for(var i = 0; i < 10; i++) { for(var j = 0; j < 10; j++) { size = i * j; } } document.write("</table>"); // --> </script> </body> </html> Ok, just to be honest this is for my assignment from my University, I have completed the whole code myself I just can't seem to get just one little thing. Code: function GenerateTables(){ var start = document.tables.start.value; var end = document.tables.end.value; var size = document.tables.size.value; for (start; start <= end; start++) { document.write("<table width='100' align='center'>") document.write("<caption><b>Table of </b>" + start + "</caption><br>") document.write("</table><br>") for (var i = 1; i <= size; i++) { document.write("<table border=1 align='center'>") document.write("<tr height=40 align='center'>") document.write("<td width=40 align='center'>" + start + "</td>") document.write("<td width=40 align='center'> * </td>") document.write("<td width=40 align='center'>" + i + "</td>") document.write("<td width=40 align='center'> = </td>") document.write("<td width=40 align='center'>" + start * i + "</td>") document.write("</tr>") document.write("</table>") } } } The function generates multiplication tables depending on the input user provides using this interface (given below). And it generates them like this (given below). Now what I want is to generate those tables sideways not downwards. Not all the tables, but it should complete one table downwards, then start the new table on its side. I can't seem to figure out anything that would make that happen, and ideas would be appreciated, thanks. Regards, Papichoolo. I would like to create a quiz/test, and I would like it to be corrected by Javascript--where when the user clicks the submit button, it will show the user "You got number% correct." How can I do this?
I have the following regex pattern: var patt=/(&(?=(amp|apos|gt|lt|quot)|[^&])+/; It's validation to ensure that any ampersand sign is followed by amp;, apos;, gt;, lt;, or qout;. It should validate any other character except the aforementioned ampersand case. The regex checks out ok on all online regex testers. however, when i apply it to javascript via the test() method, things that return a fail in the testers return true with the js test() method, so the validation isn't working for me right now in javascript. e.g. var testStr = 'someString&'; //should fail var testStr2 = 'someString&'; //should pass however, calling patt.test(testStr); returns true when it should be returning false... Thanks for any input and help you all have to offer. I've been beating this to death for about 3 hours and I'm a little stuck on it.. I have javascript function which calculates the product of two numbers. function fnCalculate() { var lat1=price * num; } Here for eg if price is 19.50 and num is 1 then o/p should be 19.50 and not 19.5 as its resturning now. Also if price is 15.00 and num is 1 then o/p should be 15.00 and no just 15. Is it possible ... someone please help Hello, Could someone help me with this problem? I'd like the posted result to be a number with two decimals. I know how to make this normally but not with this kind of script. Thanks very much in advance! Code: <html> <head> <script type="text/javascript"> function calcResult(){ document.getElementById('result').innerHTML = ''; var num1 = newNumber(document.getElementById('txt1').value); var num2 = 0.429; var num3 = 1; if(isNaN(num1) || isNaN(num2)){ alert('Please enter a number'); } else { document.getElementById('result').innerHTML = num1 * num2; document.getElementById('entry').innerHTML = num1 * num3; } } window.onload=function(){ document.getElementById('btnCalc').onclick = calcResult; document.getElementById('btnCalc').onclick = calcEntry; } </script> </head> <body> <p> <input type="text" id="txt1" size="5" value="" maxlength="5" /> <button id="btnCalc">Calculate</button> </p> <p> <span id="entry"></span> × 0.429 = <span id="result"></span> </p> </body> </html> hello guys, I'm new here .. I am developing a fw js, now I have to test the selector $.. operation is similar to jquery $('#Id') $('table td .class) If there are several factors you should: $('table td .class).each(function () { this .. }); or $('table td .class).item(number) ... http://debbe.altervista.org/d&d.html can be used to test the methods set / get the attributes or setCSS or getCSS for styles .. Thanks to all Hi All, I am looking to make a "enemy" dissapear when "character" hits it? I don't really know what I'm doing. All help is appreciated! PHP Code: <!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>Untitled Document</title> <style> body { padding:0; margin:0; background:#000; } canvas { display:block; margin:30px auto 0; border:1px solid #ccc; background:#999; } </style> <script> var canvas, ctx, width = 320, height = 480, rightKey = false, leftKey = false, enemyTotal = 4, enemies = [], enemy_x = 50, enemy_y = 1, enemy_w = 32, enemy_h = 32, speed = 1, character_x = (width / 2) - 15, character_y = height - 65, character_w = 32, character_h = 32, level_1, level_1_bgX = 0, level_1_bgY = -1312; var enemy, character; for (var i = 0; i < enemyTotal; i++) { enemies.push([enemy_x, enemy_y, enemy_w, enemy_h, speed]); enemy_x += enemy_w + 30; } function drawEnemies() { for (var i = 0; i < enemies.length; i++) { ctx.drawImage(enemy, enemies[i][0], enemies[i][1]); } } function moveEnemies() { for (var i = 0; i < enemies.length; i++) { if (enemies[i][1] < height) { enemies[i][1] += enemies[i][4]; } else if (enemies[i][1] > height - 1) { enemies[i][1] = -45; } } } function clearCanvas() { ctx.clearRect(0,0,width,height); } function drawCharacter() { ctx.drawImage(character, character_x, character_y); if (rightKey) character_x += 5; else if (leftKey) character_x -= 5; if (character_x <= 0) character_x = 0; if ((character_x + character_w) >= width) character_x = width - character_w; } function drawlevel_1() { ctx.drawImage(level_1,level_1_bgX,level_1_bgY); if (level_1_bgY > 1792) { level_1_bgY = -1791; } level_1_bgY += 1.5; } function init() { canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); enemy = new Image(); enemy.src = 'character_1.gif'; character = new Image(); character.src = 'character_1.gif'; level_1 = new Image(); level_1.src = 'level_1.jpg'; setInterval(gameLoop, 25); document.addEventListener('keydown', keyDown, false); document.addEventListener('keyup', keyUp, false); } function gameLoop() { clearCanvas(); drawlevel_1(); moveEnemies(); drawEnemies(); drawCharacter(); } function keyDown(e) { if (e.keyCode == 39) rightKey = true; else if (e.keyCode == 37) leftKey = true; } function keyUp(e) { if (e.keyCode == 39) rightKey = false; else if (e.keyCode == 37) leftKey = false; } window.onload = init; </script> </head> <body> <canvas id="canvas" width="320" height="480"></canvas> </body> </html> Thanks I've gone far enough with my website and thanks to you guys i completed a lot of issues i had. I just included a browser detect snippet which is supposed to show the pages correctly on IE, Firefox, Safari, Opera and Chrome. I understand that watching a greek site won't make much sense for you, but i'm hoping you will at least understand how the thing goes. The first yellow box on your left is the menu. Clicking there will take you to the menu's categories. You will find a few categories in english there, at least Now, in each category, clicking the button below the product should put it in the cart, part of it appears on every page in a floating layer to the top-right. There is also a link in there to "checkout" (ΟΛΟΚΛΗΡΩΣΗ ΠΑΡΑΓΓΕΛΙΑΣ). This is where the problem comes: In every browser except Chrome, the list of items of the cart will appear. I don't know why but in Chrome, not only i see nothing in there, but it also deletes everything in the cart! If any of you, specially the ones who have many browsers installed, could spend a little time testing my site, i would really appreciate it. Old Pedant and Phillip should be more aware since you guys have already helped me out and you kinda know the functionality of the site. The address is: lukulos.gr (works directly now, no need to type specific files) Code: <html> <head> <script> function testemail(){ var fld= document.getElementById("input1").value; var reg = /^[\w+]@[\w+].com$/i; if(reg.test(fld)) { alert("email is ok"); } else { alert("email is not correct"); } } </script> </head> <body> <p> <form name="form1" onsubmit="testemail()"> enter email : <input id="input1" type="text" /> <input id="input2" type="submit" value="Submit" /> </form> </p> </body> </html> I am trying to test an email ID with above RegExp. I enter a correct email or not, I am always getting message "email is not correct" So RegExp is not working. How can I find out whats wrong with this RegExp ? Is there any tool to figure that out ? I tried with FireBug, but it does not point to whats wrong with RegExp Thanks Code: //Script to Rotate Images and Transition Effects var slidespeed=2000 var slideimages=new Array("1.jpg","2.jpg","3.jpg") var whichimage=0 var imgobj, filtersupport, blenddelay var imageholder=new Array() for (i=0;i<slideimages.length;i++){ //preload images imageholder[i]=new Image() imageholder[i].src=slideimages[i] } function slideit(){ if (filtersupport) imgobj.filters[0].apply() imgobj.src=imageholder[whichimage].src if (filtersupport) imgobj.filters[0].play() whichimage=(whichimage<slideimages.length-1)? whichimage+1 : 0 setTimeout("slideit()", slidespeed+blenddelay) } //End Image Rotation/Transistion What object can I test for instead of "filtersupport" so that I can have an else {} for non-ie browsers? I have Javascript on my site and will not work at all if JS is disabled. How do I notify my users that the pull down menus use javascript and will not work unless they turn it on? Can someone advise me how I could get this websocket to work. I'm working off a tutorial which gives this example. Code: var connection = new WebSocket('ws://html5rocks.websocket.org/echo', ['soap', 'xmpp']); // When the connection is open, send some data to the server connection.onopen = function () { connection.send('Ping'); // Send the message 'Ping' to the server }; // Log errors connection.onerror = function (error) { console.log('WebSocket Error ' + error); }; // Log messages from the server connection.onmessage = function (e) { console.log('Server: ' + e.data); }; This does work and I can write something similar can get the correct results as long as I am connecting to that address. I am not able to get the same results from my server and I cannot even get the sockets onopen function to run. Here is my JS Code- Code: socket = new WebSocket("ws://micahwilliamson.com:80/socket.php", ['soap', 'xmpp']); socket.onmessage = function(e) { alert(e.data); } socket.onopen = function() { alert("Success"); socket.send("test"); } socket.onerror = function(err) { alert(err); } Here is my php socket code. Code: <?php ini_set("display_errors", 1); $host = "localhost"; $port = 80; $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket"); $bind = socket_bind($socket, $host, $port) or ("Could not bind to socket"); $listen = socket_listen($socket, 3) or die("Could not listen to socket"); $spawn = socket_accept($socket) or die("Could not accept incoming connection"); $input = socket_read($spawn, 1024) or die("Could not read input"); $input = trim($input); $output = strrev($input); socket_write($spawn, $output, strlen($output)) or die("Could not write to socket"); socket_close($spawn); socket_close($socket); ?> It seems the socket cannot make a connection so it could be my php code or the file hosting server just wont allow it. I got a VPS- maybe that would work better? I am not a JAVA programmer. However I expected to find a similar template on the web. I couldn't. A list of checkbox options: 1. Water 2. Sand 3. Red paint Submit button which brings results IF Water and Sand selected, result = Mud IF Water and Red paint selected, result = Red water The closest thing I got to was http://www.javaworld.com/javaworld/j...orm_check.html How would code for such a program look like? Ideally I would want it to be really simple. Paste to notepad and save as html. When looping thru a group of elements and using getNamedItem('itemname').value there are some elements that are going to have a null value for that particular getNamedItem... the problem is that you cannot test this value to see if it's null because it generates an error instead, and the code does not continue: _allnames[_j].attributes.getNamedItem("onchange") is null You cannot even test it for null like this: if (_allnames[_j].attributes.getNamedItem("onchange").value != null) { do something } This will also generate an Error instead of testing to see if it is null. How do you test getNamedItem for null to avoid this error? EDIT: It appears that the problem is limited to Firefox. It works fine in IE7, but the error occurs in both FF v3.5.4 and in v3.5.5! Thanks In my example below, there are no elements that match, so you should see an alert box that says 'No match', but (using Firebug) you will see that an error is generated when it tries to find the value of onchange in the last input tag. Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title></title> <script type="text/javascript"> function myLoad() { //Add some text beside the Zip/Postal Code box var _match = false; var _cell = 0; var _allnames = document.getElementsByName('zip_code'); //Get all the TD tags var _len = _allnames.length; //How TD tags are there? for (_j=0; _j<_len; _j++) { //Loop thru each child _nodetype = _allnames[_j].nodeType; _name = _allnames[_j].attributes.getNamedItem('name').value; _onchange = _allnames[_j].attributes.getNamedItem('onchange').value; _type = _allnames[_j].attributes.getNamedItem('type').value; _size = _allnames[_j].attributes.getNamedItem('size').value; if (_nodetype == 1) { if ( (_name == 'zip_code') && ( _onchange == 'zip_changed();') && (_type == 'text') && (_size == '10') ) { //Look for these specific things to get the correct node (INPUT tag in this case) _match = true; alert('Match found'); var _old_node = _allnames[_j]; //This is the INPUT tag we were looking for var _new_node = document.createTextNode(' Enter NONE if this does not apply'); //This is the text we will add on the end of it _allnames[_j].parentNode.insertBefore(_new_node, _old_node.nextSibling); //insertBefore inserts before the element, but using targetElement.nextSibling inserts it after instead (insertAfter is deprecated)!! } } } if (_match != true) { alert('No match'); } } </script> </head> <body onLoad="myLoad();"> <table class="zipncountry" border="1"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </table> <br> <table border="3"> <tr> <td>4<input type="hidden" value="" ></td> <td>5</td> <td><input type="text" name="zip_code" value="123" size=11 maxlength=20 onChange="zip_changed();"><input type="hidden" name=zip_code value="321" maxlength=20 ></td> </tr> </table> <br> <table class="zipncountry" border="5"> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> </table> </body> </html> Hello! I am trying to validate name, email and comment inputs! I did the following! Code: $("#CmtSub").click(function(event){ var email = $("#CmtEmail").val(); var filter = /'^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$'/; if( $("#CmtName").val() == "" ){ $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ $("#CmtName").css({'background-position': '0 -90px'}, 0); } if(filter.test(email) && email != "") { $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ $("#CmtName").css({'background-position': '0 -90px'}, 0); } //just to debug console.log("the value and the email ("+ email +")"); if( $("#CmtTxtArea").val() == "" ){ $("#CmtTxtArea").css({'border': 'red 1px solid'}, 0); return event.preventDefault(event); }else{ $("#CmtTxtArea").css({'border': 'white 1px solid'}, 0); } // then proceed ! }); this code here isn't working! any help please ?: Code: if(filter.test(email) && email != "") { //passme = false; $("#CmtName").css({'background-position': '0 -45px'}, 0); return event.preventDefault(event); }else{ //passme = true; $("#CmtName").css({'background-position': '0 -90px'}, 0); } help is greatly appreciated ! Hello, I am trying to complete a test site but adding some javascript. In short I am trying to make the main content div slide from behind the nav via some JS. http://www.squareflash.com/test/index.html First I am just trying to get the animate onClick to work. I found this practice site -- Using this I am trying to get some guidance on how to add a automatic stop function to the moveRight variable. http://www.tutorialspoint.com/cgi-bi...=javascript_45 The site is in html5, is there anything special I need to know for that? ty Hey all, I am doing an image/data rotating script. I get the data from a XML. I parse that data and build the divs and such. Data Set 1 --- background image in a div, with a button and some text overlayed on the div. ... ... Data Set 4 -- etc.. The thing is, I load the script and the div appears, BUT the text overlay appears first THEN the image pops up for the first Data Set. I can't have that. I need them both to appear at once. Is there a way to determine that a div background image is loaded? -- I have access to the prototype js library. thanks. Code: solved ! |