首页 > 解决方案 > 变量中数组的长度

问题描述

我的程序根据用户输入的最后一个字母选择一个数组,并选择所选数组的名称。我想找到与用户输入的单词的最后一个字母同名的 arrey 的长度。字母/数组选择有效,但长度无效。基本是一字足球的风格。

我试过这个但它不起作用:

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);

标签: javascript

解决方案


我只是了解您正在输入数组标识符,然后检查该数组的长度。如果我错了,请纠正我。

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 (this[lastChar].length);
  //OR
  console.log (window[lastChar].length)

var number = Math.floor(Math.random() * this[lastChar].length);

推荐阅读