Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Attila Bongar
7,831 PointsQuestion about the Splice() method
Is it possible to pass "random" as the first argument to the splice method?
I would like to insert an item to a random location in the array.
Splice(random, 1, "O")
Possible?
5 Answers

Balázs Földesi
Full Stack JavaScript Techdegree Graduate 16,750 PointsYeah, i think that's possible what you want. I would do it this way:
var random = Math.floor(Math.random() * NUMBER); //It's depends on in what range you want a random number array.splice(random, 0, "0");

Attila Bongar
7,831 PointsI rushed my answer. I know what you mean, and makes alot of sense now. Thank you for your answer!!
Koszonom Balazs!

Attila Bongar
7,831 Pointsvar grid = document.getElementById('grid');
var random = Math.floor(Math.random() * Array.length);
var arr = Array(620).fill('O');
arr.splice(random, 1, "C");
grid.textContent = arr.join('');

Attila Bongar
7,831 PointsIt keeps putting the C at index zero. Not the random location we created with that var random. Not sure whats wrong.

Balázs Földesi
Full Stack JavaScript Techdegree Graduate 16,750 PointsAt first, with Array with capital A, you referencing to the Array global object, it's a reserved word in javascript. At my code switch the 'array' name to your array's variable name.

Attila Bongar
7,831 PointsGotcha. I actually just added a numeric value as the length (+1 of course).
In fact, since than i figured out my whole program. I got the loop going as well in every 3 seconds, it places the C to another location. Now that i got the basic principals down, i will dress it up and add more cool functionality. Im excited, i just created my first program! And largely thanks to your idea with the random. Im gonna paste the code for the program. Its based on the "find the C under one minute" eye test. But just to mess with people, i jump the C around.
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
</head>
<button onclick="setInterval(myFunction, 3000);">Find the "C"!!</button>
<body id="parent">
<div id="grid">
</div>
<script src="script.js"></script>
</body>
</html>
* {
box-sizing: border-box;
}
#parent {
display: flex;
justify-content: center;
align-items: center;
background: lightgray;
}
#grid {
background: black;
color: white;
width: 360px;
height: 360px;
resize: both;
word-break: break-all;
}
var grid = document.getElementById('grid');
function myFunction() {
var arr = Array(620).fill('O');
var random = Math.floor(Math.random() * 621);
arr.splice(random, 1, "C");
grid.textContent = arr.join('');
};

Balázs Földesi
Full Stack JavaScript Techdegree Graduate 16,750 PointsCongrats for you first program, good idea! :) Yeah, at first what matters is that it's working. Btw with Array(620).fill('O'); it's creates 620 items in the array, with index from 0 to 619, so 620 at the random number generator should be good. In your case arr.length would work there.

Attila Bongar
7,831 PointsThanks! your right!
Balázs Földesi
Full Stack JavaScript Techdegree Graduate 16,750 PointsBalázs Földesi
Full Stack JavaScript Techdegree Graduate 16,750 PointsIf you want to range the whole array, that line should do it: var random = Math.floor(Math.random() * array.length);