Else If statements not running - javascript

I am trying to keep a variable within a range of numbers when the function is ran, but the if statement does not run.
I am trying to have the object move only if its between 0 and 50 , if the objects value X is trying to go to 55 it will not run the code, but the issue I am facing is if move.X value is 51 and move.Y is 7 nothing runs, I need it to where move.Y will still ran with the if statements, while move.X can only be ran backwards. (decreasing the numbers)
go(move) {
if (move.X > 0 && move.X < 50) {
if (move.Dir === "W") {
move.X -= 1;
} else if (move.Dir === "E") {
move.X += 1;
}
if (move.Y > 0 && move.Y < 50) {
if (move.Dir === "N") {
move.Y -= 1;
} else if (move.Dir === "S") {
move.Y += 1;
}
} else {
console.log("Too Far");
}
}
}

You could take a different approach and check the position along with the wanted direction.
function go(move) {
if (move.X > 0 && move.Dir === "W") move.X -= 1;
else if (move.X < 50 && move.Dir === "E") move.X += 1;
else if (move.Y > 0 && move.Dir === "N") move.Y -= 1;
else if (move.Y < 50 && move.Dir === "S") move.Y += 1;
else console.log("Too Far");
}

Your code has two problems:
1) If one of the values exits the range, e.g. X = -1, then there is no way to get back into the range again, as you block both directions in that case.
You should change your logic to only block the E if it reaches the left border and only the W if it reaches the right border, that way the object can be moved into the range again.
2) The last else part will only be executed if move.X > 0 && move.X < 50 is false. All the other nested ifs won't enter that branch.
You could either not nest the if / elseifs as shown in Nina's answer, or you would have to add an else branch to every if.

If move.Dir values are strictly associated with each coordinate (W and E - with X, while N and S - with Y), I would just try smth like:
function go(move) {
switch(move.Dir) {
case 'W':
if (move.X > 0) { move.X -= 1; }
break;
case 'E':
if (move.X < 50) { move.X += 1; }
break;
case 'N':
if (move.Y > 0) { move.Y -= 1; }
break;
case 'S':
if (move.Y < 50) { move.Y += 1; }
break;
}
}

Related

addWithSurcharge exercise alternative answers

I had this JavaScript exercise from jshero.net:
Write a function addWithSurcharge that adds two amounts with surcharge. For each amount less than or equal to 10, the surcharge is 1. For each amount greater than 10 and less than or equal to 20, the surcharge is 2. For each amount greater than 20, the surcharge is 3. The call addWithSurcharge(10, 30) should return 44.
My solution was :
function addWithSurcharge (a,b){
let myS = a+b
if (myS <10){
return myS +=2} else if (myS >10 && myS <=20){
return myS +=2} else if (myS >20 && myS <30){
return myS +=3} else if (myS >= 30 && myS <40){
return myS +=4} else if(myS >40){
return myS +=5}
}
Somehow it worked, I passed the challenge but I feel like there was an easier way to solve this. Do you know other alternative answers for this exercise?
you could write it as a switch statement. something like this:
function addWithSurcharge (a,b) {
let myS = a+b
switch (true){
case myS < 10:
return myS + 2
case (myS > 10 && myS <= 20):
return myS + 2
case (myS > 20 && myS < 30):
return myS + 3
case (myS >= 30 && myS < 40):
return myS + 4
default:
return myS + 5
}
}
I think you can round to the superior decade and then divide by 10.
I'm surprised you passed the test cause you don't really fit the problem, you forgot every case when equal to 10, 20, 30, ...
By the way, this is my way to answer your problem. With this way it's "infinite" but if you wan't stop adding after 40, just add Math.max(X, (decadeRounded / 10)) where X is your maximum, for example Math.max(5, (decadeRounded / 10))
function addWithSurcharge (a,b) {
let myS = a + b
let decadeRounded = Math.round( (myS/10) ) * 10;
return myS + (decadeRounded / 10);
}
document.getElementById('result').innerHTML = addWithSurcharge(10, 20);
<div id="result"></div>
You can try something like this
function addWithSurcharge(a, b) {
if (a <= 10) {
a += 1
} else if (a > 10 && a <= 20) {
a += 2
} else if (a > 20) {
a += 3
}
if (b <= 10) {
b += 1
} else if (b > 10 && b <= 20) {
b += 2
} else if (b > 20) {
b += 3
}
return a + b;
}
function addWithSurcharge(a, b) {
i = 0;
if (a <= 10) {
i = i + 1;
} else if (a > 10 && a <= 20) {
i = i + 2;
} else if (a > 20) {
i = i + 3;
}
if (b <= 10) {
i = i + 1;
} else if (b > 10 && b <= 20) {
i = i + 2;
} else if (b > 20) {
i = i + 3;
}
return a + b + i;
}

