How to translate this psuedo code to javascript

BKM Computers

New Member
Reaction score
0
Location
Hastings Point NSW Australia
Does anyone know how to read psuedo code? I'm having some trouble converting this psuedo code to javascript. it's just a simple problem called the birthday problem and i have done this problem before but last time done it was in VBScript and i'm not to sure how convert it to javascript.
the main trouble i'm having is in the for loop that is highlighted red. any help with this problem would be much appreciated.
:confused:

Begin Birthday Problem
Set Count to 0
For Group = 1 to 100
Finding Match in Single Group of Birthdays
If Match is true Then
Add 1 to Count
End If
End For
Output Count”%”
End

Begin Finding Match in Single Group of Birthdays
For Person = 1 to 23
Set Birthdays[Person] to random integer between 1 and 365
End For
Set Match to false
For PersonX = 1 to 22
For PersonY = PersonX + 1 to 23
If Birthdays[PersonX] = Birthdays[PersonY] Then
Set Match to true
End If
End For
End For


Thanks in advance
 
Does anyone know how to read psuedo code? I'm having some trouble converting this psuedo code to javascript. it's just a simple problem called the birthday problem and i have done this problem before but last time done it was in VBScript and i'm not to sure how convert it to javascript. [remainder of post snipped]

Homework assignment for a programming class?

If you did it previously in VBScript, do it the same way except use Javascript syntax.
 
....why is that pseudo code confusing me so much?

for each person in the array cycle through the array comparing values but then you would need to put in extra code to not count the compare the same person.
 
....why is that pseudo code confusing me so much?

for each person in the array cycle through the array comparing values but then you would need to put in extra code to not count the compare the same person.

You don't need any extra code because It checks for one match in a single group once if finds a match it exits the if block and goes on an does it all over again until it as looped 100 times. once it has done the problem 100 times it exits the loop and displays how many birthdays matched. the answer is around 50%.
 
Homework assignment for a programming class?

If you did it previously in VBScript, do it the same way except use Javascript syntax.

Yeah it is for an assignment but the code is not a part of it. I have to hand code a portfolio web site that outlines my skills. I want to add this but it is not necessary for me to do so. I just found the psuedo code in some of my old files from last year and i've tried to convert it to JS and i'm struggling with it and it's been driving me crazy for a few days now.
 
You don't need any extra code because It checks for one match in a single group once if finds a match it exits the if block and goes on an does it all over again until it as looped 100 times. once it has done the problem 100 times it exits the loop and displays how many birthdays matched. the answer is around 50%.

You lost me, it looks like it would always find a match because its using 1 array and checking to see if one value in the array matches any value in the array which it will everytime?
 
There's an old axiom that states if you get 23 people together, at least two of them will share the same birthday. The pseudo-code is laying out one way to see if there's any truth to the story.

The 2nd group of pseudo-code is meant to be used as a subroutine.
  • The first part of the subroutine simply assigns a random birthday (a day number from 1 to 365) to 23 different people.
  • The second part of the subroutine then does a sequential search to see if two people have the same birthday. (e.g. person 1 is compared to person 2, person 1 is compared to person 3, person 1 to person 4, ... person 1 to person 23, then person 2 to person 3, person 2 to person 4 ... finally person 22 to person 23). The subroutine then returns whether or not a match was found. (The pseudo-code is pretty inefficient in how the search is done, but that's a different discussion!)
The 1st group of pseudo-code simply calls the subroutine 100 times and displays the number of times a match was found (in essence, a percentage).

Pretty straight-forward.
 
Back
Top