JavaScript - Javascript Function With Array
Similar TutorialsHi all, I have a question about following code. It is tested as working well but the problem is that I need to catch values of array registos from javascript function getData(dataSource, sql_str) in PHP and not only update values of each form element. Can you help me, plz? Code: <script language = "javascript"> function getData(dataSource, sql_str){ if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ var registos = eval('('+xmlhttp.responseText+')'); document.getElementById("id_1").value=registos["cliente"]; document.getElementById("id_2").value=registos["designacao"]; } } xmlhttp.open("GET", dataSource+"&sql_str="+sql_str,true); xmlhttp.send(); } </script> <?php $sql_str="SELECT cliente, designacao WHERE cliente = '$cliente'"; $record=$db->sql_query($sql_str); list($registos[cliente], $registos[designacao]) = $db->sql_fetchrow($record); echo "<form id=\"form\">"; echo "<input type=\"text\" id=\"id_1\" name=\"registos[cliente]\" value=\"".$registos[cliente])."\">"; echo "<br>"; echo "<input type=\"text\" id=\"id_2\" name=\"registos[designacao]\" value=\"".$registos[designacao]."\">"; echo "<br>"; echo "<input type=\"button\" value=\"CONFIRMAR\" onclick=\"getData('mysql_query.php', '".base64_encode(serialize($sql_str))."')\">"; echo "</form>"; hello all, I am new to javascript, i just wanted to know how can i send a array from perl to javascript function.... if anybody having any idea about this please reply me..thanks in adbvance I want to pass dynamic array values to java script function.how its possible. explain with some code.........
Hi, I am facing a problem in passing replace() function as an argument in user defined java function, can any one help me how to resolve it? intention is to pass a file path to my user defined function, but before passing the path i want to replace the character '\' to '\\' I am posting my javascript function he <a href="#" onclick="OpenDocPreview('<%# Eval("PATH")%>'.replace(/\\/g,"\\\\"), '<%# Eval("Filename")%>')"><%# Eval("DocTitle") %></a> function OpenDocPreview(url, docname) { alert('message from search base : ' + url + ' ' + docname); } thank you, I am working on a page where the user will select a location from a dynamically generated dropdown list. I was able to create the php multidimensional array (tested and working) from a MySql database using the users information at login, but I'm having problems converting it to a javascript multidimensional array. I need to be able to access variables that I can pass to a number of text fields within an html form. For instance, if a user belongs to a company with multiple addresses, I need to be able to let them select the address they need to prepopulate specific text fields. php array creation: Code: if ($row_locations) { while ($row_locations = mysql_fetch_assoc($locations)) { $mail[$row_locations['comp_id']]=array('mailto'=>$row_locations['mailto'], 'madd'=>$row_locations['madd'], 'madd2'=>$row_locations['madd2'], 'mcity'=>$row_locations['mcity'], 'mstate'=>$row_locations['mstate'], 'mzip'=>$row_locations['mzip'], 'billto'=>$row_locations['billto'], 'badd'=>$row_locations['badd'], 'badd2'=>$row_locations['badd2'], 'bcity'=>$row_locations['bcity'], 'bstate'=>$row_locations['bstate'], 'bzip'=>$row_locations['bzip']); } } javascript function - this should create the array and send variables to text fields. Code: function updateAddress() { var mail = $.parseJSON(<?php print json_encode(json_encode($mail)); ?>); { if (comp_id in mail) { document.getElementById('mailto').value=mail.comp_id.mailto.value; document.getElementById('madd').value=mail.comp_id.madd.value; document.getElementById('madd2').value=mail.comp_id.madd2.value; document.getElementById('mcity').value=mail.comp_id.mcity.value; document.getElementById('mstate').value=mail.comp_id.mstate.value; document.getElementById('mzip').value=mail.comp_id.mzip.value; } else { document.getElementById('mailto').value=''; document.getElementById('madd').value=''; document.getElementById('madd2').value=''; document.getElementById('mcity').value=''; document.getElementById('mstate').value=''; document.getElementById('mzip').value=''; } } } Where is this breaking? Thanks in advance. How can I call a PHP Function inside a Javascript Function? This is what I have so far, but I don't think I'm doing it the right way. Any suggestions? PHP Code: <?php function phpQuery(){ $query = mysql_query("INSERT INTO mytable VALUES('','name','email')"); } ?> <script type="text/javascript"> function delayQueries() { timeoutID = window.setTimeout(doQueries, 2000); } function doQueries() { var runQuery = "<?php phpQuery(); ?>"; } </script> I made a mouseover event of a caption on a picture, when I hover the opacity of the background color of the hover and the text goes down. What I want is that when I hover over the image which the caption is floating on, the onmouseover event gets activite. For an imaginary example: Code: function unhighlight(x) { x.style.backgroundColor="transparent" } Function ActivationFuction() { activate.function="unhighlight" } thanks Hi everyone, I am pretty new at javascript OOP. I have a javascript file that has plenty of javascript functions, variables, arrays, etc. I need to convert this into an object. Here is an example: Code: var myvar1 = ''; var myvar2 = new array(); var myvar3 = new array(); var myvar4; var myvar5 = 60; var myvar6 = ''; function myfunc1(){ myvar1 = 'hello'; return myvar1; } function myfunc2(somenum=0){ myvar5 = somenum; //calling myfunc1() from within this function. //do something in here } function myfunc3(){ //calling myfunc1() from within this function. for(i=0;i<somelength;i++){ myvar2 = myvar3[i]; (something to put into the array) } } 1. I need to create an object and put ALL the varibles (myvar1 - myvar5) in that object. 2. Then the first two functions "myfunc1 and myfunc2" inside the same object. 2. And the function myfunc3 needs to sit OUTSIDE my object as a regular javascript function. It calls on myfunc1 (which is inside the object) and also inserts data into myvar2 (which is classified inside the object) and myvar3 (which is classified inside the object as well). This is what i came up with, but it's not going well: Code: var myobj1 = function(){ this.myvar1 = ''; this.myvar2 = new array(); this.myvar3 = new array(); this.myvar4; this.myvar5 = 60; this.myvar6 = ''; var myfunc1 = function(){ this.myvar1 = 'hello'; return this.myvar1; } var myfunc2 = function(somenum=0;){ this.myvar5 = somenum; //calling this.myfunc1() from within this function. //do something in here } } function myfunc3(){ //calling mynewobj.myfunc1() from within this function. for(i=0;i<somelength;i++){ mynewobj.myvar2 = mynewobj.myvar3[i]; (something to put into the array) } } var mynewobj = myobj1; HELP.....!!!! i really want to get into object oriented world, so thanks in advance. Hi, i can't find the mistake in my little script hope someone can help me. PHP Code: <?php /* -------------------- read thumbfolder -------------------- */ function isRdyPfD($filename){ if ($filename == '.' || $filename == '..') { // To-Top-Dir return false; } $ext = explode(".",$filename); $ext = $ext[sizeof($ext) - 1]; $allowedformats = array ( 'jpg', 'png', 'jpeg', 'gif' ); return in_array($ext,$allowedformats); } function getPicsfromDir($dir){ /* array with names of the pictures in $dir */ if (is_dir($dir)) { if ($dh = opendir($dir)) { $filearray = array(); while (($file = readdir($dh)) !== false) { if (isRdyPfD($file) === true) { $filearray[] = $file; } } closedir($dh); return $filearray; } } else { return false; } } // End Function $thumbs = getPicsfromDir("./images/thumbs/"); /* -------------------- thumbfolder -------------------- */ echo "<div id='thumbslider'>\n"; echo "<ul id='thumbs'>\n"; for($i = 0; $i < count($thumbs); $i++){ echo "<li><img src=\"./images/thumbs/$thumbs[$i]\" onclick=\"thumbClick($i)\" /></li>\n"; } echo "</ul>\n"; echo "</div>\n"; /* -------------------- big size images folder -------------------- */ $bigSizeImages = getPicsfromDir("./images/"); //print_r($bigSizeImages); $jsValue = ''; for ($j=0; $j < count($bigSizeImages); $j++){ $jsValue = $jsValue . $bigSizeImages[$j]; if ($j < (count($bigSizeImages)-1)) { $jsValue = $jsValue . ","; } } ?> <script type="text/javascript"> images = new Array(<?php echo $jsValue ?>); function thumbClick(pos){ //alert(pos); alert(images[pos]); } </script> I can't trace the images array values? thanks for a feedback!!! Hi, Here is a working code to copy 2d php array to 2d javascript array. Code: <html> <head> <?php for($i = 0; $i < 3; $i++) { for($j = 0; $j < 2; $j++) {$quest[$i][$j] = $i*10+$j;} } ?> <script type="text/javascript"> var questions = new Array(3); for (var i = 0; i < 3; i++) { questions[i] = new Array(2); } questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; document.write(questions[0][0] + "<br />"); document.write(questions[0][1] + "<br />"); document.write(questions[1][0] + "<br />"); document.write(questions[1][1] + "<br />"); document.write(questions[2][0] + "<br />"); document.write(questions[2][1] + "<br />"); </script> </head> </html> Now,here's the thing.Notice these lines in the code questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; I would like to put these lines in a loop,something like for (var i = 0; i < 3; i++) { questions[i] = ["<?php echo join("\", \"", $quest[i]); ?>"]; } But even after a lot of efforts I am unable to do so,what am I doing wrong?Thanks Hi, In a nutshell,can anyone tell me how to copy a 2d (two dimensional ,2 dimensional) php array to 2d javascript array?I would be obliged if anyone can provide me a method of doing that OR I have written a code to copy a 2d php array to a 2d javascript array.It is working but there is one problem(please see the following).Can anyone tell me what I am doing wrong here? The 2d php array is $quest[100][6] and the 2d javascript array is questions[100][6] . I have written the javascript code inside the <?php....?> itself using echo "<script language="javascript" type="text/javascript">.......</script>; Now ,inside the javascript,when I try to copy the 2d php array to the 2d javascript array using the following method it works questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1]= ["<?php echo join("\", \"", $quest[1]); ?>"]; ... and so on However, if I try to do the same using the following method it does not work for (var i= 0; i <= 99; i++) { questions[i]= ["<?php echo join("\", \"", $quest[i]); ?>"]; } Why is that?What mistake am I making?Any help will be deeply appreciated.Thanks -----------------------------THE CODE------------------------------------ <?php Access database and store result of mysq_query in $result....... $result = mysql_query($query); for ( $count = 0; $count <= 99; $count++) { $quest[$count]=mysql_fetch_array($result,MYSQL_NUM);; } echo "<script language="javascript" type="text/javascript"> var questions = new Array(100); for (var i = 0; i <100; i++) { questions[i] = new Array(6); } /*The following method of copying 2d php array to 2d javascript array is not working for ( var i = 0; i <= 99; i++) { questions[i]= ["<?php echo join("\", \"", $quest[i]); ?>"]; } */ /*The following method ,however,is working*/ questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"",$quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"",$quest[2]); ?>"]; ....and so on </script>"; mysql_close($db_server); ?> I would like to pass an array to a function but how does the program know which array I would like to choose from?? Lets say I have 3 arrays and I would like to pass array C, to my function. I checked the web but they only show if you have ONLY 1 array but NOT for multiple arrays. How would I even go about doing this?? Code: var arrA=new Array("fox.com","nbc.com","abc.com", "google.com"); var arrB=new Array("car","bike","boat", "plane"); var arrC=new Array("1","2","3", "4", "5", "6", "7", "8", "9"); function display(myArray){ myArray[1] = "changed"; } display(myArray); document.writeln(myArray[1]); thanks I've been trying for several hours to solve this University problem but I'm just unable to get it done. I'm supposed to create a function that takes an array with all the A,B,C,D ... and shift them one place to the right (for instance array of A,B,C,D should be shifted and be like D,A,B,C - the last character goes to first position) Here is the code that I've been dealing with: Code: var abcArray = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; function shift(anyArray) { var newArray = Array(anyArray.length); for (var position = 0; position < newArray.length; position = position + 1) { if (position == 0) { newArray[position] = anyArray[anyArray.length - 1]; } else { newArray[position] = anyArray[position - 1]; } } anyArray = newArray; } shift(abcArray); document.write(abcArray); If I remove the function and try this code directly on the array it does work, but when I use the code as it is just like you see - the function shift doesn't change the Array and doesn't shift the values inside one position to the right and I have no clue why. Anyone has some ideas?! Array.prototype.each = function (fn) { this.map(fn) } This is my each function that works great in every other browser but IE. UGH! What am I doing wrong? the error points to the this in the function. Is it that IE doesn't like map? Has anyone seen this before? I thought my code was correct. Works perfect in FF, chrome and opera. the canvas text doesn't work in opera, but it does render the features so the each function is working. I'll post the code if needed, but it's huge. here's the script running. http://www.pdxbusiness.com/canvas/golf/ I've read through the past threads and did not find anything that quite matched my problem, which surprises me because I thought it would be fairly common. Anyway, I have a script where I define an array and populate the array. In the same script is a function that tries to scan the table looking for a match with the value in a global primitive. In the HTML, a button click invokes the function. But while the function can correctly access the global primitive, it simply stops running when it encounters the reference to the global array. Here's a shortened, simplified snippet of the code: [code] <script type="text/javascript"> var len = 2; var course_code = new Array(); course_code[0] = "A010"; course_code[1] = "A500"; function runcodes() { alert('In runcodes'); alert('len = '+len); alert('course_code.length = '+course_code.length); for (i = 0 ; i < course_code.length ; i++) {alert(course_code '+i+' = '+course_code[i]);} } </script> <body> <button type="button" name="runc" id="runc" onclick="runcodes()"; > Click to display course codes table. </button> </body> [ICODE] When I bring this up in a browser and click the button, I get the following alerts: In runcodes len = 2 and then the script simply stops. So it stops on the first reference to the array. Clearly I am getting to function, and the function is able to handle the global primitive 'len', but it stops on encountering the global array. I've tried a lot of variations on the the theme, but nothing gets past this restriction. How do I access elements in a global array from inside a function? Hello there, I was having some trouble with 2D arrays (or array of arrays). Essentially, the array has 100 rows, with two columns. The first column of every row holds a name, and the second holds a sales amount. With the use of a do while loop, the user can continuously add up to 100 names and sales amounts. After all the information the user wishes to add is stored into the 2D array I'm attempting to pass that very same 2D array as a parameter to a function called printRow as can be seen in the code below: Note: the function call and the actual function are found in two separate javascripts. Code: var salesPerson=new Array(100) for (i=0; i <=100; i++) { salesPerson[i]=new Array(2); } var x = 0; do { salesPerson[x][0] = getName(); salesPerson[x][1] = getSales(); x++; }while(x != 100 && confirm("Enter more employee information?")); printRow(salesPerson[][]); Code: function getName() { var nameEntered = prompt("What is your first name?"); return nameEntered; } function getSales() { var error; var salesEntered; do { salesEntered = prompt("What were your sales?"); error = false; if (isNaN(salesEntered) || salesEntered == null || salesEntered == "") error = true; }while(error); return salesEntered; } function printRow(salesPerson[][]) { for (i =0; i<salesPerson.length; i++) { document.write(salesPerson[i][0] + " " + salesPerson[i][1]); } } At the moment I'm only looking to print the contents of the 2D array that I pass as a parameter to the document. As is, the javascipt doesn't seem to execute at all, it worked fine up until I added the printRow function call and function which leads me to believe I may not be passing it as a parameter correctly. Any tips on how to do this correctly would be greatly appreciated! hi there i'm a newb so be nice lol --> I want to use javascript in an array to submit content. I have some php background so using some techniques I worked this out. >> I need to select bathroom details and the script needs to be user friendly, however I have multiple bathrooms that can be selected. So based on a drop down list I need to select the bathrooms needed and then I will get more options as stated below. These options will need to be submitted so I need different varibles for each item selected I expect that you'll have a better way to do it I just want it to work my HTML script LOOKED as follows Code: <!-- BATHROOM TYPE SELECTOR HTML--> <label> <input name="fullbathr" type="checkbox" value="fullbathr" onClick="checker('yesFullba'); UpdateChecks('option');" /> Full Bathroom </label> <span id="yesFullba" style="visibility:visible"> | <input name="babath" type="checkbox" value="babath" id="babath" onClick="change('bathselba'); UpdateChecks('option');";/> Bath </label> | <input name="bashower" type="checkbox" value="bashower" id="bashower" onClick="change('showerselba'); UpdateChecks('option');" /> Shower </label> <span id="bathselba" style="visibility:hidden"> <br /> Bath Type: <label> <input type="radio" name="bathtype" value="standardba" id="bathtype_0" /> Standard</label> <label> <input type="radio" name="bathtype" value="ovalba" id="bathtype_1" /> | Oval</label> <label> <input type="radio" name="bathtype" value="cornerba" id="bathtype_2" /> | Corner</label> <label> <input type="radio" name="bathtype" value="spaba" id="bathtype_3" /> | Spa</label> </span> <span id="showerselba" style="visibility:hidden"> <br /> Shower Heads: <label> <input type="radio" name="showerheadba" value="singlesho" id="showerheadba_0" /> Single Head</label> <label> <input type="radio" name="showerheadba" value="doublesho" id="showerheadba_1" /> | Double Head</label> </span> </span> <br /> <!-- end BATHROOM TYPE SELECTOR HTML --> i converted it as follows Code: <!-- CREATE BATHROOMS --> function createbaths(number2) { databaths = ""; inter = "'"; dinter = '"'; spaces=" "; if (number2 < 7 && number2 > -1) { for (i=1; i <= number2; i++) { databaths = databaths + "<br><strong>Bathroom " + i + " :</strong><br>" <!-- BATHROOM TYPE SELECTOR JAVASCRIPT--> + " <label>" + " <input name=" + dinter + "fullbathr" + dinter + " type=" + dinter + "checkbox" + dinter + " value=" + dinter + "fullbathr" + dinter + " onClick=" + dinter + "checker(" + inter + "yesFullba" + inter + "); " + "UpdateChecks(" + inter + "option" + inter + ");" + dinter + " />" + "Full Bathroom </label>" + "<span id=" + dinter + "yesFullba" + dinter + " style=" + dinter + "visibility:visible" + dinter + "> | " + "<input name=" + dinter + "babath" + dinter + " type=" + dinter + "checkbox" + dinter + " value=" + dinter + "babath" + dinter + " id=" + dinter + "babath" + dinter + " onClick=" + dinter + "change(" + inter + "bathselba" + inter + "); " + "UpdateChecks(" + inter + "option" + inter + ");" + dinter + ";/>" + "Bath </label> | " + "<input name=" + dinter + "bashower" + dinter + " type=" + dinter + "checkbox" + dinter + " value=" + dinter + "bashower" + dinter + " id=" + dinter + "bashower" + dinter + " onClick=" + dinter + "change(" + inter + "showerselba" + inter + "); " + "UpdateChecks(" + inter + "option" + inter + ");" + dinter + " />" + "Shower </label> " + "<span id=" + dinter + "bathselba" + dinter + " style=" + dinter + "visibility:hidden" + dinter + ">" + "<br />" + "Bath Type: <label>" + "<input type=" + dinter + "radio" + dinter + " name=" + dinter + "bathtype" + dinter + " value=" + dinter + "standardba" + dinter + " id=" + dinter + "bathtype_0" + dinter + " /> " + "Standard</label>" + "<label>" + "<input type=" + dinter + "radio" + dinter + " name=" + dinter + "bathtype" + dinter + " value=" + dinter + "ovalba" + dinter + " id=" + dinter + "bathtype_1" + dinter + " /> | " + "Oval</label>" + "<label>" + "<input type=" + dinter + "radio" + dinter + " name=" + dinter + "bathtype" + dinter + " value=" + dinter + "cornerba" + dinter + " id=" + dinter + "bathtype_2" + dinter + " /> | " + "Corner</label>" + "<label>" + "<input type=" + dinter + "radio" + dinter + " name="+ dinter + "bathtype" + dinter + " value=" + dinter + "spaba" + dinter + " id=" + dinter + "bathtype_3" + dinter + " /> | " + "Spa</label></span>" + " <span id=" + dinter + "showerselba" + dinter + " style=" + dinter + "visibility:hidden" + dinter + "><br />" + " Shower Heads: <label>" + "<input type=" + dinter + "radio" + dinter + " name=" + dinter + "showerheadba" + dinter + " value=" + dinter + "singlesho" + dinter + " id=" + dinter + "showerheadba_0" + dinter + " />" + "Single Head</label>" + "<label>" + "<input type=" + dinter + "radio" + dinter + " name=" + dinter + "showerheadba" + dinter + " value=" + dinter + "doublesho" + dinter + " id=" + dinter + "showerheadba_1" + dinter + " /> | " + "Double Head</label>" + " </span> </span> <br /> " <!-- end BATHROOM TYPE SELECTOR JAVASCRIPT --> The entire forum is using a few different functions I've setup for this section i've got these sets Code: <!-- --> function change(id){ if (document.all[ id ].style.visibility == 'hidden') { document.all[ id ].style.visibility = 'visible'; document.all[ id ].focus(); } else { document.all[ id ].style.visibility = 'hidden'; document.all[ id ].value = ''; document.all[ id ].checked = ''; } } <!-- --> <!-- for full bathroom selector--> function UpdateChecks(which) { if (which == 'fullBathroom') { fullBathroomIsChecked(); } else if(which == 'option') { BoxesCheck(); } } function fullBathroomIsChecked() { if(document.all.fullbathr.checked == false) { BoxesCheck(); } else { document.all.babath.checked = false; document.all.bashower.checked = false; } } function BoxesCheck() { document.all.fullbathr.checked = false; if((document.all.babath.checked == true) && (document.all.bashower.checked == true)) { document.all.fullbathr.checked = true; document.all.yesFullba.style.visibility = 'hidden'; fullBathroomIsChecked(); } } <!-- for full bathroom selector END --> CURRENTLY I have a drop down list from 1 to 6 when a bathroom is selected lets say 3 then it SHOULD display full - bath - shower - PER EACH BATHROOM if bath and shower is selected they disappear and full remains ticked if u tick full bath and shower disappear. included a sub cat. that changes based on the above selection this works in html but The bathroom varibles in the javascript need to be changed to suite a changing varible I based what I understand that an array will need to be added but how to i adjust the script correctly. I might work it out just have a bit of overload at the moment. Thanks in advance Another homework assignment that I can't quite seem to get to work... I've been asked to do the following using javascript: -Create a function named randInt() with one parameter of "size". Declare a variable named "rNum" equal to a random integer between 1 and the value of the size variable. Return the value of the "rNum" varialbe from the function. -Create a function named getQuote() with one parameter anemd "qNum". The function should create an array named mtQuotes with five quotes; there should be no quote for the array index "0". Return the value of the mtQuotes array for the qNum index. - In the div element of "quotes" insert a script with the following commands: Declare a variable named "randValue" which is euqal to a random integer between 1 and 5 (use the randInt() function). Declare a variable named "quoteText" containing the quote whose array index value is equal to randValue. Write the value of quoteText to the web page. Here is what I have...it returns undefined. thanks. Code: <html> <head> <script type="text/javascript"> function randInt(size) { var rNum=Math.ceil(Math.random()*5); return(rNum); } </script> <script type="text/javascript"> function getQuote(qNum); var mtQuotes = new Array(); mtQuotes[0] = ""; mtQuotes[1] = "I smoke in moderation, only one cigar at a time."; mtQuotes[2] = "Be careful of reading health books, you might die of a misprint."; mtQuotes[3] = "Man is the only animal that blushes or needs to."; mtQuotes[4] = "Clothes make the man. Naked people have little or no influence on society."; mtQuotes[5] = "One of the most striking differences between a cat and a lie is that a cat has only nine lives."; return mtQuotes[qNum]; </script> </head> <body> <div id="quotes"> <script type="text/javascript"> var randValue=randInt(5); var quoteText=getQuote(randValue); document.write(quoteText); </script> </div> I'm trying to pass titleArray and pointsArray to the task(); I'm getting an error mgs this.assignments() is not a function. I've highlighted this.assignments() Code: <html> <head> <title>Variable - Examples 1</title> <script type="text/javascript"> function Student(firstName, lastName, email, courseID, titleArray, pointsArray){ this.firstName = firstName; this.lastName = lastName; this.email = email; this.courseID = courseID; this.title = titleArray; this.points = pointsArray; this.assignments = task; this.totalPoints = addExam; this.finalGrade = calcGrade; } Student.prototype = { constructor : Student, toString : studentInfo }; var t = this.title; var p = this.points; function task(t, p){ var title = this.t; var points = this.p; var titlePoints = ""; for (var i=0; i < points.length; i++){ titlePoints += title[i] + " : " + points[i] + "<br>"; } return titlePoints; } function addExam(){ var assignments = [30, 30, 28, 27, 29]; var exams = [41,45]; var highOfTwo = Math.max(41,45); var sum = 0; for (var i=0; i < assignments.length; i++){ sum += assignments[i]; } return sum + highOfTwo; } var totalPoints = addExam(); function calcGrade(){ var grade; if (totalPoints >= 190){ return "A+"; }else if (totalPoints >= 180){ return "A"; }else if (totalPoints >= 175){ return "B+" }else if (totalPoints >= 170){ return "B"; }else if (totalPoints >= 165){ return "B-"; }else if (totalPoints >= 160){ return "C"; }else if (totalPoints >= 150){ return "D"; }else if (totalPoints < 150){ return "F"; } } function studentInfo(){ return "Student : " + this.lastName + "," + this.firstName + "<br>"+ "eMail : " + this.email + "<br>" + "Course ID : " + this.courseID + "<br>" + "--------------------------------" + "<br>" + this.assignments() + "--------------------------------" + "<br>" + "Total Points : " + this.totalPoints() + "<br>" + "Final Grade : " + this.finalGrade(); } var titleArray = ["Assignment1","Assignment2","Assignment3","Assignment4","Assignment5","MidTerm", "Final"]; var pointsArray = [30, 30, 28, 27, 29, 41, 45]; var student = new Student("FirstName", "Lastname", "myemail@gmail.com","COIN-070B.01", titleArray, pointsArray ); </script> </head> <body> <script type="text/javascript"> document.writeln(student.toString()); </script> </body> </html> Hi all, I have an array containing numbers. I want to order this numbers contained from major to minor in order to print them .. Here's what I have done: Code: var arr = new Array(6); arr[0] = "10"; arr[1] = "5"; arr[2] = "40"; arr[3] = "25"; arr[4] = "1000"; arr[5] = "1"; function sortNumber(a,b) { return b - a; } for(var i=0;i < 6; i++){ var myarray = arr[i]; myarray.sort(sortNumber); alert(myarray); } But I get no alert and a "myarray.sort is not a function" error. What am I doing wrong? How can I solve this? Thanks a lot in advance! |