2
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

0

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Math.random() could return 1, in which case randomChoice() accesses a bad index.
@orip: MDN says Math.random cannot return 1: "Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range."
1
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

Comments

1

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

2 Comments

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.
:) :) yeap, that's because all the code in this answer is javascript :P not python :P I'm updating it
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.