javascript if else for beginners - javascript

I am very new to Javascript, so I am having a hard time figuring out this easy exercise and what I'm doing this wrong. Any help would be greatly appreciated!
You are given two numeric variables: var n = 25; var result = 0;
Their values may change when you submit.
DO NOT EDIT CODE ABOVE THIS LINE.
=================================
Your Challenge:
Using if and else, make decisions based on the values
of n and result with the following rules:
1. When n is even,
set the value of result to result's current value
plus 10.
2. When n is odd,
set the value of result to result's current value
minus the value of n.
3. Do not declare new variables.
4. Be sure your solution works for all values of n and result.
if (n%2 == 0) {result+10;} else {result-n;}

Your problem isn't if/else, the problem is you never set result to the new value. result+10 just results in the value without storing that value anywhere. (In many programming langauges, that would be a syntax error, but JavaScript allows what it calls ExpressionStatement where any expression can be a statement.)
Either use the compound assignment operators (+= and -=), or write a verbose assignment (result = ...).
Side note: It's easier to debug and edit code when statements are on their own lines, suggest:
if (condition) {
trueStuffHere
} else {
falseStuffHere
}
...or any of the several variations on that theme where trueStuffHere and falseStuffHere are on lines of their own.

You may set the result?
if (n%2 == 0) {
result = result + 10;
} else {
result = result - n;
}
Or if you're a bit better:
if (n % 2 == 0) {
result += 10;
} else {
result -=n;
}

Related

Keep adding numbers into an array until a = 0

Hi I'm new to javascript and I'm sorry if my question is silly for you.
This question was solved with the following code:
let arr = [12, 6, 53]
let a
do {
a = prompt("Enter a number")
a = Number.parseInt(a)
arr.push(a)
}
while (a != 0)
console.log(arr)
But, if I want that run the code if a == 0 rather than a != 0
i simply changed
while (a != 0)
into
while (a == 0)
But, it simply adds the number into the array
Can anyone please explain why this is happening?
I was expecting that the output should keep asking me to enter a number until a is equal to 0.
I'm a bit silly (Please explain me if I'm right or wrong) I think that running the code until a != 0 and a == 0 should give the same answer in this code
First off, stop using do..while. Its usages are very limited and 0.01% of use cases where it actually makes sense are simply not worth the trouble of having yet another syntax construct in your codebase.
In your case, the use of do is not justified, because you want to check the condition (a!=0) before you perform the action (arr.push), not after it. The right pattern here is "loop forever + break":
while (true) {
let a = prompt("Enter a number")
a = Number.parseInt(a)
if (a !== 0) {
break
}
arr.push(a)
}
Also note that 1) block-scoped variables belong inside the block, not outside it and 2) declaration without initialization (as in let a;) is a code smell and should be avoided.
Hope this helps.
Can anyone please explain why this is happening?
I was expecting that the output should keep asking me to enter a
number until a is equal to 0. I'm a bit silly (Please explain me if
I'm right or wrong) I think that running the code until a != 0 and a
== 0 should give the same answer in this code.
a != 0
Add items to the array if they do not equal 0 (else stop)
a == 0
Add items to the array if they equal 0 (else stop)
If you do not want the numbers that do not match adding to the array, then you need to also check this condition before inserting the value (before push).
let arr = [12, 6, 53];
let a;
do {
a = prompt("Enter a number");
a = Number.parseInt(a);
if (a == 0) {
arr.push(a)
};
}
while (a == 0);
console.log(arr);

JS array being overwritten by bolean values when being read

