views:

65

answers:

4
octopusList = {"first": ["red", "white"],
            "second": ["green", "blue", "red"],
            "third": ["green", "blue", "red"]}
squidList = ["first", "second", "third"]

for i in range(1):
    squid = random.choice(squidList)
    octopus = random.choice(octopusList[squid])

print squid + " " + octopus

Can anyone help me write this in JavaScript? I've got most of my program written into JavaScript but specifically how to get a list with lists in it written in JavaScript has me puzzled. I'm new to programming in general, so thanks for putting up with my questions. :D

+4  A: 

First of all, I'd like to say that that for i in range(1): line is useless. It'll only execute the contents once, and you're not using i.

Anyway, the code you posted should work fine with a few tweaks in JavaScript. First you'll need to reimplement random.choice. You could use this:

function randomChoice(list) {
    return list[Math.floor(Math.random()*list.length)];
}

Now after that, it's simple:

var octopusList = {
    "first": ["red", "white"],
    "second": ["green", "blue", "red"],
    "third": ["green", "blue", "red"]
};
var squidList = ["first", "second", "third"];

var squid = randomChoice(squidList);
var octopus = randomChoice(octopusList[squid]);

// You could use alert instead of console.log if you want.
console.log(squid + " " + octopus);
icktoofay
+1  A: 
js> octopusList = {"first": ["red", "white"],
               "second": ["green", "blue", "red"],
               "third": ["green", "blue", "red"]}

js> squidList = ["first", "second", "third"]
first,second,third

js> squid = squidList[Math.floor(Math.random() * squidList.length)]
third

js> oct_squid = octopusList[squid]
green,blue,red

js> octopus = oct_squid[Math.floor(Math.random() * oct_squid.length)]
blue
ars
+1  A: 

...specifically how to get a list with lists in it written in Javascript has me puzzled.

You can ( also ) create a list in with list in it in JavaScript like this:

var listWithList = [["a,b,c"],["d,"e","f"], ["h","i","j"]]

Because when you code in JavaScript

o = { "first" : ["red","green"],
      "second": ["blue","white"]}

You're actually creating a JavaScript object with two properties first and second whose values are a list ( or array ) with to elements each. This works just fine as you can see in icktoofay answer

Since that's a JavaScript object you could use this syntax to retrieve them

listOne = o.first;
listTwo = o.second;
OscarRyz
It's worth noting that this answer is oriented towards if it's already translated to JavaScript. The `{"a": "b", ...}` syntax in Python specifies a dictionary, not just a plain object. Also, to access elements in Python, you'd use `myList["first"]` and `myList["second"]`, etc. Finally, that list of lists code should work in Python, if you remove the `var` part.
icktoofay
:) :) yeap, that's because all the code in this answer is javascript :P not python :P I'm updating it
OscarRyz
+1  A: 

Consider using the json module to translate data structures from Python to JSON format (which is valid Javascript) -- and viceversa, if you ever need to. For example:

>>> octopusList = {"first": ["red", "white"],
...             "second": ["green", "blue", "red"],
...             "third": ["green", "blue", "red"]}
>>> print json.dumps(octopusList)
{"second": ["green", "blue", "red"], "third": ["green", "blue", "red"], "first": ["red", "white"]}
>>> 

As you see, in this case the "translation" is just about an identity (the change of ordering in the dictionary entries [[in Python]] / object attributes [[in Javascript]] is irrelevant, as neither Python's dicts nor JS's objects have any concept of "ordering";-).

Alex Martelli