lundi 25 septembre 2017

Javascript string to object error? random array var not aloowed

I want create a array from array by random, but i'm starting on javascript. Here is my question.

//array
var t = ["house","pen","table","eletronic"];

//-> selected a name option 0

var w = t[0]; // selected

var x = w;
var y = 0 to 3; // random

var house =["red","blue","orange","black"];
var pen   =["silver", "gold", "cooper","plastic"];
var table =["marble","oak","yep","pine"];
var eletro=["computer","mobile","mac","tablet"];


// what i wish
var z = house[0]; // return red  // x = typeof return object

//x this is the error type string not reconize list array querry
var z = x[y]; // x = typeof return string 
var z = "house"[0]; // return h - return string - not object

//after make a defaut

var a = x[y]; //y != y
var b = x[y]; //y != y

document.getElementById("demo1").innerHTML=z; // blue house;
document.getElementById("demo2").innerHTML=a; // silver pen;
document.getElementById("demo3").innerHTML=b; // marble table;
<p id "demo1"></p>
<p id "demo2"></p>
<p id "demo3"></p>

I think i must convert doble quotes - "house" - string to object - house - to convert to a var and before feed the system?




Aucun commentaire:

Enregistrer un commentaire