JavaScript - Need Some Help With Functions (homework)
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> Similar TutorialsI 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> 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 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> 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. 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. Hello 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> 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 Hello, this is a homework project which kind of works but isn't perfect. My tutor said the result must be an alert but I ended up with a document.write instead. He said it was fine but I'd like to know how to make it alert the result if anyone can help. FWIW I tried making the contents of the for loop into a variable without success. Code: <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> <script type="text/javascript"> function displaymessage() { /*Set a variable for the user's word*/ var word_forward; /*Get the user's word from the form above and put it into the new 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 variable 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; /*Write the first part of the result line*/ document.write('The word spelled backwards is: '); /*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(word_forward.charAt(char_num) + " "); } } </script> 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" Hi, I have written a number of functions designed to return frequency data on 1000 randomly chosen numbers using different math functions for the rounding. I would like to include all of these functions within the wrapper of another function so that only one call is needed to get returns from all of the 'inner' functions. However, while each of the functions works in isolation, the moment I wrap them in another function they stop working. The following code is one of the functions 'frequencyWrapperOne' that has been wrapped in the function 'testWrapper'. A call to testWrapper does nothing. Can anyone see what I'm doing wrong? Code: function testWrapper() { function frequencyWrapperOne() { var numberArrayOne = new Array(0,0,0); for (var i = 0; i < 1000; i = i + 1) { var chosenNumber = Math.floor(Math.random() * 3); if (chosenNumber == 0) { numberArrayOne[0] = numberArrayOne[0] + 1; } if (chosenNumber == 1) { numberArrayOne[1] = numberArrayOne[1] + 1; } if (chosenNumber == 2) { numberArrayOne[2] = numberArrayOne[2] + 1; } } return window.alert(numberArrayOne.join(',')); } } testWrapper(); Thanks. Ok here is what I have so far, my ending part of my Call Function I think is ok. for my main function. I think I misplaced the { and } I will show you all the codes I have for my main function this is what I have I think I did misplace something but I can not pin point where that one small things should be Code: unction showResults(race,name,party,votes) { // script element to display the results of a particular race var totalV = totalVotes(votes); document.write("<h2>" + race + "</h2>"); document.write("<table cellspacing='0'>"); document.write("<tr>"); document.write("<th>Candidate</th>"); document.write("<th class ='num'>Votes</th>"); document.write("<th class='num'>%</th>"); document.write("</tr>"); } for (var i=0; i < name.length; i++) { document.write("<tr>"); document.write("<td>" name[i] + "(" + party[i] + ")</td>"); document.write("td class='num'>" + votes[i] + "</td>"); var percent=calcPercent(votes[i], totalV) document.write("<td class='num'>(" + percent +"%)</td>"); createBar(party[i],percent) document.write("</tr>"); } document.write("</table>"); } </script> Just wondering if i misplaced any ; or { or } anywhere suggestions? Here is my call function Code: <script type="text/javascript"> showResults(race[0],name1,party1,votes1); showResults(race[1],name2,party2,votes2); showResults(race[2],name3,party3,votes3); showResults(race[3],name4,party4,votes4); showResults(race[4],name5,party5,votes5); </script> I been going over this, I can not seem to figure out what { i might be missing? Thanks Is there a way to activate a function from another function? It has to be in the script tag, it can't be in the HTML section. Can I use something similar to this following code? If not, can anyone give me some help? I have tried to do it various ways, and have looked it up a few times, but to no avail. Can I use something similar to this following code? If not, can anyone give me some help? if (condition) {activate functionname();} Any help I can get would be appreciated. Thanks a lot to anyone who can help. im no jQuery rookie but for the first time ...instead of simply writing out the animation jQuery('element').hide() in that way.... i instead needed to keep track of the functions so i wrote it like function hidetheElement() { jQuery('element').hide() } but now i need to run 2 of those functions together after a click ...for example jQuery('ClickedElement').click( movethelement,hidethelement ) see what i mean?....i dont know how to properly run two defined functions whats the proper syntax.... and here is my actual code for reference (its basically a menu link being clicked...hiding one page...and showing another....i needed to define the functions so if a menu link is clicked it could hide every other page first...by function....and then call a function to show the page Code: jQuery(document).ready( function() { jQuery('#ladiesmenu').click( previewoff,ladieson ); function previewoff() { jQuery('#preview').fadeOut(500); } function ladieson() { jQuery('#ladies').fadeIn(500); } I'm a newbie student coder for now, hoping to enter the wonderful world of software engineering in another year. I put together a very simple site for a class and all is well, but my "Contact SRS" form doesn't seem to want to run my JavaScript code even though I've used a JS debugger which returned no errors. When I click the Submit button it's supposed to validate the form. But I keep getting "Error on page" in the bottom IE 7 message bar (where it usually says "Done" when you load a page). There is another function that also does not work - it's in the Telephone number fields - it' supposed to check that the values entered are numbers. Would anyone be kind enough to try to run this page in your browser and see if the validation functions runs for you? If they don't, could you offer any suggestions as to code errors or possible browser settings that might be causing the problem? Thanks in advance to anyone offering their time!!! Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <meta name="description" content="Page used to contact SRS" /> <meta name="keywords" content="resume, resumes, job, jobs, job market, job info, interview tips, job interview, salary, salaries, cover letter, cover letters, SRS, Superior Resume Source, business cards, help with resume, help with job search" /> <title>Contact SRS</title> <link rel="stylesheet" href="style.css" type="text/css" media="screen" /> <script type="text/javascript">/* <![CDATA[ *//* ]]> */ function checkForNumber() { if (isNan(document.forms[0].area.value)){alert("You must enter a numerical value");return false;} else if (isNan(document.forms[0].exchange.value)){alert("You must enter a numerical value");return false;} else if (isNan(document.forms[0].phone.value)){alert("You must enter a numerical value");return false;} } // function validateForm () // { // if(document.forms[0].firstname.value==""){alert("You must enter your first name.");return false;}} // } // else if(document.forms[0].lastname.value== ""){alert("You must enter your last name.");return false;} // else if(document.forms[0].address1.value== ""){alert("You must enter your address.");return false;} // else if(document.forms[0].city.value== ""){alert("You must enter a city.");return false;} // else if(document.forms[0].state.value== ""){alert("You must enter a state.");return false;} // else if(document.forms[0].zip.value== ""){alert("You must enter your zip code.");return false;} // else if(document.forms[0].area.value== ""||document.forms[0].exchange.value== ""||document.forms[0].phone.value== "") // {alert("You must enter a complete phone number.");return false;}} // else {alert('Thank you! SRS will contact you within one business day.');}} function isEmpty(strfirst, strlast, straddress, strcity, strstate, varzip) { strfirst = document.forms[0].firstname.value strlast = document.forms[0].lastname.value straddress = document.forms[0].address1.value strcity = document.forms[0].city.value strstate = document.forms[0].state.value varzip = document.forms[0].zip.value //name field if (strfirst == "" || strlast == null || strfirst.charAt(0) == ' ') { alert("\"First Name\" is a mandatory field.\nPlease amend and retry.") return false; } //last name field if (strlast == "" || strlast == null || strlast.charAt(0) == ' ') { alert("\"Last Name\" is a mandatory field.\nPlease amend and retry.") return false; } //address field if (straddress == "" || straddress == null || straddress.charAt(0) == ' ') { alert("\"Address\" is a mandatory field.\nPlease amend and retry.") return false; } return true; } //city field if (strcity == "" || strcity == null || strcity.charAt(0) == ' ') { alert("\"City\" is a mandatory field.\nPlease amend and retry.") return false; } //state field if (strstate == "" || strstate == null || strstate.charAt(0) == ' ') { alert("\"State\" is a mandatory field.\nPlease amend and retry.") return false; } } function check(form)){ if (isEmpty(form.firstname)){ if (isEmpty(form.lastname)){ if (isEmpty(form.address1)){ if (isEmpty(form.city)){ if (isEmpty(form.state)){ } } } } } return false; } </script> </style> </head> <body> <a name="top"></a> <div id="container"> <div id="header"> <h1></h1> </div> <div id="content"> <h2>Contact SRS</h2> <p>Use this page to contact SRS and/or order products and services. Refer to our <a href="services.htm">Services</a> page for pricing.</p> <p><strong>Remember, our work is guaranteed or your money back!</strong></p> </div> <div id="sidebar"> <ul> <li><a href="index.htm">Home</a></li> <li><a href="about.htm">About SRS</a></li> <li><a href="services.htm">Services</a></li> <li><a href="contact.htm">Contact SRS</a></li> </ul> <div class="widget"> Did you know that employers prefer CV's that are limited to three pages or less? </div> <div class="widget"> First impressions last a lifetime... write a Thank You letter EVERY time! </div> <div class="widget"> Want to know your personality type? Take this FREE <a href="http://www.careerfulfillment.com/?hop=daviduk"> personality test.</a> </div> </div> </div> <form method="get" enctype="application/x-www-form-urlencoded"> <table border="0"> <tr> <td id="contact table" valign="top"> <h3><strong>Contact SRS</strong></h3> <p><strong>* Required</strong><p> <p>First Name*<br /> <input type="text" name="firstname" size="50" /> </p> <p>Last Name*<br /> <input type="text" name="laststname" size="50" /> </p> <p>Address*<br /> <input type="text" name="address1" size="50" /><br /> <input type="text" name="address2" size="50" /> </p> <p>City, State, Zip*<br /> <input type="text" name="city" size="34" /> <input type="text" name="state" size="2" maxlength="2" /> <input type="text" name="zip" size="10" maxlength="10" onchange="return checkForNumber;" /> </p> <p>Telephone*</p> <p>(<input type="text" name="area" size="3" maxlength="3" onchange="return checkForNumber;" />) <input type="text" name="exchange" size="3" maxlength="3" onchange="return checkForNumber;" /> <input type="text" name="phone" size="4" maxlength="4" onchange="return checkForNumber;" /></p> <strong>Check the reason(s) for your inquiry?*</strong> <p><input type="checkbox" name="geninquiry" value="General_Inquiry" />General Inquiry <br/> <input type="checkbox" name="help" value="Help" />Help<br/> <input type="checkbox" name="new_resume" value="Order New Resume" />Order a New Resume <br /> <input type="checkbox" name="Update_resume" value="Update My Resume" />Update My Resume <br/> <input type="checkbox" name="cover_letter" value="Order Cover Letter" />Order a Cover Letter <br /></p> <textarea rows="10" cols="50">Type your comments here</textarea> <p><input type="button" name="Submit" value="Submit" onsubmit="return check(this);" /> <input type="reset" /></p> </td> </tr> </table> </form> <a href="#top">Back to Top</a><br /> </body> </html> . Why do we have functions without parentheses at times? Ex 1) http://www.youtube.com/watch?v=OHYFNDzlDTE (On 2:18) Ex 2) At the bottom: Code: var panel, flag; function mousemoveResponse(e) { var x, y; if( window.event ) { x= event.x; y=event.y; } else if(e) { x=e.pageX; y=e.pageY; } if(flag) { panel.innerHTML = "Mouse is at X: " + x + ", Y: " + y ; } } function mouseoverResponse() { flag = false; panel.innerHTML = "Mouse is Over"; } function mouseoutResponse() { flag = true ; } function init() { panel=document.getElementById("panel"); flag = true; panel.innerHTML="Move the mouse..." ; document.onmousemove=mousemoveResponse; panel.onmouseover=mouseoverResponse; panel.onmouseout=mouseoutResponse; } onload=init; I would really like to thank the guys that have been helping me. I think my brain would have exploded otherwize. Some of the problems have been simple misspellings, but when you have been staring at the same thing for hours it can sorta slip by. HOPEFULLY this will be the last thing that I need some help with. Bascially Im not sure if Im using the function command properly. When Submit Details is click it should validate the form and then when Calculate is pressed calculate the form (obviously). I think maybe my var Array table is wrong? And the way the functions are set out doesnt seem it sit right. Code: <script type="text/javascript"> var element; var itemArray = new Array(); itemArray [0] = "Bike" //not sure if these are properly writen?// itemArray [1] = "TV" itemArray [2] = "iPod" itemArray [3] = "Car" var flag="OK"; var count = 0 document.write ("<form name='myform'><br><table>"); while (count <= 3) { if (count < 3) { document.write ("<tr><td><value>Enter money from " + itemArray [count] + " sale.</td><td><input type='text' name=" + itemArray [count] + "></td></tr>"); count++ } else { document.write ("<tr><td><value>Enter the price of " + itemArray [count] + ".</td><td><input type='text' name=" + itemArray [count] + "></td></tr>"); document.write ("<tr><td><input type='submit' value='Submit Details' onClick='validateform'</td></tr>"); document.write ("<tr><td><input type='submit' value='Calculate Details' onClick='calculateform'</td></tr></table></form>"); count++ } } function validateform() //<=== doesnt run { element=document.getElementsByTagName ('input'); for (counter=0; counter<element.length; counter++) { switch (element[counter].type) { case "submit": break; default: if (isNaN(element[counter].value) || (element[counter].value == "") ) { alert("You need to enter a number into " + element[counter].name); flag="NotOK" } else { Bike=element[0].value; TV=element[1].value; iPod=element[2].value; Car=element[3].value; } break; } } } function calculateform() //<=== Doesnt run either { if (flag=="OK") { if ( (Number(element[Bike])) && (Number(element[TV])) && (Number(element[iPod])) && (Number(element[Car])) ) //<========= Gets Bike is not defined error easy// { TotalMoney= parseFloat (Bike) + parseFloat (TV) + parseFloat (iPod) if (TotalMoney >= Car) { alert ("The total money is " + TotalMoney + " and the car price is " + Car + " and you can afford the car") } else { alert ("The total money is " + TotalMoney + " and the car price is " + Car + " and you cannot afford the car") } } } } </script> |