/    Sign up×
Community /Pin to ProfileBookmark

about coding in javascript

Hi!!!

I have a doubt in scripting in one of the program in javascript.

Program:
Generate random 10 numbers from 1 to 10 such that 3,4 and 5 should not appear one after the another and vice versa.
it means the order should not be in trio and dual.
i mean to say it should not appear in the following ways:
3 4 5
4 3 5
5 3 4 and all the possible combinations.
3 4
4 5
5 3 and all the possible combinations.

The script which i have written has been attached to this mail.

Please give help me to solve this problem..

Thanks,
Jigar.

[upl-file uuid=fbd73d18-37fe-448e-919b-f08fdab1fe25 size=1kB]random numbers.txt[/upl-file]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherJan 19.2007 — Huh? If 3, 4 and 5 can't appear as pairs than they automatically can't appear as triplets. All you have to do is check whether both the previous number and the current number are in the set [3, 4, 5] and if so, discard the current number.
Copy linkTweet thisAlerts:
@jigarsarsenalauthorJan 19.2007 — Huh? If 3, 4 and 5 can't appear as pairs than they automatically can't appear as triplets. All you have to do is check whether both the previous number and the current number are in the set [3, 4, 5] and if so, discard the current number.[/QUOTE]


Hi!!!!

Thanks for ur suggestions....

Thanks,

Jigar.
Copy linkTweet thisAlerts:
@GilgamishJan 19.2007 — [CODE]
function getRndList()
{
var b = new Array();
var currentRndNo = -1
var previousRndNo = -1;


for(var i=0;i<10;i++)
{
var currentRndNo=Math.ceil(Math.random()*10);
if( ((currentRndNo-1)==previousRndNo ) | ((currentRndNo+1)==previousRndNo) )
{
i--
}
else
{
b[i] = currentRndNo
previousRndNo = currentRndNo
document.write(b[i] + "------->" + i +"<br>");
}

}

}
[/CODE]


is this what you are looking for?

Gilgamish
Copy linkTweet thisAlerts:
@A1ien51Jan 19.2007 — Way to go, do someones homework for them!!!

Eric
Copy linkTweet thisAlerts:
@GilgamishJan 19.2007 — Way to go, do someones homework for them!!!

Eric[/QUOTE]


lol. sorry i am a newbee... it will never happen again... !?

Gilgamish
Copy linkTweet thisAlerts:
@Mr_JJan 19.2007 — Here's one possiblity, I have not test it fully but it should give you the idea

[code=php]<HTML>
<HEAD>
<TITLE>Random Numbers</TITLE>

<script type="text/javascript">
<!--
function generate(){

temp=""
firstRun=true
selectedNums=new Array()
document.getElementById("display").innerHTML=temp

for(var i=0;i<10;i++){
chosen=false
rndnum=Math.ceil(Math.random()*10)

if(!firstRun){

for(var j=0;j<selectedNums.length;j++){
if(rndnum==selectedNums[j]){
chosen=true
i--
}
}

}

if(!chosen){
selectedNums[i]=rndnum
firstRun=false
temp+=rndnum
document.getElementById("display").innerHTML+=selectedNums[i]+" "
}

}

if(/34|35|345|45|453|543|54|53|43/.test(temp)){
generate()
}

}

onload=generate

// -->
</script>

</HEAD>
<BODY>
<h1>Random Numbers</h1>

<div id="display" style="font-size:40px"> </div>

</BODY>
</HTML>

[/code]
Copy linkTweet thisAlerts:
@KorJan 19.2007 — lol. sorry i am a newbee... it will never happen again... !?

Gilgamish[/QUOTE]

In this particular case I guess you did well. After all [B]jigarsarsenal[/B] tried to solve the problem and show us his code.

But, as an unwritten general rule of the Forum, we usually do not someone else's homework. We try to help him to solve his problem, but only if he show us that at least he had tried to do something, but he got stuck somewhere... ?

It is not the case now. [B]jigarsarsenal[/B] had a code of it's own. We suppose it is his code ?
×

Success!

Help @jigarsarsenal 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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