/    Sign up×
Community /Pin to ProfileBookmark

The "Sum" Of Certain Array Values…

Hello.

Quick question…how do you add the value of certain slots in an array.

For instance with this code:

[CODE]
function convertToString(){

var first_example={};
first_example = readCookie(first_example);

var first_pics = first_example[“whatever1”].toString();
var second_pics = first_example[“whatever2”].toString();
var third_pics = first_example[“whatever3”].toString();
var fourth_pics = first_example[“whatever4”].toString();

var first_array = first_pics.split(“,”);
var second_array = second_pics.split(“,”);
var third_array = third_pics.split(“,”);
var fourth_array = fourth_pics.split(“,”);

alert[COLOR=”Blue”](eval(first_array[1] + first_array[0]))[/COLOR];

}
[/CODE]

..I’m trying to get the sum of the part highlighted in blue…and I know that what I have is wrong…so how would I
accomplish this feat..?..

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 23.2009 — Not enough information from your example so this is just a guess ... ?

Assuming results of first_pics is a string something like: '1,2,3,4,5,6,7,8,9'

Check to make sure this is true with an alert(first_pics)

just after the assignment creation

Note, use of eval function is not the suggested way to do this.
Copy linkTweet thisAlerts:
@ZeroKilledFeb 23.2009 — apologies, no useful information
Copy linkTweet thisAlerts:
@ChuckBauthorFeb 23.2009 — 
Assuming results of first_pics is a string something like: '1,2,3,4,5,6,7,8,9'
[/QUOTE]


That's exactly right... now after I split by " , " I want to get the sum of first_pics[0] + first_pics[1]...and this is where the problem is...
Copy linkTweet thisAlerts:
@ZeroKilledFeb 23.2009 — there is no need to use [b]eval[/b] method and it may make the code more harder to maintain. so, since each element of the array are string, you have to convert them to integer before sum them. you can use either [b]Number[/b] or [b]parseInt[/b] function:
alert(Number(first_array[1]) + parseInt(first_array[0]));
Copy linkTweet thisAlerts:
@ChuckBauthorFeb 23.2009 — there is no need to use [b]eval[/b] method and it may make the code more harder to maintain. so, since each element of the array are string, you have to convert them to integer before sum them. you can use either [b]Number[/b] or [b]parseInt[/b] function:
alert(Number(first_array[1]) + parseInt(first_array[0]));[/QUOTE]


This is it...I almost wish I figured this out myself....I was close

instead I originally had [CODE]alert(Number(first_array[1]+first_array[0]));[/CODE]

...but thanks guys.
Copy linkTweet thisAlerts:
@JMRKERFeb 23.2009 — Be careful here ...

alert(Number(first_array[1]+first_array[0]));

...vs...

alert(Number(first_array[1])+Number(first_array[0])))

... or ...

var S = Number(first_array[1])+Number(first_array[0]);

alert(S);


String concantonate here so you might evaluate "1'+'2' as '12' and not display the number you may wish as a sum of '3' instead.
×

Success!

Help @ChuckB 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,
)...