How to get an array of numbers from one html input

Hello all
Here is my code:

// let numb = document.querySelector('#inPut11');
// let numbVal = new Array (numb.value);
  let numbVal = [15,9];

  function findArrGcd (arr) {
    return arr.reduce(function gcd(x,y) {
      return y === 0 ? x: gcd(y, x % y);
  outPut.textContent = findArrGcd(numbVal);

there is an array numbVal but I want to operate on an array which’s got from html input instead, I did that too but it displays the whole input value as they are entered.

From the input you get a string, you could use split to divide the string in an array of number using non numbers characters as argument of split (str.split(/[^0-9]+/) or something of this kind, didn’t check if this can work) and so you have an array of numbers! Well, maybe you also need to convert the strings to numbers but you are there

You maybe also need to consider for the case in which you have other characters before the first number or after the last number

1 Like

This worked fine thanks a bunch

Why would you need to operate on an array from the html input? If you have already created the array, just use the array created from calling findArrGcd.

1 Like

Dear RandellDawson, I am working on a side project for my portfolio, that’s why I need to operate on an array of numbers got from html input,

If you are referring to the “Find out the Greatest Common Divisor…”, the list entered by the user does not have to be an array. It could just numbers separated by commas and/or spaces. In your JavaScript, you would convert the comma/space delimited string to an array of numbers, then use that in your code.

// assuming you have an input element with id="list"
const list = str.split(/(\s*,\s*|\s*)/g);  // creates an array of string values (which are numbers)
// do something with your list array.
1 Like

thank you so much, yes that true