JavaScript - Why Is My Javascript Homework Correct?
Hello coding forum users,
I am working on the only javascript chapter of my HTML textbook. It asks me to: Quote: replace the line <img id="sky" src="sky0.jpg" alt=""/> with a script element that writes the following HTML code: <img id='sky' src='mapNum.jpg' alt=' ' /> where mapNum is the value of the mapNum variable. so I try PHP Code: <script type="text/javascript" /> document.write("<img id='sky' src='mapNum.jpg' alt=' ' />") </script> after a few other attempts I search codingforums and find: PHP Code: document.write("<img id=\"sky\" src='sky"+mapNum+".jpg' alt='' />"); and PHP Code: document.write('<img id="sky" src="sky'+mapNum+'.jpg" alt="" />'); and these work perfectly fine but I don't understand why? my textbook only has one javascript chapter and its really small and vague, and my teacher hasn't answered my emails, so I would be grateful if anyone can tell my why my first code falls flat. Similar TutorialsHello everybody, could anyone offer an insight into why this isn't work please? It should be a simple project I was given at college but it stops near the end. The task is to ask a user for a word using a form, then spell it out backwards — stupid, but, hey — it's a lesson! Here's the code: Thanks. <h2>JavaScript exercise 4</h2> <p>Ask the user for a string of text. This must then be displayed in reverse. (For example, if the user enters the words "Hello World!" an alert dialog box should appear with "!dlroW olleH") </p> <form action="javascript: displaymessage();" method="get"> <p>Please enter a word: <input id="user_word" name="user_wrd" type="text"> <input type="submit" value="Submit"> </p> </form> <script> function displaymessage() { /*Set a variable for the user's word*/ var word_forward; /*Get the user's word from the from above and put it into the variable*/ word_forward = document.getElementById("user_word").value; /*Write the user's word from the variable to the page*/ document.write("<p>The word is: " + word_forward + ".</p>"); /*Calculate the length of the user's word*/ var word_length=word_forward.length; /*Write the length of the user's word to the page*/ document.write("<p>The word length is: " + word_length + " characters.</p>"); /*Subtract 1 from word length to allow for character index numbers starting from zero*/ word_length2=(word_length-1); /*Set a variable for the character index number*/ var char_num; /*Begin character index number countdown loop: loop start value / loop end value / increment*/ for(char_num=word_length2; char_num=0; char_num--) { /*Write word backwards using charAt*/ document.write("The word spelled backwards is: " + word_forward.charAt(char_num)); } } </script> Hello, i have been trying to make a small one digit calculator, but i am having a lot of trouble with it..This is what i have so far. It will run each function individually, but if i try with all four sign functions, addition will only work....What am i doing wrong here? <!-- saved from url=(0014)about:internet --> <HTML> <HEAD> <TITLE>Page Title</TITLE> <SCRIPT language="JavaScript"> <!-- var n1 = -1; //1st operand of the expression -1 indicates n1 is empty var n2; //2st operand of the expression var startNew = true; var result; var operator; function readNumber(value) { if (n1 < 0) //n1 is empty { n1 = value; } else //n1 is not empty { n2 = value; } } function addition() { result = n1 + n2; //always deal with addition } function subtraction() { result = n1 - n2; //always deal with subtraction } function multiplication() { result = n1 * n2; //always deal with multiplication } function division() { result = n1 / n2; //always deal with division } function calculate_display() { //calculate the answer if (operator == '+') addition(); startNew = window.confirm(n1 + '+' + n2 + '=' + result + '\n' + ' start a new expression? '); if(startNew == true) //if ok is clicked { //clear n1 and n2 n1 = -1; n2 = -1; } { return; } if (operator == '-') subtraction(); startNew = window.confirm(n1 + '-' + n2 + '=' + result + '\n' + ' start a new expression? '); if(startNew == true) //if ok is clicked { //clear n1 and n2 n1 = -1; n2 = -1; } { return; } if (operator == '*') multiplication(); startNew = window.confirm(n1 + '*' + n2 + '=' + result + '\n' + ' start a new expression? '); if(startNew == true) //if ok is clicked { //clear n1 and n2 n1 = -1; n2 = -1; } { return; } if (operator == '-') division(); startNew = window.confirm(n1 + '/' + n2 + '=' + result + '\n' + ' start a new expression? '); if(startNew == true) //if ok is clicked { //clear n1 and n2 n1 = -1; n2 = -1; } { return; } } //--> </SCRIPT> </HEAD> <BODY> <form> <input type = "button" value="0" onclick="readNumber(0);"> <input type = "button" value="1" onclick="readNumber(1);"> <input type = "button" value="2" onclick="readNumber(2);"> <input type = "button" value="3" onclick="readNumber(3);"> <input type = "button" value="4" onclick="readNumber(4);"> <BR> <input type = "button" value="5" onclick="readNumber(5);"> <input type = "button" value="6" onclick="readNumber(6);"> <input type = "button" value="7" onclick="readNumber(7);"> <input type = "button" value="8" onclick="readNumber(8);"> <input type = "button" value="9" onclick="readNumber(9);"> <BR> <input type = "button" value="+" onclick="operator = '+';"> <input type = "button" value="-" onclick="operator = '-';"> <input type = "button" value="*" onclick="operator = '*';"> <input type = "button" value="/" onclick="operator = '/';"> <input type = "button" value="=" onclick="calculate_display();"> </form> I found and am using the following script to add checkbox values to a textarea. Code: <script type="text/javascript"> function add_sub(el){ if (el.checked) el.form.elements['type'].value+=el.value; else{ var re=new RegExp('(.*)'+el.value+'(.*)$'); el.form.elements['type'].value=el.form.elements['type'].value.replace(re,'$1$2'); } } </script> </head> <body> <form name="form1" method=post> <textarea name="type" rows="5" cols="35" onclick="this.focus();this.select();"> </textarea><br> <input type="checkbox" name="bob" id="bob" value="<p>" onclick="add_sub(this);"><label for="bob"><p></label><br> <input type="checkbox" name="bob1" id="bob1" value="<span>" onclick="add_sub(this);"><label for="bob1"><span></label><br> <input type="checkbox" name="bob2" id="bob2" value="<div>" onclick="add_sub(this);"><label for="bob2"><div></label> That is working. However, I wanted to put carriage returns after each checkbox value added, so I added the + "\r\n" as follows: Code: <script type="text/javascript"> function add_sub(el){ if (el.checked) el.form.elements['type'].value+=el.value + "\r\n"; else{ var re=new RegExp('(.*)'+el.value+'(.*)$'); el.form.elements['type'].value=el.form.elements['type'].value.replace(re,'$1$2'); } } </script> That is working when adding the value, but removing the value when the checkbox is unchecked is not working. I know the regular expression, etc. needs to be updated to account for the carriage returns, but I have tried everything I can think of and cannot get it to work. Help correcting the code is appreciated. Hey guys, I am doing a website and the way it is suppose to work is when you click on a link there is a little triangle beside it, so when you click on it the triangle is suppose to face down but more links appear when you click on it. Anyways here is the code if someone could please tell me what im doing wrong? HTML Code: <ul class="menu" id="subnavmenu"> <li><a href="#" onClick="showHide('sublist1');return false;" id="subheading">About Us</a> <ul id="sublist1" style="display:none;"> <li><a href="#">Visit Us</a></li> <li><a href="#">Contact Us</a></li> <li><a href="#">Archives Unboxed and Revealed</a></li> </li> </ul> CSS Code: #subnavmenu ul.menu { display: block; overflow:hidden; } JAVASCRIPT Code: function showHide(element_id) { if (document.getElementById && document.getElementById(element_id) && document.getElementById(element_id).style) { var menu = document.getElementById(element_id); var arrow = document.getElementById(element_id + '_arrow'); if (menu.style.display == "block") { menu.style.display = "none"; if (arrow.src) { arrow.src = arrow.src.replace("down","right"); } } else { menu.style.display = "block"; if (arrow.src) { arrow.src = arrow.src.replace("right","down"); } } } } Whats wrong with the code below? I'm trying to get the email service provider name. Code: <html> <head> <script type="text/javascript"> function GetProvider { var fullemail = openinviter.email_box.value; var afterat = fullemail.split("@"); var Provider = afterat[1].split("."); var showit = Provider[0]; openinviter.provider_box.value=showit; } </script> </head> <body> <form action="" method="post" name="openinviter"> <input type="text" name="email_box" onchange="GetProvider()" /> <input type="text" name="provider_box" value=""> </form> </body> </html> Thanks alot! Sorry if this is the wrong forum for this question. I get confused about what forum to post a question in with regards to DHTML technologies since they are all frequently used together. Anyhoo, heres my question. I've recently discovered an interesting behavior. When I mouse over a nested element it triggers the 'onmouseout' event handler of the parent element. Take the following code snippet, for instance: Code: <table> <tr> <td onmouseout="window.alert('you moused out of td');"> <img src="image0.gif" style="height:50px; width:50px;" /> </td> </tr> </table> In the code snippet above, the 'onmouseout' event handler executes when you mouse over the image nested inside the <td> element. I wouldn't have thought this would be the appropriate behavior since from my perspective the mouse is still inside the <td> element. Can someone make comments on this. Thank You. Edit: I've discovered that mousing over a nested element causes both an 'onmouseout' and an 'onmouseover' event for the parent element. They are called back to back. Seems a little bit of an odd sequence of events but maybe it makes sense in the grand scheme of things. Yeah, right. Box 1 - Box 2 / Box 3 and i put a round off number so the decimals dont give a million numbers the problem is...the math is wrong now.... someone please help me fix this.. <!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> <title>Calc</title> <script type="text/javascript"> function calculate() { var one = document.getElementById("one").value*1; var two = document.getElementById("two").value*1; var three = document.getElementById("three").value*1; document.getElementById("answer").value=((one-two)/three); ; } </script> </head> <body> Enter Number 1 <br> <br><input type="text" id="one" /><hr>Enter Number 2<br><br><input type="text" id="two" /><br><br><hr>Enter Average number<br><br><input type="text" id="three" /><br><br><button onclick="calculate();">Calculate</button> <br id="result"></center></br> Total <input type="text" readonly="readonly" id="answer" /><br><br></center> </body> </html> </td> </tr> </table> </center> </body> </html> i am dynamically making pdf files, works great using abcpdf etc etc. the only problem that i am having is with the page breaking. i want to create a function that runs onload and can read the position of elements and add breaks before the element if it is cut off. each block ("from the west", "holiday inn express") is a span with the class name of "entry" i want to loop through those, get their height, add them together and if that span falls in the range of the page size (792px) add a page break before it. the difficult part will be when we get to pages 2 and above does that make sense? im throwing around some ideas now in a js file, if you have any please post them thanks Hi, i have a complete validation code here which seems not working properly.When i filled the full name filed and i click submit, the form get submitted but when i filled the full name and filled email too and click on submit the third or fourth like country alert or helpmessage pop up. Now i am trying to fish out why it is doing that but i am not getting it now. please i know its a long code but please try to help me on where i am wrong. I will thank you for using blue thank Please i will suggest copy the code i see what i mean.Here is it Code: <html> <head> <title>Final form validation</title> <script type="text/javascript"> function formvalidator() { var Fullname=document.getElementById("Fullname"); var email=document.getElementById("email"); var addr=document.getElementById("addr"); var country=document.getElementById("country"); var zip=document.getElementById("zip"); var phone=document.getElementById("phone"); var educa=document.getElementById("educa"); var job=document.getElementById("job"); var hours=document.getElementById("hours"); if(Checkfullname(Fullname,"Please enter your full name.")){ if(Checkemail(email,"Please enter a valid email.")){ if(Checkaddr(addr,"Please enter your address for better contact.")){ if(Checkcountry(country,"Please select a country.")){ if(Checkzip(zip,"Please enter your area zip code of 5 digits.")){ if(Checkph(phone,"Please enter your phone number for better contact of 14 digits.")){ if(Checkeduca(educa,"Please select your education status.")){ if(Checkjob(job,"Please select job.")){ if(Checkhours(hours,"Please select the number of hours you want to work." )){ return true; }}}}}}}}} return false; } function Checkfullname(elem,helpmg) { var eval=elem.value; eval=eval.replace(/[^a-z\s\-\.\']/gi,""); eval=eval.replace(/^\s+|\s+$/g,""); eval=eval.replace(/\s{2,}/g," "); eval=eval.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()}); document.getElementById("Fullname").value=eval; if(eval.length>=5 && eval.length<=50){ return true; } else{ alert(helpmg); elem.focus(); return false; }} function Checkemail(elem,help) { var eReExp=/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; if(elem.value.match(eReExp)){ return true; } else{ alert(helpmg); elem.focus(); return false; }} function Checkaddr(elem,helpmg) { var eval=elem.value; eval=eval.replace(/^\s+|\s+$/g,""); eval=eval.replace(/\s{2,}/g," "); document.getElementById("addr").value=eval; if(elem.value.replace(elem.value)){ return true; } else{ alert(helpmg); elem.focus(); return false; }} function Checkcountry(elem,helpmg) { if(elem.value=="Please select country"){ alert(helpmg); elem.focus(); return false; } return true; } function Checkzip(elem,helpmg) { var zReExp=/^\d{5}$/; if(elem.value.match(zReExp)){ return true; } else{ alert(helpmg); elem.focus(); return false; }} function Checkph(elem,helpmg) { var pReExp=/^\d{14}$/; if(elem.value.match(pReExp)){ return true; } else{ alert(helpmg); elem.focus(); return false; }} function Checkeduca(elem,helpmg) { if(elem.value=="Please choose"){ alert(helpmg); elem.focus(); return false; } return true; } function Checkjob(elem,helpmg) { if(elem.value=="Please select job"){ alert(helpmg); elem.focus(); return false; } return true; } function Checkhours(elem,helpmg) { if(elem.value=="Please select hours"){ alert(helpmg); elem.focus(); return false; } return true; } </script> <style type="text/css"> #fullpage{width:100%;background-color:#f3f3f3; border:1px solid #336699;} #firstbar{ width:100%; background-color:#336699; text-align:left; font-color:white; } #field{ margin-right:70%;text-align:right;} </style> </head> <body> <h2><font color=#336699>Form validation..The power of javascript<font></h2> <p><font clor=#336699>Please note:all the field marked asteric is required and must be field.<br/>For help on filling the form just contact as at support@jobs.com.<font></p> <hr width="100%" color="#336699" size="2"> <div id="fullpage"> <form onsubmit="return formvalidator()"/> <div id="firstbar"><p><font color="white">Personal Details</font></p></div> <div id="field"> <p><font color=red>*</font>Full Name<input type="text" id="Fullname"/></p> <p><font color=red>*</font>Email<input type="text" id="email"/></p> <p><font color=red>*</font>Contact Address<input type="text" id="addr"/></p> <p><font color=red>*</font>Country<select id="country"/> <option>Please select country</option> <option>Ghana</option> <option>United States</option> <option>India</option> <option>Germany</option> <option>Italy</option> <option>Nigeria</option> <option>South Africa</option> <option>United kingdom</option> <option>Malasia</option> <option>Egypt</option> <option>France</option> <option>China</option> </select></p> <p><font color=red>*</font>Zip Code<input type="text" id="zip"/></p> <p><font color=red>*</font>Phone Number<input type="text" id="phone"/></p> <p>Fax Number<input type="text" name="fax"/></p> </div> <div id="firstbar"><p><font color="white">Educational Details & Job</font></p></div> <div id="field"> <p><font color=red>*</font>Education Status<select id="educa"/> <option>Please choose</option> <option>High School</option> <option>Diploma</option> <option>Degree</option> <option>Certified We Developer</option> <option>Certified We Designer</option> <option>others</option> </select></p> <p>Experience(Details if any)<input type="text" name="exp"/></p> <p><font color=red>*</font>Job Type<select id="job"/> <option>Please select job</option> <option>We Developer</option> <option>Web Designer</option> <option>Softwar Developer</option> <option>IT Consultancy</option> <option>Stock Trader</option> <option>Marketing Position</option> </select></p> <p><font color=red>*</font>Working Hours<select id="hours"/> <option>Please select hours</option> <option>1 to 5hurs</option> <option>1 to 8hurs</option> <option>1 to 10hurs</option> <option>1 to 12hurs</option> <option>1 to 13hurs</option> <option>1 to 15hurs</option> <option>1 to 20hurs</option> </select><p/> <p>Salary Demanded<input type="text" name="sala"/></p> <p>Comment(if any)<textarea name="text" rows="3" cols="40" wrap="virtual"/></textarea></p> <input type="submit" value="Submit Form"/> <input type="reset" value="Reset Form"/> </div></div> </form> </body> </html> Thanks.Clement Osei. hi I need some help with a project I am working on. I spent time with my instructor but I lost something in translation. I know this is really basic but I really can't see what is wrong. we are trying to open employee payroll records and if the employee works 35hrs or less they get straight pay. anything over 35 is time and a half. I though my calculations were correct but if you calculate the first four records, which I wll post after the code, they are wrong. here is my code, Code: //constants var THIRTYFIVE = 35; var ONE_POINT_FIVE = 1.5; var ZERO = 0; //global variables var employeeNumber; var employeeName; var hourlyWage; var hoursWorked; var grossPay; var partOne; var partTwo; var totalForEmployee; var counter; var records; partOne = ZERO; partTwo = ZERO; totalForEmployee = 0; function calculateGrossPay() { while (records.readNextRecord()) { employeeNumber = records.getEmployeeNumber(); employeeName = records.getEmployeeName(); hourlyWage = records.getEmployeeHourlyWage(); hoursWorked = records.getEmployeeHoursWorked(); if (hoursWorked <= THIRTYFIVE) { partOne = hoursWorked * hourlyWage; } else { partOne = (hourlyWage * THIRTYFIVE); partTwo = ((hoursWorked - THIRTYFIVE) * hourlyWage) * ONE_POINT_FIVE; } totalForEmployee = partOne + partTwo; document.write(employeeNumber + "\t" + "\t" + employeeName + "\t" + totalForEmployee + "<br />"); } } /* this function prints the top row of the table */ function printHeader() { document.write("Employee" + "\t" + "Employee" + "\t" + "Gross" + "\t" + "\t" + "Withholding" + "\t" + "Net" + "\t" + "<br />" + "Number" + "\t" + "\t" + "Name" + "\t" + "\t" + "Pay" + "\t" + "\t" + "Amount" + "\t" + "\t" + "Pay" + "<br />" + "<br />"); } /* this function opens employee payroll records */ function initializeEmployeePayrollRecords() { records = openEmployeePayrollRecords(); } /* Design a program that will process the weekly employee time cards for all the employees of an organization. Each employee time card will have the these data items: an employee number, an employee name, an hourly wage rate, and the number hours worked during the week. Each employee is to be paid time-and-a-half for all hours worked over 35. A tax amount of 15% of gross salary is to be withheld. The output to the screen should display the employee's number and name, gross pay, withholding amount, and net pay. At the end of the report, display the total gross payroll amount, the total net payroll amount, and the average net pay paid per employee. */ function project4Part2() { // Your code goes in here. initializeEmployeePayrollRecords(); printHeader(); calculateGrossPay(); } this is the output I am getting Employee Employee Gross Withholding Net Number Name Pay Amount Pay 101 Joe Coyne 581.25 102 Fred Hensen 716.25 103 Ethel Roselle 1250 104 Barney Curry 1425 this is from the dataset, the first number is the wage, the second is the hours worked. 101 Joe Coyne 15 37.5 102 Fred Hensen 20 33 103 Ethel Roselle 25 45 104 Barney Curry 30 35 I would really appreciate any help with this, I am sure the answer is really simple but I am past the point of frustration. I need desperate help with a homework! Here's the task: First of all I need to define an array of 3 elements. Each element is associated with a different first name. Then I shall realize a first function to add a name in the table. The function takes as parameter the table previously created and return it containing an additional element whose value is the name that the visitor wants to add. I must use the dialog box prompt so that the user can specify the name he wishes to add in the table. I shall also perform a second function which will display the name that is included in the different array elements. This function will also take the table in parameters and proceed with setting the display name with a dialog box alert type that contains all of the name separated by ";". Here's what I've done so far: HTML Code: Code: <html> <head> <title> Task #4 </title> </head> <body> <script language="javascript"> function addName() { var names=new Array(3); names[0]="John"; names[1]="Patrik"; names[2]="Jane"; document.open(); document.write(names[3].join()); document.close(); if(names==new Array); { prompt("What name do you want to add?"+""); } return addName } </script> <noscript> <h1> You're browser doesn't support Javascript </h1> </noscript> </body> </html> I don't really understand the second part of the task, but the first part is what I've written but it doesn't seem to do anything when I try it on my browser. What am I doing wrong? Thank you for your patience. -A Javascript newbie I have no clue where to start. I have to put a pop up box on the screen and have the user enter a number under 100 and tell them how many quarters dimes nickles and pennies that would be. PLEASE HELP I am stumped
I am Having trouble getting this function to work. I am suppose to add javascript to a page to access an external js file named random.js to generate 5 random numbers when the page loads. it isnt doing it so far. Here is what i have. <html> <head> <script type="text/javascript" src="random.js"></script> <script type="text/javascript"> function showImg(o.jpg-9.jpg){ /* The showImg() function displays a random image from the 0.jpg through 9.jpg files. The random image is designed to thwart hackers attempting to endter the library database by requiring visual confirmation. */ var imgNumber= randomInteger(9);// Return a random number from 0 to 9. document.write("<img src='"+imgNumber+".jpg' alt='"+imgNumber+".jpg'/>"); } </script> </head> (<tr> <td colspan="2" class="center"> <script type= "text/javascript"> showlmg(); showlmg(); showlmg(); showlmg(); showlmg(); </script> </td> </tr> I have a homework page with three schools , four classes, and 4 weeks. Thing is I can't get the assignments to come up in the assignments text area. I can if I only have two if...else conditions, and I have tried three with nesting, but it always comes up undefined. This is for a class assignment but I really need help. I commented out the codes that didn't work. Sometimes i didn't finish the whole code before testing so is that maybe why it didn't work? Must it be complete to work? Thanks in advance for any help. I put the whole page in. I am very new to this, and i just can't get my head around it, so any help would be apreciated. It worked if all assignments as long as the assignments were the same for each class of each school. Daisey 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>functions</title> <script language="javascript"> function test2(form){ /*create a variable*/ var varSchool = form.ddlSchool.value var varClass = form.ddlClass.value var varWeek = form.ddlWeek.value var varResults /*create an if statement to determine school*/ if (varSchool==0){ varSchool = "Prairie High School's "; } else if (varSchool==1){ varSchool = "Prairie Middle School's "; } else if (varSchool==2){ varSchool = "Prairie Elementary School's "; } if (varSchool==0) { if ((varClass==0) && (varWeek==0)){ varResults = "000 this code works"; }else if ((varClass==0) && (varWeek==1)){ varResults = "001 this code works"; }else if ((varClass==0) && (varWeek==2)){ varResults = "002 this code works"; }else if ((varClass==0) && (varWeek==3)){ varResults = "003 this code works"; }else if ((varClass==1) && (varWeek==0)){ varResults = "010 this code works"; }else if ((varClass==1) && (varWeek==1)){ varResults = "011 this code works"; }else if ((varClass==1) && (varWeek==2)){ varResults = "012 this code works"; }else if ((varClass==1) && (varWeek==3)){ varResults = "013 this code works"; }else if ((varClass==2) && (varWeek==0)){ varResults = "020 this code works"; }else if ((varClass==2) && (varWeek==1)){ varResults = "021 this code works"; }else if ((varClass==2) && (varWeek==2)){ varResults = "022 this code works"; }else if ((varClass==2) && (varWeek==3)){ varResults = "023 this code works"; }else if ((varClass==3) && (varWeek==0)){ varResults = "030 this code works"; }else if ((varClass==3) && (varWeek==1)){ varResults = "031 this code works"; }else if ((varClass==3) && (varWeek==2)){ varResults = "032 this code works"; }else if ((varClass==3) && (varWeek==3)){ varResults = "033 this code works"; } } else if (varSchool==1) { if ((varClass==0) && (varWeek==0)){ varResults = "100 this code works"; }else if ((varClass==0) && (varWeek==1)){ varResults = "101 this code works"; }else if ((varClass==0) && (varWeek==2)){ varResults = "102 this code works"; }else if ((varClass==0) && (varWeek==3)){ varResults = "103 this code works"; }else if ((varClass==1) && (varWeek==0)){ varResults = "110 this code works"; }else if ((varClass==1) && (varWeek==1)){ varResults = "111 this code works"; }else if ((varClass==1) && (varWeek==2)){ varResults = "112 this code works"; }else if ((varClass==1) && (varWeek==3)){ varResults = "113 this code works"; }else if ((varClass==2) && (varWeek==0)){ varResults = "120 this code works"; }else if ((varClass==2) && (varWeek==1)){ varResults = "121 this code works"; }else if ((varClass==2) && (varWeek==2)){ varResults = "122 this code works"; }else if ((varClass==2) && (varWeek==3)){ varResults = "123 this code works"; }else if ((varClass==3) && (varWeek==0)){ varResults = "130 this code works"; }else if ((varClass==3) && (varWeek==1)){ varResults = "131 this code works"; }else if ((varClass==3) && (varWeek==2)){ varResults = "132 this code works"; }else if ((varClass==3) && (varWeek==3)){ varResults = "133 this code works"; } } else (varSchool==2) { if ((varClass==0) && (varWeek==0)){ varResults = "200 this code works"; }else if ((varClass==0) && (varWeek==1)){ varResults = "201 this code works"; }else if ((varClass==0) && (varWeek==2)){ varResults = "202 this code works"; }else if ((varClass==0) && (varWeek==3)){ varResults = "203 this code works"; }else if ((varClass==1) && (varWeek==0)){ varResults = "210 this code works"; }else if ((varClass==1) && (varWeek==1)){ varResults = "211 this code works"; }else if ((varClass==1) && (varWeek==2)){ varResults = "212 this code works"; }else if ((varClass==1) && (varWeek==3)){ varResults = "213 this code works"; }else if ((varClass==2) && (varWeek==0)){ varResults = "220 this code works"; }else if ((varClass==2) && (varWeek==1)){ varResults = "221 this code works"; }else if ((varClass==2) && (varWeek==2)){ varResults = "222 this code works"; }else if ((varClass==2) && (varWeek==3)){ varResults = "223 this code works"; }else if ((varClass==3) && (varWeek==0)){ varResults = "230 this code works"; }else if ((varClass==3) && (varWeek==1)){ varResults = "231 this code works"; }else if ((varClass==3) && (varWeek==2)){ varResults = "232 this code works"; }else if ((varClass==3) && (varWeek==3)){ varResults = "233 this code works"; } } /* make a conditional assignment variable with two conditions class and week*/ /*var varscClass if ((varSchool == 0) && (varClass == 0)){ varscClass == 0 }else if ((varSchool == 0) && (varClass == 1)){ varscClass == 1 }else if ((varSchool == 0) && (varClass == 2)){ varscClass == 2 }else if ((varSchool == 0) && (varClass == 3)){ varscClass == 3 }else if ((varSchool == 1) && (varClass == 0)){ varscClass == 4 }else if ((varSchool == 1) && (varClass == 1)){ varscClass == 5 }else if ((varSchool == 1) && (varClass == 2)){ varscClass == 6 }else if ((varSchool == 1) && (varClass == 3)){ varscClass == 7 }else if ((varSchool == 2) && ( varClass == 0)){ varscClass == 8 }else if ((varSchool == 2) && ( varClass == 1)){ varscClass == 9 }else if ((varSchool == 2) && ( varClass == 2)){ varscClass == 10 }else if ((varSchool == 2) && ( varClass == 3)){ varscClass == 11 }*/ /*if ((varscClass == 0) && (varWeek == 0)){ classWeekassign = "week 1 of reading is read Chapter 1." }else if ((varscClass == 0) && (varWeek == 1)){ classWeekassign = "week 2 of reading is complete the Chapter 1 Review, page 20." }else if ((varscClass == 0) && (varWeek == 2)){ classWeekassign = "week 3 of reading is read Chapter 2." }else if ((varscClass == 0) && (varWeek == 3)){ classWeekassign = "week 4 of reading is complete the Chapter 3 Review, page 40." }else if ((varscClass == 1) && (varWeek == 0)){ classWeekassign = "week 1 of math is complete the Chapter 3 Review, page 40." }else if ((varscClass == 1) && (varWeek == 1)){ classWeekassign = "week 2 of math is complete the Chapter 3 Review, page 40." }else if ((varscClass == 1) && (varWeek == 2)){ classWeekassign = "week 3 of math is complete the Chapter 3 Review, page 40." }else if ((varscClass == 1) && (varWeek == 3)){ classWeekassign = "week 4 of math is complete the Chapter 3 Review, page 40." }*/ /*do a switch to determine favorite color*/ switch(varClass){ case "0": varResults = varSchool + "reading assignment for "; break; case "1": varResults = varSchool + "math assignment for "; break; case "2": varResults = varSchool + "science assignment for "; break; case "3": varResults = varSchool + "english assignment for "; } var classWeekassign /*another attempt failed if (varSchool==0) { if (varClass==0) { if (varWeek==0){ classWeekassign = "week 1 is to read Chapter 1." } else if(varWeek!=0){ } } else if((varClass==0) && (varWeek==1)){ classWeekassign = "week 1 of reading is do the Chapter 1 Review, page 20." } else if((varClass==0) && (varWeek==2)){ classWeekassign = "week 3 of reading is read Chapter 2." } else if((varClass==0) && (varWeek==3)){ classWeekassign = "week 4 of reading is do the Chapter 2 Review, page 40." } } else if(varSchool==1){ } */ /*Concatenate a string*/ varResults = varResults + classWeekassign; /*Write to the page*/ form.txtResults.value = varResults + classWeekassign; } </script> </head> <body> <form id="myform" action="" method="get"> <table> <tr> <td colspan="2"><p> <select name="ddlSchool"> <option value="0">Prairie High School</option> <option value="1">Prairie Middle School</option> <option value="2">Prairie Elementary School</option> </select> <select name="ddlClass"> <option value="0">Reading</option> <option value="1">Math</option> <option value="2">Science</option> <option value="3">English</option> </select> <br /> </p></td> </tr> <tr> <td width="223"> <select name="ddlWeek"> <option value="0">Week 1</option> <option value="1">Week 2</option> <option value="2">Week 3</option> <option value="3">Week 4</option> </select></td> <td width="147"><input name="btnSubmit" type="button" value="Submit" onclick="test2(this.form)" /></td> </tr> <tr> <td colspan="2"><textarea name="txtResults" cols="50" rows="3"></textarea> </td> </tr> </table> </form> </body> </html> Hello all, I’ve got a homework to do and I'm stuck, as you can see i have done all the function's (i think they are ok but not shure) with a main program I'm stuck don’t know even where to begin :/ could anyone please please help a bit? best regards <HTML> <HEAD> <TITLE> The game of 21 </TITLE> <SCRIPT LANGUAGE = "JavaScript"> //FUNCTIONS /* *removes a given number of sticks * *function takes one argument numberToRemove that represents the number of sticks to be removed * if numberToRemove is more than numberOfSticks, reduces numberOfSticks to 0 * otherwise reduces numberOfSticks by numberToRemove *function returns no value */ function removeSticks(numberToRemove) { if (numberOfSticks< numberToRemove) { numberOfSticks =0 } else { (numberOfSticks = numberOfSticks - numberToRemove); } } /* *switches current player * *function takes no argument * if current player is 1, sets current player to 2 * otherwise sets current player to 1 *function returns no value */ function switchCurrentPlayer() { if (currentPlayer ==1) { return currentPlayer = 2; } { return currentPlayer = 1; } } /* *gets a number from a prompt dialogue * *function takes no argument * prompts the current player for a number of sticks to remove, with a suitable message *function returns the number the player enters */ function getChosenNumber() { ChosenNumber = window.prompt('Player ' + currentPlayer + ': Please enter either 1, 2 OR 3.'); return ChosenNumber; } //VARIABLES -- do not change this section var numberOfSticks = 21;//number of matchsticks, initially 21 var currentPlayer = 1;//player whose turn it is, Player 1 goes first var numberChosen;//variable used to hold the number the current player chooses //MAIN PROGRAM The players take turns to choose how many matches to remove, as long as there are still matches left. After each go the program outputs the number of matches that still remain and if this number is zero a message is displayed to say the current player is the winner. The turn then passes to the other player. </SCRIPT> </HEAD> <BODY> </BODY> </HTML> I need help with trying to figure out what exactly needs to be done with both of these homework assignments. Nothing we covered in the textbook or examples provided go along with these directions so I am at a lost. For one assignment we need to: Write a JavaScript script within a web page that will use prompt/alert dialog boxes to calculate an average value for a list of numbers that could be any size. Use a while loop that will prompt a user for a number. As long as the number is not zero or negative, continue to add the numbers up and count the total number of values entered. Once the user enters a zero or negative value, terminate the loop and calculate the average. Report this final value via an alert box. For the other assignment we need to: Write a JavaScript script that first asks the user to enter an integer value from 1..15. Validate this with an error checking that will not allow them to continue until they enter a proper number. After the input is validated, use this number to dynamically build an HTML table using a JavaScript loop with one row and including a number of columns matching the user input. Can someone please help me out on how I should go about coding for these two assignments, I am at a loss with these assignments. Hey, I'm taking a javascript class and have to do an assignment in which I'm supposed to show countdowns to various events. I have 2 files, an external js file and a html file. But I can't get it to work I've searched the forum and googled it but all the solutions I've found do not work. Here's the code. .js file: Code: /* New Perspectives on JavaScript, 2nd Edition Tutorial 2 Review Assignment Author: Date: Function List: showDateTime(time) Returns the date in a text string formatted as: mm/dd/yyyy at hh:mm:ss am changeYear(today, holiday) Changes the year value of the holiday object to point to the next year if it has already occurred in the present year countdown(stop, start) Displays the time between the stop and start date objects in the text format: dd days, hh hrs, mm mins, ss secs */ function showDateTime(time) { var date = time.getDate(); var month = time.getMonth()+1; var year = time.getFullYear(); var second = time.getSeconds(); var minute = time.getMinutes(); var hour = time.getHours(); ampm = (hour < 12) ? " a.m." : " p.m."; hour = (hour > 12) ? hour - 12 : hour; hour = (hour == 0) ? 12 : hour; minute = minute < 10 ? "0"+minute : minute; second = second < 10 ? "0"+second : second; return month+"/"+date +"/"+year+" at "+hour+":"+minute+":"+second+ampm; } function changeYear (today, holiday) { var year = changeYear.getFullYear(); holiday.setFullYear(year); year = (today > holiday) ? year + 1 : year; year = holiday.setFullYear(year); } function countdown(start, stop) { time = stop - start; seconds = Math.floor(time/1000); minutes = Math.floor(seconds/60); hours = Math.floor(minutes/60); days = Math.floor(hours/24); return days + " days," + hours + " hours," + minutes + " mins," + seconds + " secs"; } And here's the HTML file: Code: <?xml version="1.0" encoding="UTF-8" ?> <!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> <!-- New Perspectives on JavaScript, 2nd Edition Tutorial 2 Review Assignment Events in Tulsa Author: Date: Filename: events.htm Supporting files: dates.js, logo.jpg, tulsa.css --> <title>Upcoming Events at Tulsa</title> <link href="tulsa.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="dates.js"></script> <script type="text/javascript"> showCountdown(){ var today = new Date("October 31, 2010 10:00:00"); var Date1 = new Date("January 14, 2011 10:00:00"); var Date2 = new Date("May 21, 2011 12:00:00"); var Date3 = new Date("July 4, 2011 9:00:00"); var Date4 = new Date("September 1, 2011 12:00:00"); var Date5 = new Date("December 1, 2011 11:30:00"); var Date6 = new Date("December 31, 2011 3:30:00"); document.eventform.thisDay.value = showDateTime(today); changeYear(today, Date1); changeYear(today, Date2); changeYear(today, Date3); changeYear(today, Date4); changeYear(today, Date5); changeYear(today, Date6); document.eventform.count1.value = countdown(today, Date1); document.eventform.count2.value = countdown(today, Date2); document.eventform.count3.value = countdown(today, Date3); document.eventform.count4.value = countdown(today, Date4); document.eventform.count5.value = countdown(today, Date5); document.eventform.count6.value = countdown(today, Date6); } </script> </head> <body onload="setInterval('showCountdown()', 100)"> <form name="eventform" id="eventform" action=""> <div id="logo"> <img src="logo.jpg" alt="Tulsa Events" /> </div> <div id="links"> <a href="#">Home</a> <a href="#">City Services</a> <a href="#">City Agencies</a> <a href="#">Mayor's Office</a> <a href="#">News Today</a> <a href="#">Upcoming Events</a> <a href="#">Site Map</a> <a href="#">Search Engine</a> <a href="#">Public Notices</a> <a href="#">Survey Form</a> <a href="#">Contact Us</a> <a href="#">E-Government</a> </div> <div id="main"> <h3>Countdown to Upcoming Events</h3> <table> <tr> <th colspan="2" style="text-align: right">Current Date and Time</th> <td><input name="thisDay" id="thisDay" readonly="readonly" size="40" /></td> </tr> <tr> <th>Event</th> <th>Starting Time</th> <th>Countdown to Event</th> </tr> <tr> <td><input value="Heritage Day" readonly="readonly" size="20" /></td> <td><input value="Jan 14 at 10:00 a.m." readonly="readonly" size="20" /></td> <td><input name="count1" id="count1" size="40" /></td> </tr> <tr> <td><input value="Spring Day Rally" readonly="readonly" size="20" /></td> <td><input value="May 21 at 12:00 p.m." readonly="readonly" size="20" /></td> <td><input name="count2" id="count2" size="40" /></td> </tr> <tr> <td><input value="July 4th Fireworks" readonly="readonly" size="20" /></td> <td><input value="Jul 4 at 9:00 p.m." readonly="readonly" size="20" /></td> <td><input name="count3" id="count3" size="40" /></td> </tr> <tr> <td><input value="Summer Bash" readonly="readonly" size="20" /></td> <td><input value="Sep 1 at 12:00 p.m." readonly="readonly" size="20" /></td> <td><input name="count4" id="count4" size="40" /></td> </tr><tr> <td><input value="Holiday Party" readonly="readonly" size="20" /></td> <td><input value="Dec 1 at 11:30 a.m." readonly="readonly" size="20" /></td> <td><input name="count5" id="count5" size="40" /></td> </tr> <tr> <td><input value="New Year's Bash" readonly="readonly" size="20" /></td> <td><input value="Dec 31 at 3:30 p.m." readonly="readonly" size="20" /></td> <td><input name="count6" id="count6" size="40" /></td> </tr> </table> </div> </form> </body> </html> Thanks for your help, I appreciate it I am taking an online class and the instructor is no help whatsoever. Please see where I'm going wrong with this. I'm sure it is something small I'm missing. Thanks. The assignment is: Create a function named emLink() that writes an email address using text strings of the username and server names entered in reverse order as parameter values. The command to call the function would look as follows: emLink("eman","revres"). Call the stringReverse() function with name as the parameter value to reverse the order of characters. Code: <html> <head> <script type="text/javascript"> function emLink(name,server) { var rname="eman"; var rserver="revres"; stringReverse(rname); stringReverse(rserver); var email="eman@revres"; var mailText='<a href="mailto:eman@revres">"eman@revres"</a>'; document.write(mailText); } function stringReverse(textString) { if (!textString) return ''; var revString=''; for (i = textString.length-1; i>=0; i--) revString+=textString.charAt(i) return revString; } </script> </head> <body> <script type="text/javascript"> var rname="reldac"; var rserver="ude.ortsa.uwm"; emLink(rname,rserver); </script> </body> </html> Currently, all it returns it : "eman@revres" |