Calculate antenna size with EIRP - javascript

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;
}.................

Related

Wierd execution of if condition

Else condition is executed every time. If input is '11' answer is 'f' not 'D', I know the last condition should be else if but according to the logic of input is 11 output should be 'D'
function getGrade(score) {
let grade;
var score1 = Number(score);
// Write your code here
if (score1 > 25 && score <= 30)
grade = "A";
else if (score1 > 20 && score <= 25)
grade = "B";
else if (score1 > 15 && score <= 20)
grade = "C";
else if (score1 > 10 && score <= 15)
grade = "D";
else if (score1 > 5 && score <= 10)
grade = "E";
else (score1 > 0 && score <= 5)
grade = "F";
return grade;
}
Is it a copy-paste? Then it's a matter of typo.
else (score1 > 0 && score <= 5)
grade = "F";
if is missing here. Therefore, (score1 > 0 && score <= 5) is interpreted as the thing to do (so, evaluate an expression), and the next line is outside of any else/if branch and simply gets executed always.
You can use the parenthesis around the else if. Also for the last condition you can simple use else instead of else if
function getGrade(score) {
let grade;
var score1 = Number(score);
console.log(score1)
// Write your code here
if (score1 > 25 && score <= 30) {
grade = "A";
} else if (score1 > 20 && score <= 25) {
grade = "B";
} else if (score1 > 15 && score <= 20) {
grade = "C";
} else if (score1 > 10 && score <= 15) {
grade = "D";
} else if (score1 > 5 && score <= 10) {
grade = "E";
} else {
grade = "F";
}
return grade;
}
console.log(getGrade(11))
A better approach, is to use an early exit paradigm, starting with wrong values and then take a ladder of conditions which rely on the conditions before.
function getGrade(score) {
var score1 = Number(score);
if (isNaN(score1) || score1 > 30 || score1 < 0) return;
if (score1 > 25) return "A";
if (score1 > 20) return "B";
if (score1 > 15) return "C";
if (score1 > 10) return "D";
if (score1 > 5) return "E";
return "F";
}

Else If statements not running

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;
}
}

Why won't this javascript hot/cold app run?

added a while loop, and trying to end the loop by entering finish inside the loop. The game is still running after it the game is completed.
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
var correct = false;
while (!correct) {
correct = guessFunction();
var finish = false;
}
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!' + 'That was a total of ' + guessCount + ' guesses.');
correctGuess = true;
finish = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
Trying to get this code to run but it only allows for 2 alert windows when guessing the random number. Im not sure how to get this to run, perhaps the guessFunction is not running?
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
}
guessFunction();
if (diff >= 1 && diff <= 10) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50){
alert('Cold');
guessFunction();
}
else if ( diff > 50){
alert('Ice Cold');
guessFunction();
}
The script stops executing because you only call your function twice. If you want this to run until the user guesses the right number, you probably want a while loop:
var correct = false;
while (!correct) {
// guessFunction could return true if they get it right
correct = guessFunction();
}
Yes, your script is only executing twice before exiting. You've got your nesting wrong:
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
correctGuess = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
EDIT:
My answer is more verbose than it needs to be so that it relates directly to your question. The next step for you is to implement the better solution of a while loop mentioned in Menello's answer in your script.

My code used to work but won't now

So I made this code awhile ago, and it worked then, but now it won't. Can someone tell me what's wrong? What the code does is simple: I take a weapon damage value from a Fallout game (averaging the value first if FO1/FO2/FOT), tell it what game it's from, and it outputs how much damage it does in d20 Modern. I don't know if explaining what it does helps, but I hope it's clear.
var systemSelect = prompt("What system are you using? FO, F2, FT, F3, or FNV?")
var damage = parseInt(prompt("How much damage does the weapon do?"))
if (systemSelect === "FO" or "F2" or "FT") {
if (damage >= 1 && < 11) {
damage = "2d4";
} else if (damage >= 11 && < 26) {
damage = "2d6";
} else if (damage >= 26 && < 46) {
damage = "2d8";
} else if (damage >= 46 && < 61) {
damage = "2d10";
} else if (damage >= 61 && < 81) {
damage = "2d12";
} else if (damage >= 81 && < 101) {
damage = "4d6";
} else {
damage = "2d20";
}
}
if (systemSelect === "F3" or "FNV") {
if (damage >= 1 && < 8) {
damage = "2d4";
} else if (damage >= 8 && < 15) {
damage = "2d6";
} else if (damage >= 15 && < 25) {
damage = "2d8";
} else if (damage >= 25 && < 37) {
damage = "2d10";
} else if (damage >= 37 && < 61) {
damage = "2d12";
} else if (damage >= 61 && < 81) {
damage = "4d6";
} else {
damage = "2d20";
}
}
Your problem seems to be bad syntax in your if-conditions.
First of all, you can't say if (damage >= 1 && < 11), you need to specify the variable in each condition, i.e. if (damage >= 1 && damage < 11).
Secondly, the "or" operator is not or, it's || so you need if (systemSelect=="FO" || systemSelect=="F2" || systemSelect=="FT")
Fix those problems and try again - good luck :)

