Ok so I have this problem with a loop assignment I'm working on.
I'm supposed to use a "while" loop for the first 4 loops and a "for" loop for the remaining 15 loops. The problem is the first 4 in the while loop print to the console correctly in sequence like they're supposed to.
The remaining 15 only print to the console in odd intervals IE: "5,7,9,11...."
What's wrong with my for loop?
var currentGen = 1;
var totalGen = 19;
var totalMW = 0;
while(currentGen <= 4){
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + (totalMW = totalMW + 62) + " MW!");
currentGen++;
}
for(currentGen = 5; currentGen < 20; currentGen = currentGen + 1){
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + (totalMW = totalMW + 124) + " MW!");
currentGen++;
}
You have both
currentGen = currentGen + 1
and
currentGen++;
So every iteration you are increasing by 2, not 1. Just do the one or the other.
Because, for loop has its own increment section. You don't have to do it again in the body of the loop
for(currentGen = 5; currentGen < 20; currentGen = currentGen + 1 ) { //incrementing
console.log("Generator #" + currentGen + " is on, adding 62 MW, for a total of " + (totalMW = totalMW + 124) + " MW!");
// currentGen++; // Incrementing again
}
That is because you should not have the currentGen++ line inside the for loop. The for loop does that variable increment part.
Related
This question already has answers here:
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 10 months ago.
I am trying a little bit of coding and I would like for the 30 character strings to appear in the 1 second intervals one after another when the code is started. I tried setInterval & setTimeout and I couldn't get it to work. If someone could please help that would be much appreciated.
count = 0
while (count < 200) {
console.log(create_random_string(30))
function create_random_string(string_length){
var random_string = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
for(var i, i = 0; i < characters.length; i++){
random_string += characters.charAt(Math.floor(Math.random() * characters.length))
}
return "|BTC| " + random_string + " | " + " PRIVATNI KLJUČ NIJE ISPRAVAN" + " | " + " STANJE: 0.00"
}
count = count + 1
}
setInterval(code, time);
this function will allow you to wait X miliseconds before running the code defined.
here, the correct code, and i fixed your formatting a bit.
let count = 0;
while (count < 200) {
setTimeout(
function() { console.log(create_random_string(30)); }
, 500);
count++;
}
function create_random_string(string_length){
var random_string = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'
for(var i, i = 0; i < characters.length; i++){
random_string += characters.charAt(Math.floor(Math.random() * characters.length))
}
return "|BTC| " + random_string + " | " + " PRIVATNI KLJUČ NIJE ISPRAVAN" + " | " + " STANJE: 0.00"
}
by the way, you probably wanted to replace characters.length inside your for loop for : string_length to use the fuction's parameter (which you never actually used)
so basically this the prompt:
Addition countdown
you enter a number and the code should be adding a number while countingdown, for example if the user enter 10, then the result should be:
10 + 9 + 8 + 7 + 6 + 5 + 4 +3 +2 +1=55.
This is what I have so far:
var num = Number(prompt("Enter a Number Greater than zero"));
while (num > 0){
first = num;
second = num-=1;
document.write(first + " +" + second + " +");
value = first + num;
document.write(value)
num--;
}
but I keep on getting something like this:
4 +3 +72 +1 +3 (let's say 4 is the number the user inputs)
I'm stuck can someone please help me????!!
You can keep total in one variable outside of while loop.
var num = Number(prompt("Enter a Number Greater than zero"));
var total = 0;
while (num > 0) {
total += num;
document.body.innerHTML += (num == 1 ? num + ' = ' + total : num + ' + ');
num--;
}
You could change the algorithm a bit, because for the first value, you need no plus sign for the output.
var num = Number(prompt("Enter a Number Greater than zero")),
value = 0;
document.body.appendChild(document.createTextNode(num));
value += num;
num--;
while (num > 0) {
document.body.appendChild(document.createTextNode(' + ' + num));
value += num;
num--;
}
document.body.appendChild(document.createTextNode(' = ' + value));
I can't seem to make this code alert the user when the correct answer is found, nor can I make the pop-ups continue to loop to play again. I coded this as a learning exercise. In JS i'm very noob.
Can someone review my code and offer constructive criticism?
// noprotect
var targetNumber = Math.floor(Math.random() * 100 + 1);
var userGuess = prompt("Pick a number from 1 to 100 to guess which one I'm thinking about!" + " (6 guesses left.)");
for (var i=5; i>0; i--) {
if (i === 0) {
prompt("Out of guesses!" + " (" + i + " guesses left.)" + " My number was: " + targetNumber);
}
if (isNaN(userGuess) === true) {
userGuess = prompt("That value was not a number! Please pick a number from 1 to 100 to guess which one I'm thinking about!" + " (" + i + " guesses left.)");
}
if (userGuess < 1 || userGuess > 100) {
userGuess = prompt("That number was not between 1 and 100 inclusive! Please pick a number from 1 to 100 to guess which one I'm thinking about!" + " (" + i + " guesses left.)");
}
if (userGuess === targetNumber) {
userGuess = alert("You're correct! My number was: " + targetNumber);
}
if (userGuess < targetNumber) {
userGuess = prompt("You're too low, guess again" + " (" + i + " guesses left.)");
}
if (userGuess > targetNumber) {
userGuess = prompt("You're too high, guess again!" + " (" + i + " guesses left.)");
}
}
You're comparing the string value prompt() returns with a number using ===. That's not going to work.
Instead:
var userGuess = parseInt(prompt(...))
It's worth noting that prompt is an extremely clunky way to do this. What would be better is creating a form where you have a proper <input> field and a place to put the responses.
Just to add a quick note before I head home from the office. Your for loop will never allow i === 0condition to pass because the loop only continues if i > 0. You likely want to change this to i >= 0 or adjust your conditions within the loop.
I am writing a code guessing program to test the security of some iPhone passwords (This is not in anyway connected to anything illegal or is used to break into actual iPhones). The do/while loops just end all response once they occur and the last two lines don't print to the console.
print("Hi!");
print("I am a password security tester.");
var tries = 0
var guess1 = 0
var guess2 = 0
var guess3 = 0
var guess4 = 0
var guess = (guess1 * 1000) + (guess2 * 100) + (guess3 * 10) + guess4;
var password1 = (Math.floor(Math.random()*9));
var password2 = (Math.floor(Math.random()*9));
var password3 = (Math.floor(Math.random()*9));
var password4 = (Math.floor(Math.random()*9));
var password = (password1 * 1000) + (password2 * 100) + (password3 * 10) + password4;
print("This is the randomly genorated password: " + password);
print("And now begins the guessing");
do{
guess1 + 1;
tries + 1;
}while (password1 != guess1);
do{
guess2 + 1;
tries + 1;
}while (password2 != guess2);
do{
guess3 + 1;
tries + 1;
}while (password3 != guess3);
do{
guess4 + 1;
tries + 1;
}while (password4 != guess4);
print("Complete in " + tries + " tries");
print("The answer is: " + guess);
Jacob Ewing's answer is correct, but another problem is that guess will still be 0 at the end, because it doesn't automatically update. You'll need to do:
var guess = (guess1 * 1000) + (guess2 * 100) + (guess3 * 10) + guess4;
Before:
print("Complete in " + tries + " tries");
print("The answer is: " + guess);
You need to be using a += operator instead of just +
Saying guess1 + 1 returns the value of guess1 + 1, but does not increment that value directly, so you get an infinite loop.
Here is my function:
function reCalculate(i) {
document.getElementById("Q" + i).value = document.getElementById("C" + i).value - document.getElementById("QA" + i).value;
if (document.getElementById("Q" + i).value < 0) {
document.getElementById("Q" + i).value = 0;
}
if (document.getElementById("Q" + i).value < document.getElementById("E" + i).value && document.getElementById("Q" + i).value != 0) {
alert(document.getElementById("Q" + i).value + " is less than " + document.getElementById("E" + i).value + "?");
document.getElementById("Q" + i).value = document.getElementById("E" + i).value;
}
document.getElementById("Q" + i).value = Math.ceil(document.getElementById("Q" + i).value);
}
It checks Q, if it's less than 0, it makes it 0. Then, if it's not 0, but it's less than E, it makes it E. For some reason this function works UNLESS Q is a double digit number.
For example, if Q is 7 and E is 2, then it will leave Q at 7. However, if Q is 10 and E is 2, for some reason it thinks that 10<2, and it changes Q to 2!
Am I missing something here??
When you pull the .value of an element it returns a string. '10'<'2' will return true.
You can simply do a parseInt/parseFloat on the value, ala
var q = parseInt(document.getElementById("Q"+i).value,10)
Thats because it is considering your Q as a string while comparing.
Try the following instead:
function reCalculate(i){
var Z = document.getElementById, P = parseInt;
var qElem = Z("Q"+i);
var q = P(qElem.value, 10);
var c = P(Z("C"+i).value, 10);
var qa = P(Z("QA"+i).value, 10);
var e = P(Z("E"+i).value, 10);
q = c - qa;
if (q < 0) qElem.value = 0;
if (q < e && q != 0){
alert(q+" is less than "+e+"?");
qElem.value = e;
}
qElem.value = Math.ceil(q);
}
May be you should do a
parseFloat(document.getElementById("Q"+i).value)
to make sure you are comparing numbers
You are comparing strings not numbers. Use the unary + to convert to a number:
if (+document.getElementById("Q" + i).value < +document.getElementById("E" + i).value ...)
You should use variables by the way:
var input_one = document.getElementById("Q" + i).value,
input_two = document.getElementById("E" + i).value;
if (+input_one < +input_two) {
}