www.webdeveloper.com
Results 1 to 5 of 5

Thread: help me on arrays-dodgy

  1. #1
    Join Date
    Feb 2009
    Posts
    2

    help me on arrays-dodgy

    I'm not a novice so I don't need tutorial. All I want to know is (based on the code below), how would I add the inputs(in for loop) to the candidateArray elements (e.g. input of 70 for first prompt would result in 300).ANY HELP WOULD BE MOST APPRECIATIVE



    var totalMembership;
    var totalVotes;
    var candidateArray = new Array(230, 341, 196, 81, 257);
    totalMembership = window.prompt ('Please enter the number of members', '');
    totalMembership = parseFloat(totalMembership);
    totalVotes = 0;

    for(var i = 0; i < candidateArray.length; i = i + 1) //a simple counter loop
    {
    candidateArray[i] = window.prompt('enter votes cast for candidate ' + (i + 1), '');
    }

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    The window.prompt method returns a string when the user enters something, or null if the user enters nothing. You should also check that the number entered is a valid number. Try this code below:
    PHP Code:
    var totalMembership 0;
    var 
    totalVotes 0;
    var 
    candidateArray = [23034119681257];
    var 
    candidateLen candidateArray.length;

    function 
    calcVotes() {
      var 
    0;
      var 
    votes 0;
      
      
    totalMembership promptValidNumber("Please enter the number of members""",
          
    "Please enter a valid number of members");
      
      for(
    icandidateLeni++) {
        
    votes promptValidNumber("enter votes cast for candidate " + (i+1), "",
            
    "Please enter a valid number of votes.");
        
    candidateArray[i] += votes;
      }
    }

    function 
    promptValidNumber(promptStrpromptValerrorStr) {
      var 
    null;
      
      
    promptVal promptVal || "";
      
    errorStr errorStr || "Please enter a valid number.";
      
      while ( 
    isNaN(Number(prompt(promptStrpromptVal))) ) {
        
    alert(errorStr);
      }
      
      return 
    n;


  3. #3
    Join Date
    Feb 2009
    Posts
    2

    without functions...sorry

    This code needs to be without functions, and I only want to know what is wrong with it and maybe 1 or two lines corrected.

    I want to add what i type in the prompt to each element, so for example, 70 in the first prompt would make the first element in the array 300.
    *****NO FUNCTIONS*****


    var totalMembership;
    var totalVotes = 0;
    var candidateArray = [230, 341, 196, 81, 257];
    var votes = 0;
    totalMembership = window.prompt('Please enter the number of members', '');

    for(var i = 0; i < candidateArray.length; i = i + 1)
    {
    var votes = window.prompt('enter votes cast for candidate ' + (i + 1), '');
    votes = parseFloat(votes);
    }
    document.write(candidateArray[i] + '<BR>');

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    1. You need to MOVE the last '}' character to the VERY last line as it
    does not include the display inside the loop.
    2. You need to add the prompted input to the current contents of the
    candidateArray if you want to see 230+70 to equal 300, otherwise only
    original value will be displayed.
    3. Finally with your statement
    *****NO FUNCTIONS*****
    you violate your original code with the use of
    a. window.prompt()
    b. parseFloat()
    c. document.write()
    Each of which is a function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles