Welcome!

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

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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.
 
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;
}
 
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) }     
    alert (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) }    
    alert (temp)
    }
  return numArray;
}
The indentation and bracketing here hurts my brain 😲
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom