I want to show up all numbers between -10 and 19 but my google chrome console returning me just one number in while loop. what is the solution of this problem?
any idea? how can i solve this problem?
console.log("i want to print all numbers between -10 to 19");
var counter=-10;
while(counter < 20) { console.log(counter); counter++; }
Change your filter level. You can use "Info" or "Verbose" but not "Warnings" and "Errors"
Related
Hello I'm new to programming so please don't mind my silly questions. And please help me!
So, I was learning loops and started with for loop. I wanted to display :
1
11
21
31
41
51
61
71
81
91
(10 getting added to 1)
So I was referring a youtube video and it suggested the following way :
<html>
<title>I'm Learning</title>
</html>
<body>
<script>
for (var a = 1; a<=100;a = a+10){
document.write(a + "<br>")
}
</script>
</body>
</html>
Which worked. But, when I removed a = from for (var a = 1; a<=100;a = a+10) I thought it should still work as the value of a will still get changed. But, the webpage kept loading and after a while my laptop hanged. This happened twice. The webpage did not even load! So can anyone please help me with this? Please explain me if possible that why this error occurred. Thank You.
I was expecting that the output would not change.
You can do it also By a += 10 you need to assign the new value with the = aka the assign operator and it should still work :) you can't just blindly a + 10 unless you are doing it with an increment of 1 then you can do something like a++
I’m new here, and so on coding.
I friend of mine suggests me to learn JavaScript and Python, because I love riddles that I can’t solve (so this languages could help me).
Let me explain: I want to create a JS script starting from this real-life problem.
I have a padlock which have a code of three numbers to be unlocked (you have to turn upside down these numbers to obtain the “Open sesame”), the code goes to 000 to 999, obviously.
I need to create a script that lists all the possible numbers and, at the end, also tell me how many different numbers I have (I suppose 1000 if my math isn’t bad as my english).
I started the learning path, but I’m not able to create this script.
I need to check all the different combinations that i have done for unlock the padlock
Can someone help me?
Thank you so much
ps: it could be nice also the same script in bash, which it's more familiar to me
x 0stone0: I have non familarity with JavaScript, I've only done an online course, so I made no attempt, just asking.
For bash, I found right here a "skeleton" of permutation script like this:
for X in {a..z}{a..z}{0..9}{0..9}{0..9}
do echo $X;
done
but I really don't know ho to edit it, cause I don't know hot to save the output of three numbers YYY from 0 to 9
Javascript
let i = 0;
while (i <= 999) {
console.log(String(i).padStart(3, '0'));
i++;
}
Pad a number with leading zeros in JavaScript
Bash
for X in {0..9}{0..9}{0..9}; do
echo $X;
done
Try it online!
Using js, you can do:
let count = 0;
for (let i = 0; i <= 999; i++) {
count++ // count++ is the same as count = count + 1, count is used to count the number of times the loop has run
if (i < 10) { // if i is less than 10 add two zero before it, for it to look like (009)
console.log('00' + i);
} else if (i < 100) { // if i is less than 100 add one zero before it, for it to look like (099)
console.log('0' + i);
} else if (i < 1000) { // if i is less than 1000 add nothing before it, for it to look like (999)
console.log(i);
} else {
console.log(i);
}
}
// then we console.log() the count variable
console.log(`There is ${count} possibilities`);
The program is made to show 3 digits, so if it's 9, it will show 009 and same for 99 => 099
I have a p5.js animation here that is meant to animate the artwork then reverse the animation, however while it works for my other artworks, the logic doesn't work here as the if statement doesn't get triggered due to the subtraction for my mask only occurs once. Right now only have of the animation works, you can view it here: https://editor.p5js.org/theruantan/sketches/8g583LF8j
The main issue begins at line 116 and it is at line 124 where the code is unable to reach this if statement.
//Start of reversal for masks
for (let i = beachSwitches.length - 1; i >= 0; i--) {
if (beachSwitches[i] == 0) {
if (beachMasks[i] > 0) {
beachMasks[i] -= 20;
animating = true;
//console.log("beachMasks");
console.log(beachMasks[i])
}
if (beachMasks[i] <= 300) {
// Begin reversing the next bar
beachSwitches[i - 1] = 0;
// To see if the code is able to reach here.
console.log(beachSwitches[i - 1] + "This is triggered");
}
}
}
beachMarks[i] should be able to be <= 300 but it's stuck at 780.
I have recently started working with p5.js but just based on reading the code and understanding what you are trying to do what I can see is in the line you mentioned i.e 116 , if you log inside and outside you will understand why its only running once.
Inside the 116 lines for loop, you are checking for beachSwitches[i]==0, which only happens once(at the last index of that array) because the beachSwitches gets modified above, also you haven't defined anything in the else part for this condition. Which is what is beachSwtiches[i] == 0 is NOT TRUE.
In my screenshot below, you might get a rough idea. Hope this helps in some manner! Cheers!
Screenshot of logs I took from line 116 to 124
When I try to add two integers together, the integer just stays at what it originally was. It's difficult to explain, so here's my code:
var levelRewardsID = parseInt(resultData[0].levelRewardsHighestID)
var levelRewardsIDIncrease = Math.floor(Math.random()*9)+1
var newLevelRewardsID = levelRewardsID+levelRewardsIDIncrease
console.log(levelRewardsID)
console.log(levelRewardsIDIncrease)
console.log(newLevelRewardsID)
if (!isNaN(levelRewardsID)) {
console.log("not NAN")
}
if (!isNaN(levelRewardsIDIncrease)) {
console.log("not NAN 2")
}
if (!isNaN(newLevelRewardsID)) {
console.log("not NAN 3")
}
Console:
89819672607051330000
6
89819672607051330000
not NAN
not NAN 2
not NAN 3
So as you can see, everything is an integer. So that can't be the issue. Yet when I try to do var newLevelRewardsID = levelRewardsID+levelRewardsIDIncrease, the output is the same as levelRewardsID... I'm not sure what I did wrong but if anyone knows, do let me know. Thanks!
Note: I'm using Node.JS version 9.1.0 (latest as of posting this) if that helps
I think you reach the javascript number limit which seems to be 9007199254740992 and 89819672607051330000 > 9007199254740992.
See What is JavaScript's highest integer value that a number can go to without losing precision?
I'm facing a really strange problem comparing integer in Javascript. I have an array of numbers and I want to check if the current number in a loop is smaller than the previous one. To do so, I save the "current" number as "previous", so I can check them in the next loop. The function runs as expected, EXCEPT every time the current and the previous number have a different number of digits: in this case, the code doesn't see the actually smaller number as being smaller than the previous one.
For example:
111 < 120 ? ---> YES!
106 < 111 ? ---> YES!
98 < 106 ? ---> NO!
76 < 98 ? ---> YES!
5 < 76 ? ---> NO!
I'm unable to find anything strange in the code I'm using, as it is quite simple:
for(var j=0;j<arrScores.length;j++)
{
if(arrScores[j][0] < scoreAnt)
{
console.log("Smaller!");
}
scoreAnt = arrScores[j][0];
}
I've tried using parseInt() in both values, but nothing changes... Checking the length of both numbers using scoreAnt.toString().length returns a length of 1, no matter which number it is (100, 34 or 156798), and the same for arrScores[j][0]. I've also logged the whole thing to check that the numbers are the expected ones, and they are (the numbers used in the example are some of the ones I'm using)...
Any clue on what can be happening? I'm really lost with this, becuase it makes no sense for me...
Thanks in advance for your time and effort! :)
You always do scoreAnt = arrScores[j][0]; in the loop; but you should only do that if arrScores[j] is smaller; i.e. inside the inner curly braces.
for(var j=0;j<arrScores.length;j++)
{
if(arrScores[j][0] < scoreAnt)
{
console.log("Smaller!");
scoreAnt = arrScores[j][0];
}
}
Well, don't even know why, but after changing something relating the CORS of the server where these numbers came from (but not modifying the numbers at all), the comparison seems to work as expected... Have no clue about what might have changed! :S However, now it works correctly...