I saw a term today: healthy narcissism. What the hell is that? Are people trying to make this a thing? It’s not a thing. A healthy ego and high self-confidence, and self-regard are not narcissism. If one is not exploiting another, then it is not narcissism. Narcissism is an obsession with the self and an inability to see through another’s perspective. True narcissists cannot show genuine empathy. It is one of the defining features of their mental disorder. People that have a few narcissistic traits are very different.
All I did today was code multiple conditional operators in JavaScript. I’m starting to get it. Coding the conditional operator from a visual code is easy, but the problem is always a word problem. And that is where I need help figuring out what they want in the word problem.
Karissa did my laundry today. I will put it away tomorrow. Maybe tonight, we’ll see.
I just remembered that I need to get the mail. I promised Kel I would get the mail all week, so I will head out now.
JavaScript notes:
—————————————-
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:
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"; } }
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"; }
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:
function findGreaterOrEqual(a, b) { return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater"; }
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.
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. 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.
For example, say you want to write a recursive function that returns an array containing the numbers 1 through n. 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:
function countup(n) { if (n < 1) { return []; } else { const countArray = countup(n - 1); countArray.push(n); return countArray; } } console.log(countup(5));
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. 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].