Shivam Jha
@oops_Shivam
Followers
3
Following
190
Media
7
Statuses
256
Passionate about Web Development | Currently pursuing https://t.co/loIwRzBgK2 at VITB | Crafting the future of web dev (TO VIEW THE FULL CONTENTS OF THE TWEETS CLICK ON IT)
Joined January 2024
Done with the series.... Planning for something related to it... Stay tuned
0
0
2
Output for the given parameters is: [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ]
0
0
2
#Day62 of learning #javascript from scratch... click on the tweet to view full content. Today we will learn to Use Recursion to Create a Range of Numbers
1
0
2
This happens because the push happens last, after the recursive call has returned. At the point where n is pushed into the array, countup(n - 1) has already been evaluated and returned [1, 2, ..., n - 1].
0
0
1
The value [1, 2, 3, 4, 5] will be displayed in the console. At first, this seems counterintuitive since the value of n decreases, but the values in the final array are increasing.
1
0
1
function countup(n) { if (n < 1) { return []; } else { const countArray = countup(n - 1); countArray.push(n); return countArray; } } console.log(countup(5));
1
0
1
This function will need to accept an argument, n, representing the final number. Then it will need to call itself with progressively smaller values of n until it reaches 1. You could write the function as follows:
1
0
1
For example, say you want to write a recursive function that returns an array containing the numbers 1 through n.
1
0
1
There will also be a recursive call which executes the original function with different arguments. If the function is written correctly, eventually the base case will be reached.
1
0
1
As mentioned in the previous challenge, there will be a base case. The base case tells the recursive function when it no longer needs to call itself. It is a simple case where the return value is already known.
1
0
1
In a previous challenge, you learned how to use recursion to replace a for loop. Now, let's look at a more complex function that returns an array of consecutive integers starting with 1 through the number passed to the function.
1
0
1
#Day61 of learning #javascript from scratch... click on the tweet to view full content. Today we will learn to Use Recursion to Create a Countdown
1
0
1
function findGreaterOrEqual(a, b) { return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater"; }
0
0
1
It is considered best practice to format multiple conditional operators such that each condition is on a separate line, as shown above. Using multiple conditional operators without proper indentation may make your code hard to read. For example:
1
0
1
The above function can be re-written using multiple conditional operators: function findGreaterOrEqual(a, b) { return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater"; }
1
0
1
function findGreaterOrEqual(a, b) { if (a === b) { return "a and b are equal"; } else if (a > b) { return "a is greater"; } else { return "b is greater"; } }
1
0
1
In the previous challenge, you used a single conditional operator. You can also chain them together to check for multiple conditions. The following function uses if, else if, and else statements to check multiple conditions
1
0
1
#Day60 of learning #javascript from scratch... click on the tweet to view full content. Today we will learn to Use Multiple Conditional (Ternary) Operators
1
0
1
This can be re-written using the conditional operator: function findGreater(a, b) { return a > b ? "a is greater" : "b is greater or equal"; }
0
0
1
The following function uses an if/else statement to check a condition: function findGreater(a, b) { if(a > b) { return "a is greater"; } else { return "b is greater or equal"; } }
1
0
1