View Full Version : function for duplicate numbers and output

05-07-2009, 04:58 PM

I'm new to javaScript ( a couple of weeks) and I'm just getting used to everything.

I have an exercise I'm trying to complete.

What's supposed to happen is when the program is run the user clicks on
"Select your Numbers!" their then asked for 5 numbers which they input and then the numbers are checked by a function called ”isAlreadySelected” for duplicates and if there is an alert is shown. When 5 numbers are entered correctly then they are shown by a window.alert.

Its done in to 2 functions

function isAlreadySelected
function selectNumbers

When I run the below code I'm prompted for the 5 numbers but it isn't checking for duplicates and my selection is undefined.


<SCRIPT language="JavaScript">

function isAlreadySelected(newSelection, previousSelections)

var newSelection = selectNumbers;
var selectNumbers = 0;
var previousSelections = new Array;

for (var index = 0; index < previousSelections.length; index = index +1)

if (previousSelections[index] == newSelection)


window.alert('Please make a new selection ',''); // Window alert, to pick another number



function selectNumbers(highNumber, numberToSelect)

var highNumber = 0;
var numberToSelect;
var numberToSelect = new Array(numberToSelect)

for (var number = 0; number < numberToSelect.length; number = number + 1)


numberToSelect[number] = parseFloat(window.prompt('Enter your numbers 1-10 ',''));
numberToSelect[number] = isAlreadySelected()
var selection = selectNumbers




<STRONG>A test of the functions <BR></STRONG>
<FORM NAME = "lotteryForm">

<INPUT TYPE = "button" NAME = "selectBalls" VALUE ="Select your Numbers!"
ONCLICK = "var selection = selectNumbers(10,5); window.alert('You selected: ' + selection);">


Thanks for any help

Philip M
05-07-2009, 06:53 PM
Your code has quite a few errors, not least the absence of input validation. Try this:-

<script type = "text/javascript">

var selected = new Array();
var count = 0;
var newNum;
var flag = 0;

function selectNumbers() {
for (var j = 0; j<5; j++) {
newNum = parseInt(window.prompt('Please make a new selection ','')); // prompt to pick another number
if (newNum < 0 || newNum >10 || !newNum || isNaN(newNum)) {
alert ("Invalid number - try again");
flag = 1;

for (var i = 0; i<=selected.length; i++) {
if (selected[i] == newNum) {
alert ("That number has already been selected - try again");
flag = 1;

if (flag == 0) {
selected[count] = newNum;

flag = 0;

alert ("The numbers you selected are: " + selected)


Note that <SCRIPT language="JavaScript"> is deprecated and obsolete. Use <script type = "text/javascript"> instead.

BTW, please follow the posting guidelines and wrap your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [CODE] and closing [/CODX] - change the X to E - tags . You can edit your previous post.

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

05-07-2009, 07:47 PM
Thanks for the reply,

I'll study your reply as it looks a bit technical for me at the moment, but I guess that the only way I will learn