/    Sign up×
Community /Pin to ProfileBookmark

Show 5 sequential numbers of a clicked number

Hi all,

I have a list of sequential numbers here. How can i show the 5 numbers infront and behind a clicked number?

e.g. When i clicked on the number 0, this is the situation:

5 4 3 2 1 [COLOR=”Red”]0[/COLOR] -1 -2 -3 -4 -5

So when i clicked on the number 2, it should show like this:

7 6 5 4 3 [COLOR=”Red”]2[/COLOR] 1 0 -1 -2 -3

Your prompt response is very much appreciated.

?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Gozzy82Jun 02.2010 — you can do this:

<i>
</i>function showNumber(nr){
return [nr+5,nr+4,nr+3,nr+2,nr+1,nr,nr-1,nr-2,nr-3,nr-4,nr-5];
}

alert(showNumber(2));
Copy linkTweet thisAlerts:
@SparoHawkJun 02.2010 — [B]Gozzy82[/B]'s example solve it nicely.

Here is function which you can modify to increase or decrease the amount of numbers in front or behind:

Pass [B]num[/B] as the variable:
[CODE]function createSequence(num)
{
var upper = num + 5;
var lower = num - 5;
var sequence = '';

for (var i = upper; i >= lower; i--)
{
sequence += i + (i > lower ? ', ' : '');
}

alert(sequence);
}[/CODE]
Copy linkTweet thisAlerts:
@cheanyauthorJun 03.2010 — Dear Gozzy82 and SparoHawk,

Thanks for your solutions ^^

Here is my complete code:

[CODE]
<script language="javascript">
jQuery(document).ready(function() {
listNumber(0);
})

function listNumber(val) {
val = parseInt(val);
var upper = val+5;
var lower = val-5;
var sequence = '';
for (var i = upper; i >= lower; i--)
{
sequence += "<span style='cursor:pointer' onclick='listNumber($(this).html());'>";
if (i==val) sequence += "<font weight='bold' color='#FF0000'>"
sequence += i + (i > lower ? ' ' : '');
if (i==val) sequence += "</font>"
sequence += "</span>";
}
$('#seq_no').html(sequence);
}
</script>

<div id="seq_no"></div>
[/CODE]


Thanks and have a nice day ?
×

Success!

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