var dates =["09/27/2014","12/19/2013","01/13/2015",""];
var departments = ["Item1","Item2","item3",""];
var writespeed = 400;
$('.department_name').html(departments[fade-1]);
$('.date_name').html(dates[fade-1]);
The issue is in the following line of code:
$('.department_name').html(departments[fade-1]);
The issue is that in your loop, "fade", is not incremented. You can either use fade++ or assign it to a different variable and increment it.
Nothing but assigning a new variable worked.
var z = 1;
and then:
z++;
Its hard to tell from just the jquery, but I believe 'fade' needs to be incremented:
var fade = 1;
var depart_time = 1000;
console.log(0,fade);
$('.shutter').removeClass('closed-shutter open-move opened-shutter close-move');
function writeText(args) {
var dates =["09/27/2014","04/26/2014","03/17/2015"];
var departments = ["Item1","Item2","Item3"];
var writespeed = 700;
$('.write-text').each(function(){
$('.department_name').html(departments[fade-1]);
$('.date_name').html(dates[fade-1]);
fade++;
$(this).width('auto');
var w = $(this).width();
$(this).width(0).animate({width:w+'px'}, writespeed, function(){
if ($(this).hasClass('patient_name')) {
$(this).removeClass('write-text');
}
});
});
}
Related
var imgOut = function(){
var outImg = Math.floor(Math.random()*slideRandom.length);
document.getElementById("random").src = imgRandom[outImg];
var outSlide = slideRandom[outImg];
outSlide();
var wr = Math.floor(Math.random()*imgRandom.length);
var btl = Math.floor(Math.random()*bottle.length);
document.getElementById("bottle2").src = bottle[outImg];
document.getElementById("bottle1").src = bottle[btl];
document.getElementById("bottle3").src = bottle[wr];
/* try below but doesn't work or syntax error occured*/
var position= ['bottle[outImg]','bottle[btl]','bottle[wr]'];
var pos = Math.floor(Math.random()*position.length);
var pos1 = position[pos];
pos1();
}
what I'm try to do is
with different arrays to get different each images in different random position.
I've done different images to get but with three different arrays to put in random position doesn't work. what have I done wrong? or how to change above code?
I don't understand your requirements exactly, but consider this code:
var setImage = function(id, src) {
document.getElementById(id).src = src;
};
var chooseOne = function(array) {
return array[Math.floor(Math.random()*array.length)];
};
var setRandomImages = function() {
setImage("bottle1", chooseOne(bottle));
setImage("bottle2", chooseOne(bottle));
setImage("bottle3", chooseOne(bottle));
};
If the array bottle is a list of URLs, this will do something, though I cannot guarantee it will do what you want.
var position= ['bottle[outImg]()','bottle[btl]()','bottle[wr]()'];
var pos = Math.floor(Math.random()*position.length);
var pos1 = position[pos];
//pos1();
eval( pos1 );
// or
(new Function("return pos1"))();
so this might be a repost, but I don't really know how to explain my second problem.
I have this code:
var paragraphsArray = new Array();
function setParagraphs(offSet)
{
offSet = offSet * 12;
for (var i = 1; i < 13; i++)
{
var parX = i + offSet;
var testASd = $.get('php/entryParagraphs.php', {idd: parX}).done(function(paragraph)
{
//clear paragraph1 div
document.getElementById("paragraph1").innerHTML = "";
//create p elements
var pElem = document.createElement("p");
pElem.setAttribute("id", "pEntry"+i);
document.getElementById("paragraph1").appendChild(pElem);
$("pEntry"+i).text(paragraph);
});
}
}
edited: I removed the second loop because it was unnecessary, for some reason the p element creation starts on i==13, which is the extra one that shouldn't even do.
for some reason the second loop executes first, so the paragraphArray is printed out as undefined. I managed to "fix" the order with the setTimeout() function, BUT I still get the undefined message, instead of the value. In the first loop the value is printed out fine, but if I try and put it in a $("p").text(paragraph); I also get undefined. So although I was right about the execution order, the problem is still there!
Because first is in ajax call, declare paragraphsArray in global space and use a callback function, try this:
*Updated
var paragraphsArray = [];
function setParagraphs(offSet) {
offSet = offSet * 12;
var request = 0;
for (var i = 1; i < 13; i++) {
var parX = i + offSet;
var testASd = $.get('php/entryParagraphs.php', {idd: parX}).done(function(paragraph) {
request++;
paragraphsArray[request] = paragraph;
console.log(paragraphsArray[request]);
if (request === 12) {
alert('first');
callback();
}
});
}
}
function callback() {
for (var i = 1; i < 13; i++) {
console.log(paragraphsArray[i]);
}
alert('second');
}
Run the second loop inside of the first loop.
function setParagraphs (offSet) {
//paragraphs
var testing = 0;
var paragraphsArray = new Array();
offSet = offSet * 12;
for (var i=1;i<13;i++) {
var parX = i + offSet;
var testASd = $.get('php/entryParagraphs.php', { idd: parX }).done(function(paragraph) {
paragraphsArray[i] = paragraph;
console.log(paragraphsArray[i]);
alert('first');
for (var i=1;i<13;i++) {
console.log(paragraphsArray[i]);
alert('second');
}
});
}
}
$.get is async function. 1st cycle will just send requests and wouldn't wait for response, so 2nd cycle will start right after first, without getting response of $.get function. Thats why console.log(paragraphsArray[i]); in 2nd cycle shows undefined.
You only can handle response in first cylce.
You can use $("p").text(paragraph); only like in this example:
var testASd = $.get('php/entryParagraphs.php', { idd: parX }).done(function(paragraph) {
paragraphsArray[i] = paragraph;
console.log(paragraphsArray[i]);
alert('first');
$("p").text(paragraph);
});
You can't use variables, which are assigned in function
function(paragraph) {
paragraphsArray[i] = paragraph;
console.log(paragraphsArray[i]);
alert('first');
$("p").text(paragraph);
}
outside of this function.
To achieve what you want you have to use another approach.
HTML will be:
<div id='paragraphs'>
</div>
JS code:
var testASd = $.get('php/entryParagraphs.php', { idd: parX }).done(function(paragraph) {
$("#results").append("<p>"+paragraph+"</p>")
});
You should use ~ this code. I just show you approach.
this is what I've got and been struggeling for hours. if I alert(i)in the each loop it gives me 1,2,3... but if I want to use as as key for a multidimensional array it is like a string "i"
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function(i) {
//alert(i);
var elPositions = {};
elPositions.i = $(this).offset().top;
positions.push(elPositions);
//$elPosArray[i] = $(this).offset().top;
//$(this).html('outer height--> ' + $(this).outerHeight(true));
});
console.log(positions);
//console.log(el);
});
There are Questions and answers to this topic but none of them helped me to get this to work.
I would like to get an array or obj looking something like:
positions[0]['offset'] = '120';
positions[0]['height'] = '300';
positions[1]['offset'] = '420';
positions[1]['height'] = '180';
positions[2]['offset'] = '600';
positions[2]['height'] = '100';
positions[3]['offset'] = '700';
positions[3]['height'] = '300';
Here is a fiddle with the html http://jsfiddle.net/Z9WrG/
You're pretty much there!
In your loop, elPositions (here renamed data) is recreated new on each iteration, and then pushed into the array with a consecutive index. There's no need to specify i in the data object as i is assigned automatically when you push into the array.
See updated fiddle: http://jsfiddle.net/Z9WrG/2/
and code:
$(document).ready(function(){
var positions=[];
$( ".box" ).each(function() {
var $this = $(this);
var data = {};
data.offset = $this.offset().top;
data.height = $this.height();
positions.push(data);
// Now, positions[iteration_index] = { offset: x, height: y }
});
console.log(positions);
console.log(positions[0].height);
console.log(positions[0].offset);
});
I have been working on a simple math game and am having problems getting the overall answer results to return after the end of the game.
Here is what my return function looks like
function pShowResults() {
var pNumResults = document.getElementById("results");
for (var i = 0; i <= 10; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
Here is the full script
Pretty much need debugging help. I new to this so I'm guessing there is a ton that's off, but as long as I can get the results fed back I should be fine.
You are not passing the value of x in many placess
$(document).ready(function () {
//declare arrays and variables for use below
var pNum1 = [];
var pNum2 = [];
var pNumAns = [];
var pNumGuess = [];
var pNumStore = [];
var pNumCarry = 0;
var pNumTrack = 0;
var pNumMessageRight = ['Awesome Job!', 'Correct!', 'Great Job!'];
var pNumMessageWrong = ['Oh No! That Was Wrong!', 'Incorrect!', 'That\'s Wrong'];
$(".Play").click(function () {
$("#popup").attr("class", "on");
pNumTrack = 0;
pNumGen(pNumTrack);
});
$(".pNumSubmit").click(function () {
pNumCalc(pNumTrack-1);
});
$(".pNumNext").click(function () {
pNumGen(pNumTrack);
});
function pNumGen(x) {
pNum1[x] = (Math.round(Math.random() * 51));
pNum2[x] = (Math.round(Math.random() * 51));
pNumAns[x] = pNum1[x] + pNum2[x];
$(".pNum1").html(pNum1[x]);
$(".pNum2").html(pNum2[x]);
$(".pNumGuess").val("");
$(".pNum1").html(pNumTrack[x]);
if (pNumTrack == 2) {
$(".pNumNext").html("");
$(".pNumSubmit").html("Close");
pShowResults();
}
pNumTrack++;
}
function pNumCalc(x) {
pNumGuess[x] = $(".pNumGuess").val();
if (pNumGuess[x] == pNumAns[x]) {
$(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
$(".pNumNext").html("Next Question >")
} else {
$(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
$(".pNumNext").html("Maybe The Next Question >")
}
}
function pShowResults() {
var pNumResults = document.getElementById("results");
for (var i = 0; i < pNumGuess.length; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
});
Demo: Fiddle
There is a function called pNumCalc in your code which you have set to take in an argument, but you never pass one in. You use the argument to store the results in the pNumGuess array, but since the argument is never passed in, the guesses are never stored, and you end up with undefined as the answers the user gave.
Updated fiddle: http://jsfiddle.net/dwdX9/2/. Not sure how close this is to what you actually want though, but hopefully it gets you on the right track.
Because StackOverflow wants code to to be included when JSFiddle is...:
pNumCalc(pNumTrack)
You forget to define array before use it.
function pShowResults() {
var pNumStore = new Array();
var pNumResults = document.getElementById("results");
for (var i = 0; i <= 10; i++) {
pNumStore.push(pNumGuess[i]);
var pNumTable = document.createElement("div");
pNumTable.innerHTML = (pNumGuess[i]);
pNumResults.appendChild(pNumTable);
}
}
I must suggest you should use jquery instead.
After visiting your Fiddle seems like there are many problems with the code. and also your question is unclear.
for e.g.
$(".pNumSubmit").click(function () {
//why x value not passed?
pNumCalc();
});
function pNumCalc(x) {
pNumGuess[x] = $(".pNumGuess").val();
if (pNumGuess[x] == pNumAns[x]) {
$(".message").html(pNumMessageRight[Math.floor(Math.random() * pNumMessageRight.length)]);
$(".pNumNext").html("Next Question >")
} else {
$(".message").html(pNumMessageWrong[Math.floor(Math.random() * pNumMessageWrong.length)]);
$(".pNumNext").html("Maybe The Next Question >")
}
}
Please clear which array is returning undefined so that others can help you.
I have the following
var invest401_2 = $("input[type=hidden][name=invest401_2]").val();
var invest401_3 = $("input[type=hidden][name=invest401_3]").val();
var invest401_4 = $("input[type=hidden][name=invest401_4]").val();
var invest401_5 = $("input[type=hidden][name=invest401_5]").val();
var invest401_0label = Math.round(((invest401_0/balance401)* percent401kb));
var invest401_1label = Math.round(((invest401_1/balance401)* percent401kb));
var invest401_2label = Math.round(((invest401_2/balance401)* percent401kb));
var invest401_3label = Math.round(((invest401_3/balance401)* percent401kb));
var invest401_4label = Math.round(((invest401_4/balance401)* percent401kb));
var invest401_5label = Math.round(((invest401_5/balance401)* percent401kb));
$("#invest401_0").text(invest401_0label+'%');
$("#invest401_1").text(invest401_1label+'%');
$("#invest401_2").text(invest401_2label+'%');
$("#invest401_3").text(invest401_3label+'%');
$("#invest401_4").text(invest401_4label+'%');
$("#invest401_5").text(invest401_5label+'%');
Having the count total - ex. 5
How do a throw this into a for each loop.
I tried but didnt work.
Try this
var invest401_label = [];
var invest401 = [];
for(i=0;i<6;i++)
{
invest401[i] = var invest401_2 = $("input[type=hidden][name=invest401_"+i+"]").val();
invest401_label[i] = Math.round(((invest401[i]/balance401)* percent401kb));
$("#invest401_"+i).text(invest401_label[i]+'%');
}
Take a look at $.each.
http://api.jquery.com/jQuery.each/
If you add a class to this elements $("input[type=hidden][name=invest401_2]").val(); you can get them as an array and use each.
If you add a class named elements. Use the following example.
$('.elements').each(function(i, element) {
var invest = $(element).val();
$(element).val(Math.round((invest/balance401)* percent401kb));
});
Or
var $elements = $('.elements');
for(var i in $elements) {
var element = $elements[i];
element.val(Math.round((element.val()/balance401)* percent401kb));
}
Assuming there is only #invest401_0 - 5
$("[id=^invest401_]").each(function(){
$(this).text(Math.round((($("input[type=hidden][name="+this.id+"]").val()/balance401)* percent401kb))+'%');
});
Reference
http://api.jquery.com/jQuery.each/
http://api.jquery.com/category/selectors/
Although this may not work ( I'm currently writing this from an ie8 machine) this /should/ do what you want correctly and replaces all of the code you have there
for (i = 0; i < $('.hiddenelems').size(); i++) {
$('.hiddenelems:eq('+i+')').text(Math.round((($('.hiddenelems:eq('+i+')').val()/balance401)* percent401kb))+'%');
}