Stuck in an Infinite Loop with Multiple If Conditions inside a loop - javascript

Can you please let me know why this code is generating an infinite loop? I have tried to limit the steps between > min ) && (i <= logic but it's still sticking in the loop.
var min = -9.00;
var max = 14.00;
for (var i = min; i < max;) {
console.log(i);
if((i > min ) && (i <= -2.00)) {i += (0.25);}
if((i > -2.00 ) && (i <= 0.00)){i += (0.5);}
if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);}
if((i > 6.00 ) && (i <= max)) {i += (0.5);}
}

Yes you have an infinite loop. Because i = -9.00 and your first if is i > min which min = -9.00 its the same not greather. just change > to >=
var min = -9.00;
var max = 14.00;
for (var i = min; i < max;) {
console.log(i);
if((i >= min ) && (i <= -2.00)) {i += (0.25);}
if((i > -2.00 ) && (i <= 0.00)){i += (0.5);}
if((i > 0.00 ) && (i <= 6.00)) {i += (0.25);}
if((i > 6.00 ) && (i <= max)) {i += (0.5);}
}

Related

Simplifying IF/ELSE condition

I want to ask how can I simplify my code? It seems hard to read and has too much if-else condition here. Any way to simplify the code?
if (e.shiftKey && this.idx > 0) {
this.idx= this.idx - 1;
} else if (!e.shiftKey && this.idx < trapFocus.length - 1) {
this.idx = this.idx + 1;
} else if (!e.shiftKey && this.idx < trapFocus.length + 1) {
this.idx= this.idx - 2;
} else if (e.shiftKey && this.idx > - 1) {
this.idx= this.idx + 2;
}
You can simply separate the condition using separate if-else
if (e.shiftKey){
if(this.idx > 0) this.idx = this.idx - 1;
else if(this.idx > -1) this.idx = this.idx + 2;
} else {
if(this.idx < trapFocus.length - 1)) this.idx = this.idx + 1;
else if(this.idx < trapFocus.length + 1) this.idx < trapFocus.length + 1
}
This first thing you could do is to factor out e.shiftKey and use += and -= operators
if(e.shiftKey)
{
if(this.idx > 0)
{
this.idx -= 1;
}
else if(this.idx > -1)
{
this.idx += 2;
}
}
else{
if(this.idx < trapFocus.length - 1)
{
this.idx += 1;
}
else if(this.idx < trapFocus.length + 1)
{
this.idx -= 2;
}
}
If you ever want to go with ternaries:
this.idx += e.shiftKey ? (
this.idx > 0 ? -1 :
this.idx > -1 ? 2 : 0
) : (
this.idx < trapFocus.length - 1 ? 1 :
this.idx < trapFocus.length + 1 ? -2 : 0
);
Note that this is not necessarily more readable, it just takes up less space.
You can get it a bit more succinct by using the fact that your if clauses logically imply each other partly.
if e.shiftKey is true you change something if this.idx is 0 or more and if e.shiftKey is false you change something only if this.idx < trapFocus.length + 1:
let offset = 0;
if (e.shiftKey){
if (this.idx >= 0) (this.idx ? offset = -1 : offset = 2)
} else {
if (this.idx < trapFocus.length + 1)
(this.idx < trapFocus.length - 1 ? offset = 1 : offset = -2)
}
this.idx += offset;
It is not necessarily much more readable.

Simplifying multiple similar If statements (JAVASCRIPT)

Im working on a code for an adobe acrobat form, I want to add the following code:
var total =6301
var warranty
if (0 < total && total <= 3300){warranty = 194.25}
else if (3300 < total && total <= 4000){warranty = 197.5}
else if (4000 < total && total <= 5000){warranty = 202.15}
else if (5000 < total && total <= 6000){warranty = 206.75}
else if (6000 < total && total <= 7000){warranty = 211.45}
else if (7000 < total && total <= 8000){warranty = 216.1}
else if (8000 < total && total <= 9000){warranty = 220.75}
else if (9000 < total && total <= 10000){warranty = 225.4}
else if (10000 < total && total <= 11000){warranty = 230.1}
else if (11000 < total && total <= 12000){warranty = 234.75}
else if (12000 < total && total <= 13000){warranty = 239.4}
else if (13000 < total && total <= 14000){warranty = 243.95}
else if (14000 < total && total <= 15000){warranty = 248.7}
else if (15000 < total && total <= 16000){warranty = 253.3}
else if (16000 < total && total <= 17000){warranty = 258}
else if (17000 < total && total <= 18000){warranty = 262.65}
else if (18000 < total && total <= 19000){warranty = 267.3}
else if (19000 < total && total <= 20000){warranty = 271.95}
else if (20000 < total && total <= 21000){warranty = 276.6}
else if (21000 < total && total <= 22000){warranty = 281.3}
else if (22000 < total && total <= 23000){warranty = 285.9}
else if (23000 < total && total <= 24000){warranty = 290.5}
else if (24000 < total && total <= 25000){warranty = 295.25}
else if (25000 < total && total <= 26000){warranty = 299.85}
else if (26000 < total && total <= 27000){warranty = 304.55}
else if (27000 < total && total <= 28000){warranty = 309.15}
else if (28000 < total && total <= 29000){warranty = 313.85}
else {warranty = 999999}
I want to have this be a lot less repetitive as I have a lot more conditions similar to the ones above.
Thanks for the help in advance!!
This approach features an array with value pairs which works for any values.
const
getValue = total => [
[3300, 194.25], [4000, 197.5], [5000, 202.15], [6000, 206.75],
[7000, 211.45], [8000, 216.1], [9000, 220.75], [10000, 225.4],
[11000, 230.1], [12000, 234.75], [13000, 239.4], [14000, 243.95],
[15000, 248.7], [16000, 253.3], [17000, 258], [18000, 262.65],
[19000, 267.3], [20000, 271.95], [21000, 276.6], [22000, 281.3],
[23000, 285.9], [24000, 290.5], [25000, 295.25], [26000, 299.85],
[27000, 304.55], [28000, 309.15], [29000, 313.85], [Infinity, 999999]
].find(([t]) => total <= t)[1];
console.log([0, 1, 3300, 3500, 3999, 4000, 4001, 5000, 10000, 100000].map(getValue));
Use switch case instead which is ideal in terms of optimisation & performance. This way the code execution directly jumps into the corresponding condition rather than going through each if else if condition.
var total = 6301;
var warranty;
switch (true) {
case (0 < total && total <= 3300): warranty = 194.25; break;
case (3300 < total && total <= 4000): warranty = 194.25; break;
...
default: warranty = 999999; break;
}

