Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to see if variable is < or equal to 0
Heres my code im working with.
theres other behind the scenes stuff to do with lives but all i need to know
is how to see its value (more below)
function beginCountup() {
var display = document.getElementById("display"),
i = 0;
intervalID = setInterval(function () {
display.textContent = ++i;
}, 26);
lives = lives - 1;
var lcount = document.getElementById('lives')
lcount.innerHTML = lives;
}
So i need to see if lives < or equal to 0 if so i need it to target ElementById('lives')
and make it say game over thanks!
Simple if statement
if(lives<=0){
document.getElementById("lives").innerHTML="Game Over!"
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to make a little program in js that overturns the text you typed in. The code runs fine, but the for loop gets completely ignored, it doesnt do even one block of code from inside of it.
document.getElementById("StartButtonText").addEventListener("click",function(){
var text_value = document.getElementById('TextValue').value;
console.log(text_value);
var text_length = text_value.length;
console.log(text_length);
var final_text;
var order;
for (order = 1; order >= text_length; order ++) {
final_text[order] = text_value[text_length - order + 1];
console.log('for loop log ',order);
final_text = final_text + final_text[poradie];
}
console.log('after loop log ',order);
document.getElementById("TextTurningResult").innerHTML = final_text;
console.log(final_text);
});
Any ideas why it doesnt run?
Your loop will run as long as order is greater or equals than text_length.
I guess you wanted to write:
for (order = 0; order < text_length; order ++) { ... }
You should change the condition in your for loop to
for(order = 1; order <= text_length; order++){
...
}
In your case, the condition is false at the first iteration only.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing some javascript to be run in a bookmarklet that should get the text within a element that has a certain class name.
So for example
document.getElementsByClassName('price')
where the web page has or example
<span class="price">
£23
</span>
How would I go about getting the actual text within the the element that has a class name price (i.e £23 in the above example).
getElementsByClassName returns an array of elements. Traverse through that array and get the innerText property of each. For example:
var priceEls = document.getElementsByClassName("price");
for (var i = 0; i < priceEls.length; i++) {
var price = priceEls[i].innerText;
alert("Price: " + price);
}
Live demo: http://jsfiddle.net/YQsBW/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have three tabs and I want to navigate when I click on each one of them. The code that I wrote is working just fine, but I believe that is bad coding, there is any way to improve this is just for learning purposes. Thanks!!!!
jQuery(".nuestra_actualidad li:eq(0)").click(function() {
jQuery("#tabs-actualidad").css("display","block");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(1)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","block");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(2)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","block");
});
Replacing jQuery with $ if possible (unless it clashes with another library) and then it can be reduced to a single function by utilising the index of the clicked element and calling the toggle function:
$(".nuestra_actualidad li").click(function() {
var index = $(this).index();
$("#tabs-actualidad").toggle(index === 0);
$("#tabs-articulos").toggle(index === 1);
$("#tabs-noticias").toggle(index === 2);
});
Example - http://jsfiddle.net/gSKeL/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am coding a javascript based network application and in which i have to create array of 1000 rec which generate random numbers between 0 and 1 as
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
}
it takes some seconds to generate all random numbers and show it on a div so i just want to ask to how to show progress bar while it generating values?
Are you doing something daft like writing or appending values to the DOM within the loop - thus forcing the browser to try and redraw the screen each iteration...
example 1: fiddle - 4000ms+ (for me)
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
document.getElementById('out').innerHTML += ('<br/>'+rec[i]);
}
example 2: fiddle - 10ms (or thereabout)
for (var i = 0; i < 1000; i++) {
rec[i] = Math.random();
}
document.getElementById('out').innerHTML = rec.join('<br/>');
It should not take seconds to generate a 1000 random numbers. In comparison I'm typing this on an 8 year old laptop with a crappy Centrino processor and a simple test produces somewhere in the region of 420,000 random numbers within a second.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I add the a css rule say for example
for (i to 10)
.left= .left + 150
I tried something like this but I doesn't wrork
$(this).after("'style='left:'+1500+'px'");
This should work, but it looks like you are looking for a animation. You can check demo here, but the for loop runs fast that you don't even see the effect.
JQuery has .animate(), could be worth checking.
var le = 150;
for (var i=0; i < 10; i++)
{
le += 150;
$(this).css('left', le);
}