Random values - check values - if the same take a new valueWhat does ~~ (“double tilde”) do in Javascript?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?Setting “checked” for a checkbox with jQuery?How to check whether a checkbox is checked in jQuery?Checking if a key exists in a JavaScript object?How to check whether a string contains a substring in JavaScript?How to check for “undefined” in JavaScript?How to check if an object is an array?Open a URL in a new tab (and not a new window) using JavaScript
The meaning of "scale" in "because diversions scale so easily wealth becomes concentrated"
Did Captain America make out with his niece?
I was contacted by a private bank overseas to get my inheritance
What prevents ads from reading my password as I type it?
Find a text string in a file and output only the rest of the text that follows it?
Not been paid even after reminding the Treasurer; what should I do?
Do some languages mention the top limit of a range first?
Did silent film actors actually say their lines or did they simply improvise “dialogue” while being filmed?
Identify Batman without getting caught
Tile the chessboard with four-colored triominoes
Find only those folders that contain a File with the same name as the Folder
Prime too close to f in Garamond Math
London underground zone 1-2 train ticket
Traveling from Germany to other countries by train?
Which genus do I use for neutral expressions in German?
Pronouns when writing from the point of view of a robot
How to realistically deal with a shield user?
Why am I not getting stuck in the loop
What are the function of EM and EN spaces?
What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?
What is the probability of a biased coin coming up heads given that a liar is claiming that the coin came up heads?
Why is Chromosome 1 called Chromosome 1?
Make a living as a math programming freelancer?
Which pronoun to replace an infinitive?
Random values - check values - if the same take a new value
What does ~~ (“double tilde”) do in Javascript?Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?How do I check if an element is hidden in jQuery?How do I check if an array includes an object in JavaScript?Setting “checked” for a checkbox with jQuery?How to check whether a checkbox is checked in jQuery?Checking if a key exists in a JavaScript object?How to check whether a string contains a substring in JavaScript?How to check for “undefined” in JavaScript?How to check if an object is an array?Open a URL in a new tab (and not a new window) using JavaScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am making a user testimonial for my website. I am displaying 5 user testimonials on the homepage and these get pulled @random
from an array when the user loads the webpage. After the script takes 5 testimonials at random, it injects that into my HTML file.
This works all great, but I also want each value to be checked to make sure this value is not pulled already for one of the other 4 testimonials.
Then if 2 of the values are the same i would like the script to pull a other value of the array. Then it once again needs to check if the value that was pulled is not used in a other testimonial.
I am trying to find a way to do this as efficient as possible. I know how to do this if I am only pulling 2 values, but right now I am pulling 5 values and each needs to be checked with any other value that was pulled.
var user = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
Eventually what should happen is that there are 5 different values pulled from the array. None of the results should contain the same values.
javascript html
add a comment |
I am making a user testimonial for my website. I am displaying 5 user testimonials on the homepage and these get pulled @random
from an array when the user loads the webpage. After the script takes 5 testimonials at random, it injects that into my HTML file.
This works all great, but I also want each value to be checked to make sure this value is not pulled already for one of the other 4 testimonials.
Then if 2 of the values are the same i would like the script to pull a other value of the array. Then it once again needs to check if the value that was pulled is not used in a other testimonial.
I am trying to find a way to do this as efficient as possible. I know how to do this if I am only pulling 2 values, but right now I am pulling 5 values and each needs to be checked with any other value that was pulled.
var user = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
Eventually what should happen is that there are 5 different values pulled from the array. None of the results should contain the same values.
javascript html
1
I assume theuser
array has a length greater than5
outside the example you made, is that right?
– Shidersz
Mar 27 at 3:55
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Out of interest, may I ask what does double~~
do before parentheses?
– Masoud Keshavarz
Mar 27 at 4:47
5
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
1
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27
add a comment |
I am making a user testimonial for my website. I am displaying 5 user testimonials on the homepage and these get pulled @random
from an array when the user loads the webpage. After the script takes 5 testimonials at random, it injects that into my HTML file.
This works all great, but I also want each value to be checked to make sure this value is not pulled already for one of the other 4 testimonials.
Then if 2 of the values are the same i would like the script to pull a other value of the array. Then it once again needs to check if the value that was pulled is not used in a other testimonial.
I am trying to find a way to do this as efficient as possible. I know how to do this if I am only pulling 2 values, but right now I am pulling 5 values and each needs to be checked with any other value that was pulled.
var user = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
Eventually what should happen is that there are 5 different values pulled from the array. None of the results should contain the same values.
javascript html
I am making a user testimonial for my website. I am displaying 5 user testimonials on the homepage and these get pulled @random
from an array when the user loads the webpage. After the script takes 5 testimonials at random, it injects that into my HTML file.
This works all great, but I also want each value to be checked to make sure this value is not pulled already for one of the other 4 testimonials.
Then if 2 of the values are the same i would like the script to pull a other value of the array. Then it once again needs to check if the value that was pulled is not used in a other testimonial.
I am trying to find a way to do this as efficient as possible. I know how to do this if I am only pulling 2 values, but right now I am pulling 5 values and each needs to be checked with any other value that was pulled.
var user = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
Eventually what should happen is that there are 5 different values pulled from the array. None of the results should contain the same values.
javascript html
javascript html
edited May 27 at 11:22
double-beep
3,1586 gold badges19 silver badges33 bronze badges
3,1586 gold badges19 silver badges33 bronze badges
asked Mar 27 at 3:47
GyzimoGyzimo
397 bronze badges
397 bronze badges
1
I assume theuser
array has a length greater than5
outside the example you made, is that right?
– Shidersz
Mar 27 at 3:55
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Out of interest, may I ask what does double~~
do before parentheses?
– Masoud Keshavarz
Mar 27 at 4:47
5
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
1
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27
add a comment |
1
I assume theuser
array has a length greater than5
outside the example you made, is that right?
– Shidersz
Mar 27 at 3:55
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Out of interest, may I ask what does double~~
do before parentheses?
– Masoud Keshavarz
Mar 27 at 4:47
5
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
1
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27
1
1
I assume the
user
array has a length greater than 5
outside the example you made, is that right?– Shidersz
Mar 27 at 3:55
I assume the
user
array has a length greater than 5
outside the example you made, is that right?– Shidersz
Mar 27 at 3:55
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Out of interest, may I ask what does double
~~
do before parentheses?– Masoud Keshavarz
Mar 27 at 4:47
Out of interest, may I ask what does double
~~
do before parentheses?– Masoud Keshavarz
Mar 27 at 4:47
5
5
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
1
1
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27
add a comment |
3 Answers
3
active
oldest
votes
I think the most efficient way of doing this is just removing the testimonials that have been chosen from the pool of available choices.
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
If you want to avoid mutating the data, you can just add id
property to each entry and check the random testimonials's id
against an array of already selected testimonials and their corresponding id
s.
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
add a comment |
One solution is to create a shallow copy of the original array with Array.slice(), and later, every time you choice one new element, you delete that element from the copy using Array.splice() to ensure not using it again.
Example:
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
add a comment |
In order to keep users array intact, make a copy using users.slice(0);
Then, continue to generate a random number based on the usersCopy.length
until usersCopy
is empty. Call usersCopy.splice(random, 1)
to get a random user while removing it from the usersCopy
array.
Use document.getElementById
and a count variable to get the correct element and edit its html. The only issue here is you have more html elements than elements in your array. It's up to you how to handle that situation. You could repeat the first elements in this situation and if there are more array elements than HTML elements, you can stop your while loop if count
> number of HTML elements.
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369488%2frandom-values-check-values-if-the-same-take-a-new-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think the most efficient way of doing this is just removing the testimonials that have been chosen from the pool of available choices.
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
If you want to avoid mutating the data, you can just add id
property to each entry and check the random testimonials's id
against an array of already selected testimonials and their corresponding id
s.
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
add a comment |
I think the most efficient way of doing this is just removing the testimonials that have been chosen from the pool of available choices.
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
If you want to avoid mutating the data, you can just add id
property to each entry and check the random testimonials's id
against an array of already selected testimonials and their corresponding id
s.
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
add a comment |
I think the most efficient way of doing this is just removing the testimonials that have been chosen from the pool of available choices.
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
If you want to avoid mutating the data, you can just add id
property to each entry and check the random testimonials's id
against an array of already selected testimonials and their corresponding id
s.
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
I think the most efficient way of doing this is just removing the testimonials that have been chosen from the pool of available choices.
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
If you want to avoid mutating the data, you can just add id
property to each entry and check the random testimonials's id
against an array of already selected testimonials and their corresponding id
s.
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
const testimonials = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
]
function getTestimonial()
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
const data = [
id: 1,
name: 'Tom Levis',
body: 'Toms text'
,
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
,
id: 3,
name: 'Peter',
body: 'Peters text'
]
const selected = []
function getTestimonials2(testimonials)
while (true)
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length)
if (selected.indexOf(testimonials[index].id) === -1)
selected.push(testimonials[index].id)
return testimonials[index]
else
return undefined
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
answered Mar 27 at 4:12
James_FJames_F
3798 bronze badges
3798 bronze badges
add a comment |
add a comment |
One solution is to create a shallow copy of the original array with Array.slice(), and later, every time you choice one new element, you delete that element from the copy using Array.splice() to ensure not using it again.
Example:
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
add a comment |
One solution is to create a shallow copy of the original array with Array.slice(), and later, every time you choice one new element, you delete that element from the copy using Array.splice() to ensure not using it again.
Example:
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
add a comment |
One solution is to create a shallow copy of the original array with Array.slice(), and later, every time you choice one new element, you delete that element from the copy using Array.splice() to ensure not using it again.
Example:
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
One solution is to create a shallow copy of the original array with Array.slice(), and later, every time you choice one new element, you delete that element from the copy using Array.splice() to ensure not using it again.
Example:
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
var user = [
name: 'Tom Levis', body: 'Tom Levis text',
name: 'Jonah', body: 'Jonah text',
name: 'Peter', body: 'Peters text',
name: 'Albert', body: 'Albert text',
name: 'Susan', body: 'Susan text',
name: 'Dominic', body: 'Dominic text'
];
let userCopy = user.slice();
for (let i = 1; i <= 5; i++)
let rndIdx = ~~(Math.random() * userCopy.length);
let testimonial = document.getElementById('testimonial-' + i);
let user = userCopy[rndIdx];
testimonial.innerHTML = `$user.name<br><p id="testimonial-text">$user.body</p>`;
userCopy.splice(rndIdx, 1);
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
<p id="testimonial-4"></p>
<p id="testimonial-5"></p>
edited Mar 27 at 4:16
answered Mar 27 at 4:09
ShiderszShidersz
13.7k2 gold badges11 silver badges35 bronze badges
13.7k2 gold badges11 silver badges35 bronze badges
add a comment |
add a comment |
In order to keep users array intact, make a copy using users.slice(0);
Then, continue to generate a random number based on the usersCopy.length
until usersCopy
is empty. Call usersCopy.splice(random, 1)
to get a random user while removing it from the usersCopy
array.
Use document.getElementById
and a count variable to get the correct element and edit its html. The only issue here is you have more html elements than elements in your array. It's up to you how to handle that situation. You could repeat the first elements in this situation and if there are more array elements than HTML elements, you can stop your while loop if count
> number of HTML elements.
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
add a comment |
In order to keep users array intact, make a copy using users.slice(0);
Then, continue to generate a random number based on the usersCopy.length
until usersCopy
is empty. Call usersCopy.splice(random, 1)
to get a random user while removing it from the usersCopy
array.
Use document.getElementById
and a count variable to get the correct element and edit its html. The only issue here is you have more html elements than elements in your array. It's up to you how to handle that situation. You could repeat the first elements in this situation and if there are more array elements than HTML elements, you can stop your while loop if count
> number of HTML elements.
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
add a comment |
In order to keep users array intact, make a copy using users.slice(0);
Then, continue to generate a random number based on the usersCopy.length
until usersCopy
is empty. Call usersCopy.splice(random, 1)
to get a random user while removing it from the usersCopy
array.
Use document.getElementById
and a count variable to get the correct element and edit its html. The only issue here is you have more html elements than elements in your array. It's up to you how to handle that situation. You could repeat the first elements in this situation and if there are more array elements than HTML elements, you can stop your while loop if count
> number of HTML elements.
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
In order to keep users array intact, make a copy using users.slice(0);
Then, continue to generate a random number based on the usersCopy.length
until usersCopy
is empty. Call usersCopy.splice(random, 1)
to get a random user while removing it from the usersCopy
array.
Use document.getElementById
and a count variable to get the correct element and edit its html. The only issue here is you have more html elements than elements in your array. It's up to you how to handle that situation. You could repeat the first elements in this situation and if there are more array elements than HTML elements, you can stop your while loop if count
> number of HTML elements.
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
var users = [
name: 'Tom Levis',
body: 'Toms text'
,
name: 'Some Random Guy',
body: 'Some guys text'
,
name: 'Peter',
body: 'Peters text'
];
let usersCopy = users.slice(0);
let count = 0;
while (usersCopy.length)
count++;
let random = ~~(Math.random() * usersCopy.length);
let user = usersCopy.splice(random, 1)[0];
document.getElementById('testimonial-' + count).innerHTML = user.name + '<br>' + '<p id="testimonial-text">' + user.body + '</p>';
<html>
<body>
<p id="testimonial-1"></p>
<p id="testimonial-2"></p>
<p id="testimonial-3"></p>
</body>
</html>
edited Mar 27 at 4:45
answered Mar 27 at 4:04
kilkfoekilkfoe
3102 silver badges9 bronze badges
3102 silver badges9 bronze badges
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
add a comment |
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
Thanks for the help buddy. It indeed works as i would like to have it in the console. Yet i am not sure yet how to use it using the HTML file itself. The problem is for each testimonial i need to make a new line that inserts it. It has to do with the way Owl Carousel defines multiple entries. If i make it via 1 line it will see it as only 1 testimonial or it generates multiple with the same value. I placed it in a pastebin to show the html entry also. pastebin.com/Rf6kGgtW
– Gyzimo
Mar 27 at 4:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369488%2frandom-values-check-values-if-the-same-take-a-new-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I assume the
user
array has a length greater than5
outside the example you made, is that right?– Shidersz
Mar 27 at 3:55
Yes the array in teh actual code is much much longer. Eventually it needs to pull the info from the database so i dont have to manually enter the values. BUt i think the code of kilkfoe is exactly what i need :) Thanks for your time buddy.
– Gyzimo
Mar 27 at 4:11
Out of interest, may I ask what does double
~~
do before parentheses?– Masoud Keshavarz
Mar 27 at 4:47
5
Please mark one of the answers as the solution (to signify the question is solved, and to make the answer easier to find for future readers), instead of adding "SOLVED" to the title.
– JBallin
Mar 27 at 4:48
1
@MasoudKeshavarz It rounds the number (see this answer) by converting to binary format which only allows integers.
– FZs
Mar 27 at 5:27