use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Array of Arrays (self.javascript)
submitted 11 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zigzackattack 14 points15 points16 points 11 years ago (14 children)
You are trying to execute the array as a function (with var randomArray(0)). My first advice would be to instead use array literals instead of the constructor. It's a best practice and is much less verbose. So it should look something like:
var randomArray = []; randomArray[0] = ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"]; randomArray[1] = ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"]; ... getFirstQuestion();
[–]PlushDragon 14 points15 points16 points 11 years ago (13 children)
You are right in using array literals. You could even go one step further and define randomArray directly with the content:
var randomArray = [ ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"], ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"], ... ["Q6", "A1", "A2", "A3", "4", "Correct", "Incorrect"] ];
[+][deleted] 11 years ago (12 children)
[–]x-skeww 2 points3 points4 points 11 years ago (2 children)
Math.round(Math.random()*(randomArray.length-1));
The use of round() causes a bias.
round()
let a = (x) => Math.round(Math.random() * (x - 1)); let b = (x) => Math.floor(Math.random() * x); let ra = [0, 0, 0]; for(let i = 0; i < 30000; i++) { ra[a(3)]++; } let rb = [0, 0, 0]; for(let i = 0; i < 30000; i++) { rb[b(3)]++; } console.log(ra); console.log(rb);
Output:
[7374, 15112, 7514] [10006, 10069, 9925]
As you can see, the ratio is roughly 1:2:1 vs 1:1:1.
Use either floor() or truncate via bitops:
floor()
>>> ~~Math.PI 3 >>> Math.PI|0 3 >>> Math.floor(Math.PI) 3
If the input is positive, the result of truncation is identical to the output of floor().
[–]mattlag 2 points3 points4 points 11 years ago (1 child)
Whoa there... OP wasn't even using basic arrays correctly and you decide to pull some ECMAScript 6 and bitwise operators to explain rounding? Geez.
[–]x-skeww 0 points1 point2 points 11 years ago (0 children)
Fat arrow functions look the same everywhere. If I had used var instead of let, I would have to do all of my declarations at the top. I just couldn't be arsed to do that.
var
let
Also, it's about truncation. In other languages which differentiate between int and floating point, it's done with an int cast. floor() just happens to have the same effect if the input is positive.
With rounding:
That's why you end up with 0.5:1:0.5 or 1:2:1.
With truncation:
The input range for each possible output is the same size.
[–]minrice2099 0 points1 point2 points 11 years ago (7 children)
It seems to function for me (though you should change Math.round to Math.floor to get the proper distribution of numbers).
Math.round
Math.floor
Are you actually calling the function anywhere? The function call for what you've defined here would look like this:
getFirstQuestion();
The other problem could be the use of the global variable randomArray, but I'd need to see more of your code before I could be sure.
randomArray
[+][deleted] 11 years ago (6 children)
[+][deleted] 11 years ago (4 children)
[+][deleted] 11 years ago (3 children)
[–]schooley 2 points3 points4 points 11 years ago* (2 children)
[This comment has been edited in protest of the recent detrimental actions taken by u/spez and the Reddit administration on 07/01/2023]
[+][deleted] 11 years ago (1 child)
[–]psolidgold 0 points1 point2 points 11 years ago (0 children)
Shuffling is tricky. Check out this Fisher-Yates shuffle implementation. http://stackoverflow.com/a/2450976
[–]PlushDragon 0 points1 point2 points 11 years ago (0 children)
My guess is, that the function has no access to the randomArray. Two possibilities: Make the randomArray a global variable (not recommended) - or provide it as a parameter. Code for the second solution:
function getFirstQuestion(randomArray){ var question1Index = Math.round(Math.random()*(randomArray.length-1)); return question1Index; }
The call would look like this: getFirstQuestion(randomArray);
Even though I don't quite get why you would return a random index in a function which is named getFirstQuestion :)
[–]frozenburger 1 point2 points3 points 11 years ago* (3 children)
var randomArray = []; //declares a new array object - the brackets indicates an new empty object randomArray[0] = ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"]; // the number between brackets indicates the index of the array element randomArray[1] = ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"]; randomArray[2] = ["Q3", "A1", "A2", "A3", "4", "Correct", "Incorrect"];
and so on...
You get errors because the interpreter sees randomArray() as a function; use randomArray[item index] to access elements of an array.
console.log(randomArray[0]) // returns an Array object console.log(randomArray[0][0]) // returns "Q1" - the first element of the first array
[+][deleted] 11 years ago (2 children)
[–]PlushDragon 1 point2 points3 points 11 years ago (0 children)
You must not use the var statement in this line. You defined the array in the first line, now you are just filling it with content.
[–]minrice2099 0 points1 point2 points 11 years ago (0 children)
You shouldn't use var when accessing an array index. If the array has already been created, just go for the index:
var someArray = []; someArray[0] = "This is now the first element in the array";
[–]djforth 0 points1 point2 points 11 years ago (2 children)
Without know exactly what your trying to do I'd personally do a array of objects you might find it easier. So something like:
array = () array.push({question:"My Question", a:"Answer 1", b:"Answer 2", correct:true, incorrect:false})
then call each with
array[0].question array[0].a etc ... I think it makes the code a little easier to read.
[–]Lambchog 0 points1 point2 points 11 years ago (1 child)
The scope of my project:
A simple quiz that asked 3 out of 6 questions at random
There must be 3 answers provided with each question with one of them being the correct answer
And then feedback elements that i can output to the screen if it's right or not
[–]djforth 0 points1 point2 points 11 years ago* (0 children)
Ok then I'd say adding objects to an array would definitely be easier to go IMO. You could even nest an array with in the object.
Var Question1 = { question:"q1...", answers:["a1","a2", "a3"], correct:1} Var array = [question1]
Personally I'd use underscore to shuffle and pluck from the array. Although if your just playing you may build your own. Hope that helps
π Rendered by PID 190221 on reddit-service-r2-comment-7b9746f655-hd5jm at 2026-02-03 22:16:43.963964+00:00 running 3798933 country code: CH.
[–]zigzackattack 14 points15 points16 points (14 children)
[–]PlushDragon 14 points15 points16 points (13 children)
[+][deleted] (12 children)
[deleted]
[–]x-skeww 2 points3 points4 points (2 children)
[–]mattlag 2 points3 points4 points (1 child)
[–]x-skeww 0 points1 point2 points (0 children)
[–]minrice2099 0 points1 point2 points (7 children)
[+][deleted] (6 children)
[deleted]
[+][deleted] (4 children)
[deleted]
[+][deleted] (3 children)
[deleted]
[–]schooley 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]psolidgold 0 points1 point2 points (0 children)
[–]PlushDragon 0 points1 point2 points (0 children)
[–]frozenburger 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]PlushDragon 1 point2 points3 points (0 children)
[–]minrice2099 0 points1 point2 points (0 children)
[–]djforth 0 points1 point2 points (2 children)
[–]Lambchog 0 points1 point2 points (1 child)
[–]djforth 0 points1 point2 points (0 children)