Can't get the right result in condition

I can't get theright result, "Weird" on stdin, 18 and 20. Everything looks good to me, however something must be off.
if (N % 2 == 1) {
console.log("Weird");
}
else if ((N % 2 == 0) && (2 >= N <= 5)) {
console.log("Not Weird");
}
else if ((N % 2 == 0) && (5 <= N <= 20)) {
console.log("Weird");
}
else if ((N % 2 == 0) && (N > 20)) {
console.log("Not Weird");
}
else {
console.log("Weird");
}
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const N = parseInt(readLine(), 10);
if (N%2==1) {
console.log("Weird");
}
else if ((N % 2 == 0) && (2 >= N <= 5)) {
console.log("Not Weird");
}
else if ((N % 2 == 0) && (5 <= N && N <= 20)) {
console.log("Weird");
}
else if ((N % 2 == 0) && (N > 20)) {
console.log("Not Weird");
}
else{
console.log("Weird");
}
}
I ve added the whole code. In the main function, in the second else if condition, there seems to be the problem. When n is given 18 or 20, I can not get the right output which should be "Weird"
You can't be doing two conditions in the same time
if (5<=N<=20) {}
Will evaluate 5<=N first which produces either true/false which are when compared to numbers will evaluate to (1/0) respectively. Then the second part ( <= 20) will be evaluated.
Combine two conditions only with AND / OR operators
if (5 <= N && N <= 20) {}
This will solve your problem.

