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! While you're at it, check out some resources Treehouse students have shared here.
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
Edward Taylor
6,416 PointsChoosing a random value from an array and then saving that value to do other things to.
Ok I'm stuck and I think the awnser is stupidly basic
I have an array of proxies var proxies = [proxy1, proxy2, proxy3, proxy4,proxy5]
I then choose a random proxy using
var proxytouse = await proxies[Math.floor(Math.random() * proxies.length)];
Which chooses a random proxy to use in my script. Problem is I need to save the proxy that proxytouse chooses so that if it hits a captcha I can then remove it from my array and save that proxy to a database of proxies which needs checking.
it sounds simple but every time I call proxytouse it will always choose a random proxy
1 Answer
Steven Parker
243,656 PointsThe use of "await" is rather mysterious in this snippet, but for altering the array, you probably want to know the index value rather than the chosen value. So it makes sense to choose in 2 steps, and then the index can be directly used later:
var lastproxyindex = Math.floor(Math.random() * proxies.length);
var proxytouse = proxies[lastproxyindex];
For a more complete analysis and specific answer, show more of the code (particularly the promise and async function).