Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with for loop

    I'm trying to loop through 2 arrays and display the largest number and also a name which are both indexed the same. I can't seem to get the loop working properly. If any1 could help, it would be really apreciated. Heres my code.

    var markNum = prompt("Enter the number of student marks", "Enter a number");
    var i;
    var b;
    var c;
    var inputNames;
    var inputMarks;
    var names = new Array();
    var marks = new Array();
    var largeMark;

    for (i = 0; i < parseInt(markNum); i++)
    {
    inputNames = prompt("Enter the names of the sudents");
    names[names.length] = inputNames;
    }

    for (b = 0; b < parseInt(markNum); b++)
    {
    inputMarks = parseInt(prompt("Enter the mark for " + names[b]));
    marks[marks.length] = inputMarks;
    }

    for (c = 0; c < marks.length; c++)
    {
    var d = 0;
    largeMark = marks[0];
    while (d < marks.length)
    {
    if (largeMark < marks[d])
    {
    largeMark = marks[d];
    }
    d++;
    }
    document.write(names[c] + " has the highest mark. The mark is " + largeMark + "%." + "<br>");
    }


  • #2
    New Coder
    Join Date
    Apr 2007
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up solution

    i've made some modifications in ur code... its now working right...
    i've given some statements where i've made changes...
    go through the code. u can copy and paste in ur code.....


    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    <script language="JavaScript">
    var markNum = prompt("Enter the number of student marks", "Enter a number");
    var i;
    var b;
    var c;
    var inputNames;
    var inputMarks;
    var names = new Array();
    var marks = new Array();
    var largeMark;
    var pos; //take a new variable to find the index of array for highest marks
    for (i = 0; i < parseInt(markNum); i++)
    {
    inputNames = prompt("Enter the names of the sudents");
    names[names.length] = inputNames;
    }
    for (b = 0; b < parseInt(markNum);b++)
    {
    inputMarks = parseInt(prompt("Enter the mark for " + names[b]));
    marks[marks.length] = inputMarks;
    }
    for (c = 0; c < marks.length; c++)
    {
    var d = 0;
    largeMark = marks[0];
    while (d < marks.length)
    {
    if (largeMark < marks[d])
    {
    largeMark = marks[d];
    pos=d; //assign the value of d to 'pos' at which we get largeMark.
    }
    d++;
    }
    }
    //place this statement outside of for loop and replace 'names[c]' with 'names[pos]'
    document.write(names[pos] + " has the highest mark. The mark is " + largeMark + "%." + "");

    </script>
    </HEAD>
    <BODY>

    <P>&nbsp;</P>

    </BODY>
    </HTML>

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Alternative solution using a 2D array and the Array.sort function.
    Code:
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    <script language="JavaScript">
    var markNum = parseInt(prompt("Enter the number of student marks", "Enter a number"));
    var i;
    var b;
    var inputNames;
    var inputMarks;
    var marks = new Array();
    
    for (i = 0; i < markNum; i++)
    {
      inputNames = prompt("Enter the names of the students","");
      marks[marks.length] = new Array();
      marks[marks.length-1][0] = inputNames;
    }
    for (b = 0; b < markNum;b++)
    {
      inputMarks = parseInt(prompt("Enter the mark for " + marks[b][0], ""));
      marks[b][1] = inputMarks;
    }
    
    function sortFunc(a, b){ //reverse sort
      return b[1] - a[1];
    }
    
    marks.sort(sortFunc);
    
    //the highest mark is in the first element of the array
    document.write(marks[0][0] + " has the highest mark. The mark is " + marks[0][1] + "%." + "");
    
    </script>
    </HEAD>
    <BODY>
    
    <P>&nbsp;</P>
    
    </BODY>
    </HTML>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    May 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys! Yeah, I thought about using multi-d array but I'm just not very familiar with them just yet.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •