/    Sign up×
Community /Pin to ProfileBookmark

Reverse display format

The codes below are displaying the last 3 lines of .js file in the following format :

data3,title3,msg3
data4,title4,msg4
data5,title5,msg5

i.e. its displaying the data of last 3 lines

But, I want it in the following (reverse) format

data3,title5,msg5
data4,title4,msg4
data5,title3,msg3


———————————————————

Any help will be appreciated, thanks

Here is my code:
——————————————————-

<script language=”JavaScript” type=”text/javascript”><!–

for(var i=dataSet.length-3; i<dataSet.length; i++) {
for(var j=0; j<3; j++)
document.write(dataSet[i][j]+ “,”);
}

//–></script>
———————————————————


p.s. “dataSet” refers to an array in .js file. I am copying 2 sample lines of that

dataSet[dataSet.length]=new Array(“date4”, “title4″,”msg4”);
dataSet[dataSet.length]=new Array(“date5”, “title5″,”msg5”);

———————————————————-

Thanks

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 31.2004 — decrement j

Try this:

for(var i=dataSet.length-3; i<dataSet.length; i++) {

for(var j=2; j>-1; j--)

document.write(dataSet[i][j]+ ",");

}
Copy linkTweet thisAlerts:
@yasuruauthorMar 31.2004 — Thanks for ur reply KOR,

I tried the decrement thing, but it didnt solve the problem, bcoz its reversing the inner array whereas I want to reverse the Outer one.

In this code loop for "i" is for the lines (which is outer),

and loop for "j" is for the data in the lines (which is inner).

Now what I am looking for is "How to reverse the outer loop"


----------------------------------------------------------------
for(var i=dataSet.length-3; i<dataSet.length; i++) {

for(var j=0; j<3; j++)

document.write(dataSet[i][j]+ ",");
-----------------------------------------------------------------

Copy linkTweet thisAlerts:
@KorMar 31.2004 — Not very clear to me... Anyway, there is method reverse() which might help you

[i]array[/i].reverse()
Copy linkTweet thisAlerts:
@yasuruauthorMar 31.2004 — Well, let me make it clear,

as u see there are 2 for loops i and j, where, "i" is checking the lines and returning last 3 lines from .js file, whereas "j" is returning the data in those lines.

example:

3rd from lastline has: date1, title1, message1

2nd from lastline has: date2, title2, message2

1st from lastline has: date3, title3, message3


sample data of .js file:

dataSet[dataSet.length] = new Array("date1", "title1","message1");[/QUOTE]

in the same format data is getting added to this .js file. But I want only last 3 lines to dispaly where the LAST LINE should come first.

therefore the above example will look like this:

1st from lastline: date3, title3, message3

2nd from lastline: date2, title2, message2

3rd from lastline: date1, title1, message1
-----------------------------



Hope now I make it clear. thanks
Copy linkTweet thisAlerts:
@TageMar 31.2004 — Try this for loop instead...

[code=php]for(var i=dataSet.length-1; i>dataSet.length-4; i--){
for(var j=0; j<3; j++){
document.write(dataSet[i][j]+",")}}[/code]


Hope that helps, later...
Copy linkTweet thisAlerts:
@yasuruauthorApr 01.2004 — Thanks Tage, it worked. U solved my problem.
Copy linkTweet thisAlerts:
@TageApr 01.2004 — No problem, glad I could help!
×

Success!

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