Length of an array in a variable

My program selects an array based on the last letter of what the user enters and the name of the chosen array is selected. I want to find the length of the arrey that has the same name as the last leter of the word the user inputs. The letter/array selection works but the length doesn’t. basicli the style of an word foot ball.

I tried this but it doesn’t work:

var a = ['quasar','Quebec','quercetin']
var b = ['boron','broky','branka']
var c = ['cop','camera','controler','copito']
//i dint write all of the variables becous it woud be to long but his works as an example.
function hrat() {
  var input = document.getElementById("text").value;

  var lastChar = input.substr(input.length - 1);
  console.log (lastChar);
  console.log ((lastChar).length);
  var number = Math.floor(Math.random() * (lastChar).length);

Answer

I assume you want something like this.

const DATA = {
    search: {
    a: ['quasar','Quebec','quercetin'],
        b: ['boron','broky','branka'],
        c: ['cop','camera','controler','copito'],
  }
}

const getSearchEntryArray = (value) => {
    const {search} = DATA;
  const output = search[value];
  return output || [];
}

const getRandomIndex = (array) => {
    return array.length > 0 ? Math.floor(Math.random() * array.length) : -1 ;
}

const getOutput = (value) => {
    const resultArray = getSearchEntryArray(value);
  const index = getRandomIndex(resultArray);
  
  return resultArray[index];
}

const hrat = () => {
    event.preventDefault();
  
  const input_text = document.getElementById("text");
  
  const input = {
    _value: input_text.value,
    get lastChar() {
        return this._value.substr(this._value.length - 1);
    },
  };
  
  console.log(getOutput(input.lastChar));

  input_text.value = '';
}
<form onsubmit="hrat()">
  <input id="text">
  <button>Send</button>
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *