# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# JavaScriptHow to use a list?

#### giomach

##### New Coder
JavaScript:
``````function select (n, min, max) {
/*     Return n unique numbers selected randomly from the contiguous range min..max inclusive (n <= max-min+1)   */
/*  const numArray = [];  */
var temp;
for (var i = 1; i <= n; i++)
{  temp = getRndInteger (min, max);
/*  if contains (numArray, temp) { i-- }
else                         { numArray.push (temp) }     */
}
/*  return numArray  */
return 0;
}``````

Hi,
The lines commented out in the above function were intended to accumulate the unique generated numbers in a list and return that list.
What is the correct way to do this?
Many thanks.

Thanks, EkBass, but on re-checking W3Schools (arrays, functions) I still don't see what is wrong with my function. Just de-commenting "const numArray = [];" is enough to make the webpage fail, even if the function is never called.

I should have included the two other small functions called from "select". here they are.

Code:
``````function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) { return true; }
}
return false;
}
function select (n, min, max) {
/*     Return n unique numbers selected randomly from the contiguous range min..max inclusive (n <= max-min+1)   */
/*  const numArray = [];                                      */
var temp;
for (var i = 1; i <= n; i++)
{  temp = getRndInteger (min, max);
/*  if contains (numArray, temp) { i-- }
else                         { numArray.push (temp) }     */
}
/*  return numArray  */
return 0;
}``````

I think I've got this now. Pair of extra brackets needed around "contains (numArray, temp)".

It still doesn't work in the development environment I was using (HTMLPad 2010 — old!), but it works when run from a browser. So I need to upgrade.

This should be workable, but i assume this is where you have already ended.

Code:
``````function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}

function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) { return true; }
}
return false;
}

function select (n, min, max) {
const numArray = [];
var temp;
for (var i = 1; i <= n; i++)
{  temp = getRndInteger (min, max);
if (contains (numArray, temp)) { i-- }
else                         { numArray.push (temp) }
}
return numArray;
}``````

I appreciate your input, EkBass. I'm pleased that I wasn't too far off the mark in my inexperienced use of variable-length arrays, and that it was just a careless syntax error after all. It now runs fine in the trial version of HMTLPad2018.

This should be workable, but i assume this is where you have already ended.

Code:
``````function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}

function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) { return true; }
}
return false;
}

function select (n, min, max) {
const numArray = [];
var temp;
for (var i = 1; i <= n; i++)
{  temp = getRndInteger (min, max);
if (contains (numArray, temp)) { i-- }
else                         { numArray.push (temp) }
}
return numArray;
}``````
The indentation and bracketing here hurts my brain 😲

Modern music hurts my brains

Modern music hurts my brains
Too bad there is not a music-related forum here or we could have a nice discussion on this ! If you mean the endlessly whining drivel you hear in supermarkets, I'm fully with you.