-1

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 recognize list array query
var z = x[y]; // x = typeof return string 
var z = "house"[0]; // return h - return string - not object

//after make a default

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 double quotes - "house" - string to object - house - to convert to a var and before feed the system?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
carlos
  • 13
  • 5

3 Answers3

1

I'm not 100% sure what you're asking here, but the behaviour i believe you want can be accomplished using 2d arrays as such -

const t = [["red","blue","orange","black"], ["silver", "gold", "cooper","plastic"], ["marble","oak","yep","pine"], ["computer","mobile","mac","tablet"]]

const [rand1, rand2] = [Math.floor(Math.random() * t.length), Math.floor(Math.random() * t[0].length)]
console.log(t[rand1][rand2])
0

You could use the eval() function to obtain the object reference, but it could lead to hairy problems so it would better use another option like for example a switch statement:

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

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

var w = Math.floor(Math.random() * 3); // random type index (0 to 3)
var x = t[w]; // name of the random type
var y = Math.floor(Math.random() * 3); // random option (0 to 3)

switch (w) { // use the appropriate object based on type index
  case 0: z=house[y]; break;
  case 1: z=pen[y]; break;
  case 2: z=table[y]; break;
  case 3: z=electronic[y]; break;
}
console.log(w, z);
var z = z + ' ' + x // appending "house" to color
console.log(z);

document.getElementById("demo1").innerHTML=z;
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
Alberto Martinez
  • 2,620
  • 4
  • 25
  • 28
  • But doing so would be yucky and unnecessary-rather have an object key like "house" with its possible value array. Going the eval route, especially with new JS devs, is almost never the optimal solution. – Dave Newton Sep 25 '17 at 19:11
  • Why do you mean by 'undefined'? Random? – Alberto Martinez Sep 25 '17 at 20:07
  • The values are sorted by all. i wish to have a key for all elements. the switch is always defined, not sorted. i want reduce code giant defined code line by line to a simple function that could have 10 thousand of itens using any db. XML? offline DB? changeble? so if i set a name he will find one on 10 thousand itens. and use a little function to see this. The value house[y] will nowt make the precious x[y] undefined sort. – carlos Sep 25 '17 at 20:07
0

It's not quite clear to me if this is what you're looking for, but one solution might be to structure your data so that it's easier to get at:

const items = {
  house: ["red","blue","orange","black"],
  pen: ["silver", "gold", "cooper","plastic"],
  table:  ["marble","oak","yep","pine"],
  eletro: ["computer","mobile","mac","tablet"]
}

const randomChoice = list => list[Math.floor(list.length * Math.random())]

const randomObject = (items) => {
  const itemType = randomChoice(Object.keys(items))
  const modifier = randomChoice(items[itemType])
  return `${modifier} ${itemType}`
}

randomObject(items) //=> "marble table" or "plastic pen", etc.

Update

The comment asked to pick a random element of a certain type. This variation would allow for that:

const randomOfType = (items, itemType) => {
  const modifier = randomChoice(items[itemType])
  return `${modifier} ${itemType}`
}

const randomObject = (items) => randomOfType(items, randomChoice(Object.keys(items)))

randomOfType(items, 'table') //=> "oak table" or "marble table", etc.
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • have any chat here? – carlos Sep 25 '17 at 20:26
  • That was great, but must set what type of item preselect- i want a house so w=t[0]; – carlos Sep 26 '17 at 03:38
  • 1 The full code. Might you help me? ok, i was sick this days and "still" the same, and didn't avaliate, but study a lot your code and look your other posts and saw you have a definitive undestand about codes. I had working a lot with a code and you in 3 lines finished a lot of my problems. I want to show it to you, it's not already done, but could you correct this to me like you did soon?? – carlos Sep 30 '17 at 07:25
  • 2Here is another big troble. the problem is multiply the code to all elements i need. If i write everything that's work, but... if i do anything to increment the code system down and lose control. must have something to shrink that big mass. – carlos Sep 30 '17 at 07:25
  • 3 drag and drop. i wish to create a function for a lot of diferent elements and didn't found anything that can help me to multiply this. this was the best simple way to finish, but try to set another "target". That becames a mess. https://stackoverflow.com/questions/18425089/ it must work individualy and alone for each of than(div elements). – carlos Sep 30 '17 at 07:25
  • 4 You are PRO I a newbie on programing, trying to learn java to make interactive fast programs for my ideias. Can you help me giving any tips of where to find a concrete programation knowledge like you? it's rarely to see someone who know exactely what are doing. – carlos Sep 30 '17 at 07:27
  • To gain programming knowledge is like gaining any other knowledge: learn from people, from books, from websites, but most of all: practice! – Scott Sauyet Sep 30 '17 at 23:51
  • most of sites i went are incomplete. do you have some references of good books and sites? – carlos Oct 01 '17 at 22:57
  • look at this https://stackoverflow.com/questions/18425089/ . can't make a multiply div effect... – carlos Oct 01 '17 at 22:58
  • The best introductory book I know is [Eloquent JavaScript](http://eloquentjavascript.net/). The best reference I know is [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript). But always expect things to be incomplete. Use multiple sources. – Scott Sauyet Oct 02 '17 at 12:39
  • Thanks! so i wish another help please. Code is always giving the same value result on random, 3 sequence times... how could always take diferent values on random math? – carlos Oct 06 '17 at 03:17
  • @carlos: Just open another question. If you've shown real effort, someone will surely offer help. – Scott Sauyet Oct 06 '17 at 12:39