SyntaxError: unterminated string literal in this javascript

Why am I getting this error???
function Overallrating() {
var amt = this.getField("txt5).value;
if (amt > 10.5){
this.getField("Text2").value = "EXCEPTIONAL";
}else if (amt < 11.00 && amt > 8.5){
this.getField("Text2").value = "EXCEEDS";
}else if (amt < 9.00 && amt > 6.5){
this.getField("Text2").value = "IMPROVEMENT NEEDED";
}else if (amt < 7.00 && amt > 4.50){
this.getField("Text2").value = "UNSATISFACTORY";
}else if (amt < 5.00 && amt > 2.00)
}
you wrote
var amt=this.getField("txt5).value;
you need " at the end of txt5. correct to:
var amt=this.getField("txt5").value;

Recursively setting a value depending on range using JavaScript

I don't know how to word this but this is what I'm trying to do:
if (score >= 0 && score <= 10) overallScore = 0;
else if (score >= 11 && score <= 20) overallScore = 1;
else if (score >= 21 && score <= 30) overallScore = 2;
else if (score >= 31 && score <= 40) overallScore = 3;
else if (score >= 91 && score <= 100) overallScore = 9;
...
Is there any way to recursively do this using a function?
overallScore = Math.max(0, Math.floor((score - 1) / 10));
no need for recursion. But if you need that:
const getOverall = score => score <= 10 ? 0 : getOverall(score - 10) + 1;
Recursion is not really appropriate here, since you can get the required value in constant time. Recursion becomes interesting when you need at least O(logn) time.
But as you ask for it, here is one way to make it recursive:
function range(score, depth = 0) {
return score <= 10 || depth >= 9 ? 0 : range(score-10, depth+1) + 1;
}
console.log(range(0)); // 0
console.log(range(10)); // 0
console.log(range(11)); // 1
console.log(range(60)); // 5
console.log(range(91)); // 9
console.log(range(110)); // 9

Stuck programming Conway's "Game of Life" in JS

We have to program a JavaScript version of Conway's Game of Life for a school project, but we're stuck on looping the edges. The whole thing works fine, but the function that calculates the number of neighbors doesn't work on the cells that are on the edges (because it has to evaluate values outside of the array, which are undefined). We've tried several options, but they all alter the functionality of the rest of the program.
What should we add for it to work on the edges of the grid?
var totalNeighbors = function(x, y) {
var total = 0;
if (x > 0 && cells[(x - 1)][y] == 1) {
total++;
}
if (x < (width - 1) && cells[x + 1][y] == 1) {
total++;
}
if (y > 0 && cells[x][y - 1] == 1) {
total++;
}
if (y < (height - 1) && cells[x][y + 1] == 1) {
total++;
}
if (y > 0 && x > 0 && cells[x - 1][y - 1] == 1) {
total++;
}
if (y > 0 && x < (width - 1) && cells[x + 1][y - 1] == 1) {
total++;
}
if (y < (height - 1) && x > 0 && cells[x - 1][y + 1] == 1) {
total++;
}
if (y < (height - 1) && x < (width - 1) && cells[x + 1][y + 1] == 1) {
total++;
}
return total;
};
Thanks!
I'd go with something more like this:
As you can see, I refactored a little bit.
var isvalid = function(x, y) {
/*
* This returns 1 if cells[x][y] == 1.
* Otherwise, we return 0.
* NOTE: If cells[x, y] is out of bounds, we return 0.
* GLOBALS USED: cells, width, and height.
*/
//This returns true if (index < size && index >= 0)
//Used to check that index is not an invalid index.
var inbounds = function (size, index) {
return (index >= 0 && index < size);
};
//given point is out of bounds
if (!inbounds(width, x) || !inbounds(height, y)) {
return 0;
}
//everything is good
return (cells[x][y] === 1) ? 1 : 0;
};
var totalNeighbors = function(x, y) {
var total = 0;
//cells[x-1][y]
total += isvalid(x-1, y);
//cells[x + 1][y]
total += isvalid(x+1, y);
//cells[x][y - 1]
total += isvalid(x, y-1);
//cells[x][y + 1]
total += isvalid(x, y+1);
//cells[x - 1][y - 1]
total += isvalid(x-1, y-1);
//cells[x + 1][y - 1]
total += isvalid(x+1, y-1);
//cells[x - 1][y + 1]
total += isvalid(x-1, y+1);
//cells[x + 1][y + 1]
total += isvalid(x+1, y+1);
return total;
};
PS: Your original code sample is 37 lines without comments. My code sample is 52 lines with comments and 33 lines without comments.
As near as I can figure, this way is cleaner and shorter. ;)

Categories

Resources