/    Sign up×
Community /Pin to ProfileBookmark

Please could you check my array

[B]I’m trying make a new array to [U]store [/U]the combined points for each couple[/B]

[CODE]var contestantNamesArray = [‘Ady and Jo’, ‘Cat and Mat’, ‘Tess and Vern’, ‘Phil and Jaz’, ‘Kat and Alan’];

//points awarded by judges and audience stored in arrays
var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];
var combinedPointsArray = [contestantNamesArray + judgesPointsArray + audiencePointsArray];[/CODE]

Is this correct?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 04.2010 — No. Show us first how you expect your combined array to look like.
Copy linkTweet thisAlerts:
@AdytyperauthorMay 04.2010 — Well at first, i want to store the combined arrays by contestant couple.

Then further on, i want to add them up.
Copy linkTweet thisAlerts:
@AdytyperauthorMay 04.2010 — or is it?

[CODE]var combinedPointsArray = new[5];[/CODE]
Copy linkTweet thisAlerts:
@KorMay 04.2010 — Probably it would have been better to asked you [I]where and how[/I] you intend to use the data. Please, be more descriptive. Which is your final aim?

maybe you can group data in an object instead of an array.

A JavaScript object looks like:
<i>
</i>var myObject=[COLOR="Blue"]{[/COLOR]
[I]somename:property,
someothername:property[/I]
//and so on
[COLOR="Blue"]}[/COLOR]

See also JSON:

www.json.org.

You may use a constructor function to populate the object itself, or you may use the [B]prototype[/B] contructor for that... It depends on which is you final aim.
Copy linkTweet thisAlerts:
@KorMay 05.2010 — There are many versions to group those data. Here's simple variant:
<i>
</i>var CombinedPointsObject={};
for(var i=0; i&lt;contestantNamesArray.length; i++){
CombinedPointsObject[contestantNamesArray[i]]=judgesPointsArray[i]+audiencePointsArray[i]
}

Now you have an object, structured on pairs [I]name:value[/I]. If you need to know the combined points of a certain contestant, you may simply call it:
<i>
</i>alert(CombinedPointsObject['Tess and Vern'])

And look how it can be used, in this simple example:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
var contestantNamesArray = ['Ady and Jo', 'Cat and Mat', 'Tess and Vern', 'Phil and Jaz', 'Kat and Alan'];
//points awarded by judges and audience stored in arrays
var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];
var CombinedPointsObject={};
for(var i=0; i&lt;contestantNamesArray.length; i++){
CombinedPointsObject[contestantNamesArray[i]]=judgesPointsArray[i]+audiencePointsArray[i]
}
onload=function(){
var div=document.getElementById('mydiv');
var txt='';
for(names in CombinedPointsObject){
txt+=names+' have '+ CombinedPointsObject[names]+' combined points&lt;br&gt;';
}
div.innerHTML=txt;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="mydiv"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@AdytyperauthorMay 05.2010 — I don't want to use an object.
Copy linkTweet thisAlerts:
@KorMay 05.2010 — I don't want to use an object.[/QUOTE]
I don't want to get old and die either, but I will, some day... :rolleyes:

You asked for a solution, I gave you a solution. It is up to you to use it or not.
×

Success!

Help @Adytyper 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...