Get Level from Experience points - Level isn't taken into consideration [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
OK, now my issue is that no matter what I do, it will only present the information as Level 1 instead of the designated level that I try. The code is as follows
function XPlevel(XP, level) {
if((XP >= 0 && XP < 300) && level === 1) {
level = 1;
} else if ((XP >= 300 && XP <900) || level === 2) {
level = 2;
} else if ((XP >= 900 && XP <2700) || level ===3) {
level = 3;
} else if ((XP >= 2700 && XP < 6500) || level === 4){
level = 4;
} else if ((XP >= 6500 & XP < 14000) || level === 5){
level = 5;
} else if ((XP >= 14000 && XP < 23000) || level === 6) {
level = 6;
} else if ((XP >= 23000 && XP < 34000) || level === 7) {
level = 7;
} else if ((XP >= 34000 && XP < 48000) || level === 8) {
level = 8;
} else if ((XP >= 48000 && XP < 64000) || level === 9) {
level = 9;
} else if ((XP >= 64000 && XP < 85000) || level === 10) {
level = 10;
} else if ((XP >= 85000 && XP < 100000) || level === 11) {
level = 11;
} else if ((XP >= 100000 && XP < 120000) || level === 12) {
level = 12;
} else if ((XP >= 120000 && XP < 140000) || level === 13) {
level = 13;
} else if ((XP >= 140000 && XP < 165000) || level === 14) {
level = 14;
} else if ((XP >= 165000 && XP < 195000) || level === 15) {
level = 15;
} else if ((XP >= 195000 && XP < 225000) || level === 16) {
level = 16;
} else if ((XP >= 225000 && XP < 265000) || level === 17) {
level = 17;
} else if ((XP >= 265000 && XP < 305000) || level === 18) {
level = 18;
} else if ((XP >= 305000 && XP < 355000) || level === 19) {
level = 19;
} else {
level = 20;
}
return level;
}
XPlevel(XP, level);
So when I plug in a character that is Level 5 for instance, it gives me back level 1 instead.
Any thoughts?
First of all, never code like this, never have multiple if-else statements that make semantically similar checks that could be written using an array or hash table in a few lines.
Your function could be re-written like this:
var xp_required = [0, 300, 900, 2700, 6500]; // ...etc, you fill this table with the XP required to be at Level = index + 1 (indices start at 0 in Arrays).
// XP for level: 1 2 3 4 5 ...
function getLevel(xp) {
for(var level = xp_required.length - 1; level >= 0; --level) {
if(xp >= xp_required[level] {
return level + 1; // The +1 is needed because Array's index starts at 0 but levels start at 1
}
}
console.log("XP value can not be negative. The given value was: " + xp);
return 0;
}
The for loop starts at the highest level and checks if the XP is enough for the player to be considered that level. If it's not, it means that the player is actually a lower level, thus decrementing the level value to be checked (until we reach index 0 which means Level 1). This means that once we get to the first level for which the XP sufficies it means that is indeed the correct level.
As a note, this could be improved by doing a binary search instead of a linear search, but I assume that this function is not called that often so the O(max_level) complexity is good enough.
Also, why is level both an input and output value for your function?
(Beside the XP stuff...) you're basically doing if level == 1 return 1 which is nonsensical.
If you already know the level than logically you don't need to check for the level.
The simplest & fastest way to get a level out of an array of XP values:
function getLevel(XP) {
var LV = 0;
[0, 300, 900, 2700, 6500, 14000].some(function(v, i) {
LV = i; // Level = index
return v > XP; // We have the LV value! Break out of loop (if condition is met)!
});
return LV;
}
Use like
var level = getLevel(2699); // 3
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

How to create a switch comparator in JS?

is there a way to create a switch comparator like this one?
switch (item) {
case (item<= 10):
money += 25;
$('#money').html(money);
break;
case (item > 10 && item <= 20):
money += 50;
$('#money').html(money);
break;
}
may be this:
item = YourValue;
switch (true) {
case (item <= 10):
money += 25;
$('#money').html(money);
break;
case (item > 10 && item <= 20):
money += 50;
$('#money').html(money);
break;
}
The expressions in the case statements will evaluate to true or false, and if that matches the switch condition,
but as per my suggestion you should go with if...else if...else statement for this kind of business logic.
Simple answer: No. Switch..case statements don't work like this. You would need an if & else if statement:
if (item <= 10)
{
money += 25;
$('#money').html(money);
}
else if (item > 10 && item <= 20)
{
money += 50;
$('#money').html(money);
}
You can use the if else instead of switch
if (item <= 10)
{
money += 25;
$('#money').html(money);
}
else if (item > 10 && item <= 20)
{
money += 50;
$('#money').html(money);
}

Calculate antenna size with EIRP

I wonder how I calculate the size of a C-Band and KU-Band antenna knowing your EIRP:
http://www.satlex.de/en/eirp_values_ku_band.html
http://www.satlex.it/pt/eirp_values_ku_band.html
Calculating with Javascript, because I have the EIRP of 42 dBW, as I find the size of the antenna?
I made this way, but the code is too large, it would be better to use a calculation to achieve this result.
if(dbw >= 64){
return 22;
}else if(dbw >= 63){
return 24;
}else if(dbw >= 62){
return 26;
}else if(dbw >= 61){
return 28;
}else if(dbw >= 60){
return 30;
}else if(dbw >= 59){
return 32;
}else if(dbw >= 58){
return 34;
}else if(dbw >= 57){
return 36;
}else if(dbw >= 56){
return 38;
}.................

Categories