• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How 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) }     */
    alert (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.
 

giomach

New Coder
W3Schools has some neat tutorials about this: Check out JavaScript Tutorial

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) }     */
    alert (temp)
    }
/*  return numArray  */
    return 0;
}
 

giomach

New Coder
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.
 

EkBass

King Coder
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) }     
    alert (temp)
    }
  return numArray;
}
 

giomach

New Coder
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.
 

cbreemer

King Coder
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) }    
    alert (temp)
    }
  return numArray;
}
The indentation and bracketing here hurts my brain 😲
 
Top