First time around here, and I'm an awful dev, so forgive in advance my possibly numerous mistakes :)
here's my issue: I'm trying to make a program that checks the proportions of each number from 1 to 9 in an array (I'm putting Benford's Law to the test), for that I have created 9 different variables (basically nX where X=1-9), I'm reading through my array with a for loop, and incrementing on each of my variables every time that the corresponding number is found (if array[i]= X, nX++), and I then console log my array. The issue is that on that console log, all values in my array are set to "1", which isn't the case prior to the execution of these few lines. So I'm not quite sure what happens, but I'm guessing that my "if" checks are returning "true" and changing the value of my array elements to 1 to reflect that.
So my question would be: any of you wise wizards know what I'm doing wrong and how to keep my data from being corrupted by this operation?
here's some code:
for (i = 0; i < benfordArrayProcessed.length; i++) {
if (benfordArrayProcessed[i] = 1) {
n1++;
} else if (benfordArrayProcessed[i] = 2) {
n2++;
} else if (benfordArrayProcessed[i] = 3) {
n3++;
}
[...]
In your expression, you need use boolean expression instead of assignment, e.g. if (benfordArrayProcessed[i] == 1). The values in the error get overwritten with because of assignment int he first statement itself and that's the reason you see 1 for all the elements.
Apart from the == usage, you can also chose to go for a switch statement:
for (i = 0; i < benfordArrayProcessed.length; i++) {
switch (benfordArrayProcessed[i]) {
case 1:
n1++;
break;
case 2:
n2++;
break;
case 3:
n3++;
break;
[...]
}
}
Or, use an object with keys to up the numbers (a bit more tricky and depending on how you want to implement the rest of your code):
const n = {
1: 0,
2: 0,
3: 0,
};
for (i = 0; i < benfordArrayProcessed.length; i++) {
const num = benfordArrayProcessed[i];
n[num]++;
}

Assigning String to Variable with If Else Statements

sorry this is a super basic question, but I'm not too familiar with google script yet so finding answers to these simple tasks is proving to be difficult. In this code, I'm trying to set a string of numbers to a variable based on another variable. AppNaming is the variable I'm trying to assign the numbers to. AppType is already defined earlier in the code, and depending on the type, I need it to take only part of the variable AppNumber. Depending on the AppType, the AppNumber is separated by commas or slashes, hence the indexOf parts. Are if statements not allowed with var functions? I need the var AppNaming to change based on the AppType in order to name a file later on in the code. Thanks for all the help and sorry again if these questions are annoying; I'm still learning.
function AutoFill(e) {
// variables all defined earlier
//Naming code
var AppNaming = if( AppType == '1' ){
AppNumber.substring( 0, AppNumber.indexOf(","))
}
else if ( AppType == '2'){
AppNumber.substring( 0, AppNumber.indexOf("/"))
}
else ( AppType == '3'){
AppNumber.substring( 0, AppNumber.indexOf(","))
}
You can only assign values to variables. Unfortunately, if statements are not values so you can't say var a = if (...) { } but you can say var a = 3
An if statement controls the flow of the application.
var something = 20;
var a;
if (something > 20) {
a = "this block runs if something is greater than 20";
} else if (something < 20) {
a = "this block runs if something is less than 20";
} else {
a = "this block runs otherwise";
}
// The value of 'a' is 'this block runs otherwise' at this point
So in your example, you can assign App.Number.substring(0, AppNumber.indexOf(",") to a variable because that expression will return a value.
I would recommend you to learn the basics of JavaScript.

Fibonacci for large numbers in Javascript

I have the following code:
function fib(n) {
let first=BigInt(0);
let snd=BigInt(1);
let currentNumber;
let countMax=Math.abs(n)+1;
let counter=2;
if(n==0){
return first;
}
else if (n==1||n==-1){
return snd;
}
while(counter<countMax)
{
currentNumber=first+snd;
first=snd;
snd=currentNumber;
counter++;
}
if((n<0) && (n % 2 ==0))
{
return -currentNumber;
}
return currentNumber;
}
That returns the fibonacci number for the given (n).
My issue is that I have to improve the performance of this code. I tried to use different fibonacci formulas (exponential ones) but I lose a lot of precision cause phi number has infinite decimals, so I have to truncate and for big numbers I lost a lot of precision.
When I execute for instance fib(200000) I get the huge number but the code spends more than 12000 ms.
For other hand I tried using recursion but the performance decreases.
Could you provide me an article or clue to follow?
Thanks & Regards.
First of all, you can refer the answer here which says that
Fib(-n) = -Fib(n)
Here's the recursive implementation which is not efficient as you mentioned
function fib(n) {
// This is to handle positive and negative numbers
var sign = n >= 0 ? 1 : -1;
n = Math.abs(n);
// Now the usual Fibonacci function
if(n < 2)
return sign*n;
return sign*(fib(n-1) + fib(n-2));
}
This is pretty straightforward and I leave it without explaining because if you know Fibonacci series, you know what the above code does. As you already know, this is not good for very large numbers as it recursively calculate the same thing again and again. But we'll use it in our approach later on.
Now coming towards a better approach. See the below code similar to your code just a bit concise.
function fib(n) {
if(n == 0)
return 0;
var a = 1;
var b = 1;
while(n > 2) {
b = a + b;
a = b - a;
}
// If n is negative then return negative of fib(n)
return n < 0 ? -1*b : b;
}
This code is better to use when you want to call this function only a few times. But if you want to call it for frequently, then you'll end up calculating the same thing many times. Here you should keep track of already calculated values.
For example, if you call fib(n) it will calculate nth Fibonacci number and return it. For the next time if you call fib(n) it will again calculate it and return the result.
What if we store this value somewhere and next time retrieve it whenever required?
This will also help in calculating Fibonacci numbers greater than nth Fibonacci number.
How?
Say we have to calculate fib(n+1), then by definition fib(n+1) = fib(n) + fib(n-1). Because, we already have fib(n) calculated and stored somewhere we can just use that stored value. Also, if we have fib(n) calculated and stored, we already have fib(n-1) calculated and stored. Read it again.
We can do this by using a JavaScript object and the same recursive function we used above (Yes, the recursive one!). See the below code.
// This object will store already calculated values
// This should be in the global scope or atleast the parent scope
var memo = {};
// We know fib(0) = 0, fib(1) = 1, so store it
memo[0] = 0;
memo[1] = 1;
function fib(n) {
var sign = n >= 0 ? 1 : -1;
n = Math.abs(n);
// If we already calculated the value, just use the same
if(memo[n] !== undefined)
return sign*memo[n];
// Else we will calculate it and store it and also return it
return sign*(memo[n] = fib(n-1) + fib(n-2));
}
// This will calculate fib(2), fib(3), fib(4) and fib(5).
// Why it does not calculate fib(0) and fib(1) ?
// Because it's already calculated, goto line 1 of this code snippet
console.log(fib(5)); // 5
// This will not calculate anything
// Because fib(-5) is -fib(5) and we already calculated fib(5)
console.log(fib(-5)); // -5
// This will also not calculate anything
// Because we already calculated fib(4) while calculating fib(5)
console.log(fib(4)); // 3
// This will calculate only fib(6) and fib(7)
console.log(fib(7)); // 13
Try out some test cases. It's easy to understand why this is faster.
Now you know you can store the already calculated values, you can modify your solution to use this approach without using recursion as for large numbers the recursive approach will throw Uncaught RangeError. I leave this to you because it's worth trying on your own!
This solution uses a concept in programming called Dynamic Programming. You can refer it here.
If you just add the previous value to the current one and then use the old current value as the previous one you get a significant improvement in performance.
function fib(n) {
var current = 1;
var previous = 0;
while (--n) {
var temp = current;
current += previous;
previous = temp;
}
return current;
}
console.log(fib(1)); // 1
console.log(fib(2)); // 1
console.log(fib(3)); // 2
console.log(fib(4)); // 3
console.log(fib(5)); // 5
You can also use an array in the parent scope to store the previous values to avoid redoing the same calculations.
var fibMap = [1, 1];
function fib(n) {
var current = fibMap[fibMap.length - 1];
var previous = fibMap[fibMap.length - 2];
while (fibMap.length < n) {
var temp = current;
current += previous;
previous = temp;
fibMap.push(current);
}
return fibMap[n - 1];
}
console.log(fib(1)); // 1
console.log(fib(2)); // 1
console.log(fib(3)); // 2
console.log(fib(4)); // 3
console.log(fib(5)); // 5
Benchmark for getting the 1000th number 3 times

If statement is not working properly

I'm trying to change the condition in which data is written to a table. I noticed a strange result when trying to change this: it seems WriteToTable function would runno matter what if condition I subjected it to. To test this I did the following:
var TestThis=0;
if (TestThis=1000){
WriteToTable(iPlaceDisplayNum, place.name, place.rating, xScoreFinal, iProspect, place.url, place.formatted_phone_number);
alert ('This alert should not be displaying.');
}
The function will still execute and the alert will be still be displayed when the script runs. I'm not sure why?
Here's the rest of the function, the problem is towards the bottom:
function printme(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
if (typeof place.reviews !== 'undefined') {
var xScore = 0;
var xGlobal = 0;
for (var i = 0; i < place.reviews.length; i++) {
reviews = place.reviews[i];
for (var x = 0; x < reviews.aspects.length; x++) {
aspectr = reviews.aspects[x];
xScore += aspectr.rating;
xGlobal++;
}
}
var xScoreFinal = (xScore / xGlobal);
}
if (typeof xScoreFinal !== 'undefined') {
iPlaceDisplayNum++;
var iProspect;
if (xScoreFinal < 2.3) {
iProspect = 'Yes';
}
//Not sure what's going on here
var TestThis=0;
if (TestThis=1000){
WriteToTable(iPlaceDisplayNum, place.name, place.rating, xScoreFinal, iProspect, place.url, place.formatted_phone_number);
alert ('This alert should not be displaying.');
}
}
}
}
You are assigning a value to your variable in your if condition check. Your TestThis variable is being assigned value 1000, which will be true after being converted to boolean by JavaScript. That's why your function is being always executed. You can read more about the automatic type conversion here.
Now to fix your code, change this -
if (TestThis=1000)
to this -
if (TestThis == 1000)
or if you don't want automatic type conversion -
if (TestThis === 1000)
Sometimes people like to reverse the values in the comparison, in the following way -
if (1000 === TestThis)
This is called a Yoda Condition (yeah, named after the Grand Jedi Master Yoda) . The benefit is that in case someone mistakenly puts only a single equal, it will result in an error as you cannot assign anything to a constant. I have never used it personally though (and probably never will because I find it rather unconventional).
JavaScript allows you to assign a value in a conditional, so this TestThis=1000 results to 1000 and in a conditional statement positive numbers (actually anything not 0) result to an evaluation to true.
To make it a conditional, you should do TestThis===1000 (and you should almost always use the === over the == as the === forces an actual comparison of the two and doesn't try to convert one part of the conditional to equal the other.)
You can also do 1000 === TestThis (or conversly 1000 == TestThis) Some people say this is bad coding, because it's difficult to read. I'll leave that up to you to decide, but this absolutely won't allow you to accidentally assign a value in the conditional because you can't assign a value to 1000.
In the if statement, you're setting TestThis to 1000, rather than comparing it to 1000. The = operator returns the value that was set, which evaluates to true because it is not undefined, 0, or null. You simply need to use the == operator.
if(TestThis == 1000)
if (TestThis == 1000)
Change like this.
For comparing equality in if you must have ==
Change:
if (TestThis=1000)
To:
if (TestThis==1000)
You're actually assigning to TestThis which will return true and execute the conditional block.

Categories

Resources