I have an array, and need to pick three random values from that array. These will the be put in a new array and I will be able to see the new array on my website. I also have to make sure that no value gets picked twice.
This is what I have so far:
var student = ["Hans","Ole","Nils","Olav","Per","Knut","Line","Pia"];
var velg = student[Math.floor(Math.random() * student.length)];
I'm thinking I should add an id to my HTML, so the new array will show on my website, but I'm not sure about the rest.
First sort it randomly and then get first three:
student
.sort(function(){
return Math.random() - 0.5;
})
.slice(0,3)
Since Math.random() returns random value between 0 and 1, while sort expects values to be positive or negative to determine order we, we need to subtract 0.5 to make those negatives possible.
You could try something like this in a loop
var students = ["Hans","Ole","Nils","Olav","Per","Knut","Line","Pia"];
var randomStudents = [];
for(var i = 0; i < 3; i++) {
var velg = student[Math.floor(Math.random() * students.length)];
randomStudents.push(velg);
}
Note that this can add duplicate students to the array. You should check if student is already in the array and try again.
Keyword for that would be recursion.
https://www.codecademy.com/courses/javascript-lesson-205/0/1
Related
I’ve created a lotto number generator and am having trouble making sure no two sets of numbers come out the same. I want to first check that the next generated number doesn’t match a previous one in the array, if it does then generate a new number.
The code:
https://codesandbox.io/s/billowing-leaf-oqdt3?file=/index.html
You can do it step by step by replacing what you have line 46 by:
// Get a random number
var rnd = rand(high);
// If it is already in the array, get another
while (numList.includes(rnd)) {
rnd = rand(high);
}
// Now you know the value is unique, so you can add it to the list.
numList.unshift(rnd);
use a hash:
var numberHash = {}
var num1=generateNum(numberHash);
var num2=generateNum(numberHash);
function generateNum(numberHash) {
var num = rand();
while(numberHash[num]) {
num = rand();
}
numberHash[num]=true;
return num;
}
Hello everyone I am new here and I am just starting to learn javascript.
I need to come up with a piece off code that alerts a single random element from an array. I have managed to come up with the random element but it alerts all ten of them from the array. While I need t to alert a single one and stop.
This is what I have so far.
var myList = new Array("gusty", "long", "tremendous", "play", "white",
"literate", "baboon", "dusty", "government", "cheer");
for(i = 0; i < myList.length; i++)
{
var y = Math.floor(Math.random() * 10);
alert(myList[y]);
}
So when I load this in a browser I get ten random words. But I just want one. I tired putting the var inside the alert but that didn't do anything.
Just remove the for loop. Since you are referring to the element from array using an index (myList[y]), you don't require a for loop there.
var myList = new Array("gusty", "long", "tremendous", "play", "white",
"literate", "baboon", "dusty", "government", "cheer");
var y = Math.floor(Math.random() * 10);
alert(myList[y]);
You would generally use a for loop to repeat a certain task. So in your original code, you are generating 10 random numbers, and thus accessing 10 random items from the array. If you need just one, you can simply remove the loop.
If you want to alert just one, you've no need for a loop. The loop is causing a random value to be selected each time and alerted.
There are some other optimisations you can make.
//we don't normally use new Array() without good reason; use an array literal
//i.e. [1, 2, 3, 'foo'] instead
var myList = ["gusty", "long", "tremendous", "play", "white",
"literate", "baboon", "dusty", "government", "cheer"];
//remove the loop - and instead of hard-coding 10, let's read the actual length
var y = Math.floor(Math.random() * myList.length);
alert(myList[y]);
Here's a wider discussion on array literals vs. arrays created via the Array() constructor.
I am relatively new to programming and am having some issues with a project I am working on.
msg.newCG2 = [];
for(i=0;i<msg.newCG.length;i++){
for(j=0;j<msg.campaignGroup.length;i++){
if(msg.campaignGroup[j].col10 === msg.newCG[j]){
msg.groupTotals = msg.groupTotals + msg.campaignGroup[j].col11;
}
msg.newCG2.push(msg.newCG[i], msg.groupTotals)
}
}
Basically, for each one of the "IDs" (integers) in msg.newCG, I want to look for each ID in msg.campaignGroup and sum up the totals for all listings with the same ID, from msg.campaignGroup.col11 - then push the ID and the totals to a new array - msg.newCG2.
When I run the code, the first item sent through processes, but grinds to a halt because of memory. I assume this is because of an error in my code.
Where did this code go wrong? I am sure that there are better ways to do this as a whole, but I am curious where I went wrong.
There is a typo in your second for loop and the push needs to happen inside the outer loop.
msg.newCG2 = [];
for(i=0;i<msg.newCG.length;i++){
for(j=0;j<msg.campaignGroup.length;j++){
if(msg.campaignGroup[j].col10 === msg.newCG[i]){
msg.groupTotals = msg.groupTotals + msg.campaignGroup[j].col11;
}
}
msg.newCG2.push(msg.newCG[i], msg.groupTotals)
}
How about:
msg.newCG2 = [];
for (i=0; i < msg.newCG.length; i++) {
var groupTotal = 0;
for (j=0; j < msg.campaignGroup.length; j++) {
if (msg.campaignGroup[j].col10 === msg.newCG[i]){
groupTotal = groupTotal + msg.campaignGroup[j].col11
}
}
msg.newCG2.push(groupTotal)
}
Rather than looping 1.2M times, it would be more efficient to use a single-pass over the 4000 campaign groups, grouping by id to create an array of totals for all ids -- I like using the reduce() function for this:
var cgMap = msg.campaignGroups.reduce(function(arr, grp) {
var grpid = grp.col10;
var count = grp.col11;
var total = arr[grpid] || 0;
arr[grpid] = total + count;
},
[]);
I know, the reduce(...) function is not the easiest to grok, but it takes the second arg (the empty array) and passes it, along with each campaign group object in turn, to that inline function. The result should be a simple array of group totals (from col11), indexed by the group id (from col10).
Now, it's just a matter of returning the totals for those 300 ids found in msg.newCG -- and this map() function does that for us:
var cgOut = msg.newCG.map(function(gid) {
return cgMap[gid]; // lookup the total by group id
}
);
I've made some assumptions here, like the group ids are not terribly large integers, and are rather closely spaced (not too sparse). From the original code, I was not able to determine the format of the data you are wanting to return in msg.newCG2. The final push() function would append 2 integers onto the array -- the output group id and the total for that group. Having pairs of group ids and totals interleaved in a flat array is not a very useful data structure. Perhaps you meant to place the total value into an array, indexed by the group id? If so, you could re-write that line as:
msg.newCG2[msg.newCG[i]] = msg.groupTotals;
I have two arrays (representing a deck of cards), one filled with the values 1-52, and another filled with nothing. I want to take the position randomly from the first array and take that object and put it into the next spot available in the randomized array. I have this so far:
var shufdeck = new Array();
while(sufdeck.length < 52)
{
i = Math.floor(Math.random()*52);
//shufdeck[0] = deck[i] etc.
}
I'm not sure if I should be using splice or something else to take it out of the first array and put it into the second. If possible shufdeck[] should be filled and deck[] would be empty.
How can I shuffle an array?
Yes, you should splice out the chosen card, so:
var shuffledDeck = [];
while (deck.length) {
shuffledDeck.push(deck.splice(Math.random()*deck.length | 0, 1)[0]);
}
or to be a little more efficient:
var i = deck.length;
while (i) {
shuffledDeck.push(deck.splice(Math.random()*i-- | 0, 1)[0]);
}
Note that this will destroy deck. If you don't want that, copy it first:
var deckToShuffle = deck.slice();
Say I have an array in JS: var fruits = [apple,orange,banana]
I want to store the index of each fruit in variables such that at any point in time, if I add more stuff to the array, I will still know that the index of apple is X. So in this case, 0 is apple, but if I add something to the beginning of that away, the index of apple changes.
The more verbose way I can think of is to loop through the array
for (var i=0;i<fruits.length;i++) {
switch(fruits[i]) {
case:"apple"
var indexApple = i;
break;
//etc
}
}
Another way I can think of is use the value of the arrays as the variable name.
for (var i=0;i<fruits.length;i++) {
//psedo code
var 'index' + fruits[i] = i;
}
So in the end I'd have var indexApple = 0, indexOrange = 1, etc. THe key to the second method is to be able to create a dynamic variable by concatenating the string 'index' and the value of the array to create that variable. Not sure how to do that.
Note: Ideally I want the variables that store the index to be dynamically generated. Such that I only I can modify/add to the fruits array, and a new variable will be generated to store the index.
it seems like ensuring your the value of the index is legitimate will be difficult. i would include jquery and use the inArray method which returns the index of the item in the array.
function showIndexes() {
var appleIndex = $.inArray(fruits, "Apple"); //returns 0
var guavaIndex = $.inArray(fruits, "Guava"); //returns -1
fruits.unshift("Guava");
appleIndex = $.inArray(fruits, "Apple"); //returns 1
guavaIndex = $.inArray(fruits, "Guava"); //returns 0
}
The simplest solution is simply to build an Object which gives you nearly O(1) lookup time, and will scale with your array:
function LinkFruits(fruits) {
FruitLookup = {}
fruits.forEach((fruit,ind) => FruitLookup[fruit] = ind)
}
Now you can simply "lookup" your index from the FruitLookup table when needed like:
console.log("The index of apple is",FruitLookup.apple,"and for orange is",FruitLookup.orange)
Now if you modify your array you simply need to run LinkFruits(fruits).
Technical Note: If you want to fully automate this process you can look into Array.observe() which is now deprecated. Or overload the push and pop methods of this array to trigger the update before falling back to the default prototype methods.