Javascript - make function return unique result for every div
I need this function to run on a variable amount of divs and return a unique result for each div.
class Sentence {
constructor(na, num, col) {
this.name = na;
this.number = num;
this.color = col;
}
makesentence() {
$('div').html(this.name + " is " + this.number + " years old and loves " + this.color);
}
}
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
newsentence.makesentence();
So, if my HTML is 3 empty divs:
<div></div>
<div></div>
<div></div>
I would like it to return something like:
lisa is 67 years old and loves red
zach is 56 years old and loves magenta
kelly is 27 years old and loves orange
But now it just returns something like:
lisa is 67 years old and loves red
lisa is 67 years old and loves red
lisa is 67 years old and loves red
javascript jquery function
add a comment |
I need this function to run on a variable amount of divs and return a unique result for each div.
class Sentence {
constructor(na, num, col) {
this.name = na;
this.number = num;
this.color = col;
}
makesentence() {
$('div').html(this.name + " is " + this.number + " years old and loves " + this.color);
}
}
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
newsentence.makesentence();
So, if my HTML is 3 empty divs:
<div></div>
<div></div>
<div></div>
I would like it to return something like:
lisa is 67 years old and loves red
zach is 56 years old and loves magenta
kelly is 27 years old and loves orange
But now it just returns something like:
lisa is 67 years old and loves red
lisa is 67 years old and loves red
lisa is 67 years old and loves red
javascript jquery function
3
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to callMath.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.
– Chris G
Jan 3 at 0:16
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29
add a comment |
I need this function to run on a variable amount of divs and return a unique result for each div.
class Sentence {
constructor(na, num, col) {
this.name = na;
this.number = num;
this.color = col;
}
makesentence() {
$('div').html(this.name + " is " + this.number + " years old and loves " + this.color);
}
}
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
newsentence.makesentence();
So, if my HTML is 3 empty divs:
<div></div>
<div></div>
<div></div>
I would like it to return something like:
lisa is 67 years old and loves red
zach is 56 years old and loves magenta
kelly is 27 years old and loves orange
But now it just returns something like:
lisa is 67 years old and loves red
lisa is 67 years old and loves red
lisa is 67 years old and loves red
javascript jquery function
I need this function to run on a variable amount of divs and return a unique result for each div.
class Sentence {
constructor(na, num, col) {
this.name = na;
this.number = num;
this.color = col;
}
makesentence() {
$('div').html(this.name + " is " + this.number + " years old and loves " + this.color);
}
}
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
newsentence.makesentence();
So, if my HTML is 3 empty divs:
<div></div>
<div></div>
<div></div>
I would like it to return something like:
lisa is 67 years old and loves red
zach is 56 years old and loves magenta
kelly is 27 years old and loves orange
But now it just returns something like:
lisa is 67 years old and loves red
lisa is 67 years old and loves red
lisa is 67 years old and loves red
javascript jquery function
javascript jquery function
asked Jan 3 at 0:10
website walruswebsite walrus
114
114
3
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to callMath.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.
– Chris G
Jan 3 at 0:16
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29
add a comment |
3
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to callMath.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.
– Chris G
Jan 3 at 0:16
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29
3
3
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to call
Math.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.– Chris G
Jan 3 at 0:16
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to call
Math.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.– Chris G
Jan 3 at 0:16
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29
add a comment |
2 Answers
2
active
oldest
votes
If I understand your question correctly, then this can be achieved quite easily by reogranising your code around the iteration over <div>
elements in your document.
As a stratergy to gurantee unique results for each div, you could shuffle your input data like so:
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
And then iterate over each <div>
, populating the contents of each sequentially via each element's index
in this way:
$('div').each(function(index) {
var name = names[index];
var color = colors[index];
var number = ages[index];
$(this).html(name + " is " + number + " years old and loves " + color);
});
The idea here is to first shuffle your input data (ie to achieve the randomised result) followed by ordered access to that data when generating your sentences, which ensures uniqueness between sentenses in the final result.
Here's a working snippet showing the two ideas in action:
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
add a comment |
I am wondering why the answers have to be so complicated when a simple each loop thru each div would do the work just fine.
$.each($(".div"), function(){
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
$(this).html(newsentence.makesentence());
});
You can add a condition if you want the result to be even more unique as you mentioned.
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
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%2f54014808%2fjavascript-make-function-return-unique-result-for-every-div%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand your question correctly, then this can be achieved quite easily by reogranising your code around the iteration over <div>
elements in your document.
As a stratergy to gurantee unique results for each div, you could shuffle your input data like so:
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
And then iterate over each <div>
, populating the contents of each sequentially via each element's index
in this way:
$('div').each(function(index) {
var name = names[index];
var color = colors[index];
var number = ages[index];
$(this).html(name + " is " + number + " years old and loves " + color);
});
The idea here is to first shuffle your input data (ie to achieve the randomised result) followed by ordered access to that data when generating your sentences, which ensures uniqueness between sentenses in the final result.
Here's a working snippet showing the two ideas in action:
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
add a comment |
If I understand your question correctly, then this can be achieved quite easily by reogranising your code around the iteration over <div>
elements in your document.
As a stratergy to gurantee unique results for each div, you could shuffle your input data like so:
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
And then iterate over each <div>
, populating the contents of each sequentially via each element's index
in this way:
$('div').each(function(index) {
var name = names[index];
var color = colors[index];
var number = ages[index];
$(this).html(name + " is " + number + " years old and loves " + color);
});
The idea here is to first shuffle your input data (ie to achieve the randomised result) followed by ordered access to that data when generating your sentences, which ensures uniqueness between sentenses in the final result.
Here's a working snippet showing the two ideas in action:
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
add a comment |
If I understand your question correctly, then this can be achieved quite easily by reogranising your code around the iteration over <div>
elements in your document.
As a stratergy to gurantee unique results for each div, you could shuffle your input data like so:
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
And then iterate over each <div>
, populating the contents of each sequentially via each element's index
in this way:
$('div').each(function(index) {
var name = names[index];
var color = colors[index];
var number = ages[index];
$(this).html(name + " is " + number + " years old and loves " + color);
});
The idea here is to first shuffle your input data (ie to achieve the randomised result) followed by ordered access to that data when generating your sentences, which ensures uniqueness between sentenses in the final result.
Here's a working snippet showing the two ideas in action:
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
If I understand your question correctly, then this can be achieved quite easily by reogranising your code around the iteration over <div>
elements in your document.
As a stratergy to gurantee unique results for each div, you could shuffle your input data like so:
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
And then iterate over each <div>
, populating the contents of each sequentially via each element's index
in this way:
$('div').each(function(index) {
var name = names[index];
var color = colors[index];
var number = ages[index];
$(this).html(name + " is " + number + " years old and loves " + color);
});
The idea here is to first shuffle your input data (ie to achieve the randomised result) followed by ordered access to that data when generating your sentences, which ensures uniqueness between sentenses in the final result.
Here's a working snippet showing the two ideas in action:
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
// Declare input data to be used for sentense generation.
// Also declare age data array
var names = ['zach', 'kelly', 'lisa', 'slater'];
var colors = ['red', 'orange', 'blue', 'magenta'];
var ages = new Array(100).fill(0).map((value, index) => index + 1);
// Shuffle the input data to yield a randomized result
names = names.sort(function() { return Math.random() - 0.5; });
colors = colors.sort(function() { return Math.random() - 0.5; });
ages = ages.sort(function() { return Math.random() - 0.5; });
// Iterate over each div in the document
$('div').each(function(index) {
// Get data from each array by current div's index
var name = names[index];
var color = colors[index];
var number = ages[index];
// For the current div, update the html content as before
$(this).html(name + " is " + number + " years old and loves " + color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
edited Jan 3 at 0:45
answered Jan 3 at 0:21
Dacre DennyDacre Denny
13.9k41233
13.9k41233
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
add a comment |
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
1
1
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
This answer is missing the "unique" part.
– Chris G
Jan 3 at 0:28
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
@ChrisG ah forgot that - thanks for spotting my error :)
– Dacre Denny
Jan 3 at 0:40
add a comment |
I am wondering why the answers have to be so complicated when a simple each loop thru each div would do the work just fine.
$.each($(".div"), function(){
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
$(this).html(newsentence.makesentence());
});
You can add a condition if you want the result to be even more unique as you mentioned.
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
add a comment |
I am wondering why the answers have to be so complicated when a simple each loop thru each div would do the work just fine.
$.each($(".div"), function(){
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
$(this).html(newsentence.makesentence());
});
You can add a condition if you want the result to be even more unique as you mentioned.
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
add a comment |
I am wondering why the answers have to be so complicated when a simple each loop thru each div would do the work just fine.
$.each($(".div"), function(){
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
$(this).html(newsentence.makesentence());
});
You can add a condition if you want the result to be even more unique as you mentioned.
I am wondering why the answers have to be so complicated when a simple each loop thru each div would do the work just fine.
$.each($(".div"), function(){
var names = ['zach', 'kelly', 'lisa', 'slater'],
name = names[Math.floor(Math.random() * names.length)],
colors = ['red', 'orange', 'blue', 'magenta'],
color = colors[Math.floor(Math.random() * colors.length)],
number = Math.floor((Math.random() * 95) + 1);
var newsentence = new Sentence(name, number, color);
$(this).html(newsentence.makesentence());
});
You can add a condition if you want the result to be even more unique as you mentioned.
answered Jan 3 at 1:08
iVoidWarrantiesiVoidWarranties
5918
5918
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
add a comment |
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
what is the $.each doing here? I'm not familiar with this syntax of $.functionname,i would think you need to to specify an element to run the function on? like $('element').functionname?
– website walrus
Jan 3 at 17:29
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%2f54014808%2fjavascript-make-function-return-unique-result-for-every-div%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
3
You are picking a random name once. You are picking a random color once. If you want random results more than once, you need to call
Math.random()
more than once. You're also changing the text of every single div in one go. Having not just random() but unique results is actually simpler here: it means you just need to shuffle your arrays randomly, then compose the div texts in order. I'd also refrain from using classes until you have a firm grasp of the more basic stuff.– Chris G
Jan 3 at 0:16
My take: jsfiddle.net/khrismuc/trgo7mw5 (not going to try and score cheap points on this)
– Chris G
Jan 3 at 0:29