Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Simple Javascript issue
New Posts  All Forums:Forum Nav:

Simple Javascript issue

post #1 of 5
Thread Starter 
Hey guys,

I'm trying to return students all the names of students as I add them to the array. This will only return the first 1 and nothing more, how do I get it to return all?

Quote:
function Course()
{
this.students = new Array();

this.addStudent = function()
{
var firstName = document.getElementById("firstName");
var lastName = document.getElementById("lastName");

var s = new Student(firstName.value, lastName.value);
this.students.push(s);
}

this.returnStudents = function()
{
for(var i = 0; i < this.students.length; i++)
{
return this.students[i].fname + " " + this.students[i].lname;
}
}
}

function Student(fname, lname)
{
this.fname = fname;
this.lname = lname;
}

window.onload = function()
{

var buttonAdd = document.getElementById("addStudent");
var c = new Course();
buttonAdd.onclick = function()
{
c.addStudent();
alert(c.returnStudents());
}

}
Epic Build! 0.0
(13 items)
 
  
CPUMotherboardGraphicsRAM
{Intel i7 930 @ 4.2 GHz w/ HT} {Asus Rampage III Extreme} {Zotac AMP! + EVGA GTX 480 SLI @ 800/1600/1900} {G.SKILL Ripjaws 6GB DDR3 1605Mhz @ 7-8-7-20-1N} 
Hard DriveOptical DriveOSMonitor
{OCZ Agility 2 120GB, WD Black 1TB, F4 2TB RAID1} {Asus DVD+/- RW} {Windows 7 Ultimate 64 bit} {Asus VH242H 23.6" 1080p} 
KeyboardPowerCaseMouse
{Logitech G15} {Corsair HX 1000W} {CoolerMaster HAF-X} {Logitech G700} 
Mouse Pad
{Razer Goliathus - Speed} 
  hide details  
Reply
Epic Build! 0.0
(13 items)
 
  
CPUMotherboardGraphicsRAM
{Intel i7 930 @ 4.2 GHz w/ HT} {Asus Rampage III Extreme} {Zotac AMP! + EVGA GTX 480 SLI @ 800/1600/1900} {G.SKILL Ripjaws 6GB DDR3 1605Mhz @ 7-8-7-20-1N} 
Hard DriveOptical DriveOSMonitor
{OCZ Agility 2 120GB, WD Black 1TB, F4 2TB RAID1} {Asus DVD+/- RW} {Windows 7 Ultimate 64 bit} {Asus VH242H 23.6" 1080p} 
KeyboardPowerCaseMouse
{Logitech G15} {Corsair HX 1000W} {CoolerMaster HAF-X} {Logitech G700} 
Mouse Pad
{Razer Goliathus - Speed} 
  hide details  
Reply
post #2 of 5
If im reading this correctly, you will never get to the second iteration of the for loop because you are exiting the function via the return statement.


this.returnStudents = function()
{
for(var i = 0; i < this.students.length; i++)
{
return this.students[i].fname + " " + this.students[i].lname;
}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #3 of 5
Thread Starter 
Yeah that's true, is there any way for me to save all the names without returning from the first iteration? I can't figure that out. >.<
Epic Build! 0.0
(13 items)
 
  
CPUMotherboardGraphicsRAM
{Intel i7 930 @ 4.2 GHz w/ HT} {Asus Rampage III Extreme} {Zotac AMP! + EVGA GTX 480 SLI @ 800/1600/1900} {G.SKILL Ripjaws 6GB DDR3 1605Mhz @ 7-8-7-20-1N} 
Hard DriveOptical DriveOSMonitor
{OCZ Agility 2 120GB, WD Black 1TB, F4 2TB RAID1} {Asus DVD+/- RW} {Windows 7 Ultimate 64 bit} {Asus VH242H 23.6" 1080p} 
KeyboardPowerCaseMouse
{Logitech G15} {Corsair HX 1000W} {CoolerMaster HAF-X} {Logitech G700} 
Mouse Pad
{Razer Goliathus - Speed} 
  hide details  
Reply
Epic Build! 0.0
(13 items)
 
  
CPUMotherboardGraphicsRAM
{Intel i7 930 @ 4.2 GHz w/ HT} {Asus Rampage III Extreme} {Zotac AMP! + EVGA GTX 480 SLI @ 800/1600/1900} {G.SKILL Ripjaws 6GB DDR3 1605Mhz @ 7-8-7-20-1N} 
Hard DriveOptical DriveOSMonitor
{OCZ Agility 2 120GB, WD Black 1TB, F4 2TB RAID1} {Asus DVD+/- RW} {Windows 7 Ultimate 64 bit} {Asus VH242H 23.6" 1080p} 
KeyboardPowerCaseMouse
{Logitech G15} {Corsair HX 1000W} {CoolerMaster HAF-X} {Logitech G700} 
Mouse Pad
{Razer Goliathus - Speed} 
  hide details  
Reply
post #4 of 5
You're going to need to append all the names to a string variable.

Code:
this.returnStudents = function()
{
var namesOfStudents = "";
for(var i = 0; i < this.students.length; i++)
{
namesOfStudents += this.students[i].fname + " " + this.students[i].lname;
}
return namesOfStudents;
}
}
post #5 of 5
Depends what your function is trying to accomplish, if your just trying to get a list of the names and output that as a string i suppose you could just do roughly what floatingDivs said, perhaps add a newline after each first/last name.

Im not super familiar with web programming, but perhaps you could simply create an array of strings and return that, it would be a much cleaner approach and provide more functionality than one long string(depending on what your doing with the results).
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Simple Javascript issue