Conditionally Change BG image jQuery

I need to conditionally change the background of div based on the value of a variable here is my code:
<script type="text/javascript">
$(document).ready(function(){
if (combo.hcs <= 10) && (combo.hcs >= 1)
$(".myClass").css("background-image", "url('BB.png')";
else if (combo.hcs <= 20) && (combo.hcs >= 11)
$(".myClass").css("background-image", "url('BH.png')";
else if (combo.hcs <= 30) && (combo.hcs >= 21)
$(".myClass").css("background-image", "url('BM.png')";
else if (combo.hcs <= 40) && (combo.hcs >= 31)
$(".myClass").css("background-image", "url('HB.png')";
else if (combo.hcs <= 50) && (combo.hcs >= 41)
$(".myClass").css("background-image", "url('HH.png')";
else if (combo.hcs <= 60) && (combo.hcs >= 51)
$(".myClass").css("background-image", "url('HM.png')";
else if (combo.hcs <= 70) && (combo.hcs >= 61)
$(".myClass").css("background-image", "url('MB.pmg')";
else if (combo.hcs <= 80) && (combo.hcs >= 71)
$(".myClass").css("background-image", "url('MH.png')";
else if (combo.hcs <= 99) && (combo.hcs >= 81)
$(".myClass").css("background-image", "url('MM.png')";
}
</script>
I am not sure if this is the best approach or not but ether way, I am getting the following error in Chrome:
Uncaught SyntaxError: Unexpected token &&
I think my approach would be to save the background-urls in an array.
var imgs = ['BB.png','BH.png']; // etc in right order
var myIndex = Math.floor(combo.hcs/10);
$(".myClass").css("background-image", "url(" + imgs[myIndex] + ")");
With this solution you would not need the if/else statements. For every tenth, you store an img-name in the array.
Consider a switch statement and a more concise approach to remain DRYer and cleaner:
var bg;
var x = combo.hcs;
switch (true) {
case (x>0 && x<=10):
bg = "BB";
break;
case (x>10 && x<=20):
bg = "BH";
break;
// etc....
}
$(".myClass").css("background-image", "url('" + bg + ".png'");
If you ask for a different solution, I would actually do it in a generic way. I would name images like 10.png, 20png, 30.png... 100.png
and then would load them something like this:
var imageName = Math.ceil(combo.hcs/10)*10
var imageFullName = imageName+'.png'
$(".myClass").css("background-image", "url('"+imageFullName+"')";
you will edit it but I hope you got the point, this has only 3 lines. You can also add conditions for boundary dimensions.
And about your error:
your code
if (combo.hcs <= 10) && (combo.hcs >= 1)
should be like this:
if ((combo.hcs <= 10) && (combo.hcs >= 1))
You shouldn't be closing the brackets. The brackets enclose the condition:
if (combo.hcs <= 10 && combo.hcs >= 1)
$(".myClass").css("background-image", "url('BB.png')";
You need to include your two conditions inside the same brackets, like this :
else if (combo.hcs <= 99 && combo.hcs >= 81)
The complete corrected code :
<script type="text/javascript">
$(document).ready(function(){
if (combo.hcs <= 10 && combo.hcs >= 1)
$(".myClass").css("background-image", "url('BB.png')");
else if (combo.hcs <= 20 && combo.hcs >= 11)
$(".myClass").css("background-image", "url('BH.png')");
else if (combo.hcs <= 30 && combo.hcs >= 21)
$(".myClass").css("background-image", "url('BM.png')");
else if (combo.hcs <= 40 && combo.hcs >= 31)
$(".myClass").css("background-image", "url('HB.png')");
else if (combo.hcs <= 50 && combo.hcs >= 41)
$(".myClass").css("background-image", "url('HH.png')");
else if (combo.hcs <= 60 && combo.hcs >= 51)
$(".myClass").css("background-image", "url('HM.png')");
else if (combo.hcs <= 70 && combo.hcs >= 61)
$(".myClass").css("background-image", "url('MB.pmg')");
else if (combo.hcs <= 80 && combo.hcs >= 71)
$(".myClass").css("background-image", "url('MH.png')");
else if (combo.hcs <= 99 && combo.hcs >= 81)
$(".myClass").css("background-image", "url('MM.png')");
}
</script>

Categories