/    Sign up×
Community /Pin to ProfileBookmark

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), ”);
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 05.2009 — 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:
[code=php]var totalMembership = 0;
var totalVotes = 0;
var candidateArray = [230, 341, 196, 81, 257];
var candidateLen = candidateArray.length;

function calcVotes() {
var i = 0;
var votes = 0;

totalMembership = promptValidNumber("Please enter the number of members", "",
"Please enter a valid number of members");

for(i; i < candidateLen; i++) {
votes = promptValidNumber("enter votes cast for candidate " + (i+1), "",
"Please enter a valid number of votes.");
candidateArray[i] += votes;
}
}

function promptValidNumber(promptStr, promptVal, errorStr) {
var n = null;

promptVal = promptVal || "";
errorStr = errorStr || "Please enter a valid number.";

while ( isNaN(n = Number(prompt(promptStr, promptVal))) ) {
alert(errorStr);
}

return n;
}[/code]
Copy linkTweet thisAlerts:
@saintxxauthorFeb 05.2009 — 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>');
Copy linkTweet thisAlerts:
@toicontienFeb 05.2009 — Sounds like a class project?
Copy linkTweet thisAlerts:
@JMRKERFeb 05.2009 — 
  • 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*****
    [/QUOTE]
    you violate your original code with the use of

    a. window.prompt()

    b. parseFloat()

    c. document.write()

    Each of which is a function.
  • ×

    Success!

    Help @saintxx spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...