Can some please help me to tell me where i am going wrong in my code. I have been working on it for a little while but i am still stuck please help. this is my code so far. Thanks for the help in advance.
This is my javascript
var bottles;
var beerNum = 99;
// This program will count to 99 for us
// Set up a counter variable, and start it on zero (good place to start)
var counter = 0;
// While loop
while (counter < 99) {
// This line increments (or adds 1) to our counter each time the loop goes around.
counter--;
// This line finds the HTML element with an ID of "output" and puts the value of our counter in it, followed by a line break <br />
// The += operator takes what's already stored and appends our new value to it
document.getElementById('output').innerHTML += counter + "<br />";
}
if (bottles = beerNum){
document.getElementById('output')>innerHTML += bottles + beerNum + "of beer on the wall";
}
And this is my HTML.
<p id="output"></p>
and i have a fiddle to accompany it
http://jsfiddle.net/Matt1990/X4UHD/39/
This might help you out a bit:
for(var i=99; i>0; i--){
document.getElementById("output").innerHTML += i + "Bottles on the wall take one down, pass it around." + "<br>";
}
http://jsfiddle.net/a0knrxzs/
EDIT:
We don't need a while loop, a for loop will just stop when it is no longer greater than 0. While loops are for stopping the loop after a something has been changed.
Related
I basically just want to print numbers a list of numbers a skip multiples of 3. I got it to work but the initial way i tried it did not work and i do not understand why, just need someone to please explain why it doesn't work and goes into an infinite loop.
This is the problem, why does it go into an infinite loop? I am clearly missing a key concept about code, if someone could help thanks.
var i = 0;
while (i <= 10) {
if (i % 3 == 0) {
continue;
}
document.write( i + "</br>");
i++;
}
I know you can do it this way.
while (i <= 10)
{
if (i % 3 != 0) {
document.write("Number is " + i + "<br />");
}
i++
}
If we ignore the code producing the output and look only at the code checking and modifying i, it might become a little more clear why it's not working. It also helps to format our code for extra clarity.
var i = 0;
while (i <= 10) {
if (i % 3 == 0) {
continue;
}
i++;
}
Start with i = 0.
i <= 10 is true. Enter the loop.
i % 3 == 0 is true. Enter the if block.
continue;. Go straight to the top of the while loop again. Do not pass i++;. Do not collect 1.
Lather. Rinse. Repeat (infinitely).
continue jumps to the next iteration and doesnt complete the rest of your code in the while. So i is not being incremented but rather staying as 0 becuase you wrote continue before incrementing the i. so therefore it is in an endless loop, it is always less than 10
So I have this snippet of code I made that simply counts down from a number X by subtracting 4 until it cant subtract 4 any more and still get a number greater than or equal to 1.
$start = "2647";
for($i = $start; $i >= 1; $i-=4) {
echo $i . ' ';
}
Now, I need help styling it. Right now it just shoots out a string of numbers separated by a space. Pretty boring.
How can I get one number at a time to display in a <div> as it counts down instead of the page of numbers that Ive got now?
Here's an example using javascript.
var counter = document.getElementById("counter"),
value = 2456;
function count(){
counter.textContent = value;
value -= 4;
if (value >= 1) setTimeout(count, 1000);
}
count();
<div id="counter"></div>
It doesn't really make sense to do this live counter via the PHP server.
I just have a question about some while loop logic.
So, when you write a loop that displays a string of numbers to a document and say that while the loop is <= (less than or equal to) say, 5, and you tell the loop to add 1 each time this is true, wouldn't that mean that: while the loop is equal to 5 that it would add one to 5 too? It doesn't, but I messed up on some code when I was practicing and noticed that when it is equal to five it does not add one, but I thought it would...
console.log('2nd Loop:');
text = '';
// loop:
i = 1;
while (i <= 5) {
text += i + ' ';
i += 1
}
console.log(text); // Should print `1 2 3 4 5 `.
the reason your text doesn't display a 6 isn't because i isn't incremented. It's because the text gets added onto before it's incremented.
In other words when executing on that 5th loop, the text would add on 5, and then it would increment i, and then it would check the loop again, which would no longer be valid and therefore 6 is never printed.
In memory, it adds one. It doesn't add it to the text though.
Since you're incrementing the value after assigning it and then the loop condition fails, it doesn't get to the part where you concatenate the string.
It does. Just output i and you'll see it's 6. text never gets the 6 because of when you increment i.
console.log('2nd Loop:');
text = '';
// loop:
i = 1;
while (i <= 5) {
text += i + ' ';
i += 1
}
console.log(text,i); // Should print `1 2 3 4 5 `.
b/c you +1 after you add i to text, all you need to do is switch the two line order.
EDIT
if you want it start with one just change your i to 0 to start with.
i = 1
console.log('2nd Loop:');
text = '';
i = 0;
while (i <= 5) {
i += 1
text += i + ' ';
}
console.log(text);
I am learning Javascript and have run into an issue with the push method. When I use it within a loop it is making my array 33 items instead of just adding 3 to the list. Initial list is 1-10 items long, user defined. I initiated all the variables in the beginning of the script, and the variable items is only manipulated when the user initially tells me how long the array will be. From there it is basic exercises in array methods, and this is the one that is giving me problems. Following is the push part of the code. I appreciate any feedback and will put more code up if anyone feels it is necessary.
for (i = 0 ; i < 3 ; i++){
newfood = prompt("Please enter food " + (i + 1) + ".");
foods.push(newfood);
}
document.write("<ol>");
i = 0; //resetting variable i to 0
for (i = 0 ; i < items + 3 ; i++){
document.write("<li>" + foods[i] + "</li><br>");
}
document.write("</ol>");
Looks like you're running into string concatenation that's then treating the string as a numeric type. Convert what I assume is a string to an int:
for (i = 0 ; i < parseInt(items) + 3 ; i++) {
document.write("<li>" + foods[i] + "</li><br>");
}
Hi I've been having so trouble with this project I need to change colors or matching numbers in 2 arrays, but have the remaining numbers stay there natural color.
for(d = 0; d < lotteryNums.length; d++) {
for(x = 0; x < quickDrawNums.length; x++) {
if(lotteryNums[d] == quickDrawNums[x]) {
quickDrawNums[x] = "<span class='winner'>" + quickDrawNums[x] + "</span>";
winCounter++;
} else {
quickDrawNums[x] = "<span class='number'>" + quickDrawNums[x] + "</span>";
}
}
}
When I have this display it gives me 5 empty boxes and 1 box with the number in it. It also stops my match if from working I was just wondering if anybody could help me sort this out. Thanks for the help in Advance :)
You need to remove the "else" because you are re-writing all the quickDrawNums every time the next lotteryNums is selected. This would result in only on class ='winner' on the last lotteryNums item. Not sure why the empty boxes appear. Verify the original "else" has correct spelling and case for objects, etc.