How to use conditional (ternary) operator with nested if statements? [closed] - javascript

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 2 years ago.
Improve this question
I want to add more nested "if statements" in the succeeding lines but I don't know what to do while using ternary operator.
Object.freeze (phoneBook)
function lookup(name, address) {
for (let i = 0; i < phoneBook.length; i++)
{
return (phonebook[i].givenName === name)? phoneBook[i][address]: "Not in the phonebook";
}

Ternary operations are hard to read if they are in any way complex. It rapidly becomes very difficult to work out what level you are at if there is much nesting.
Therefore for maintainability, especially if others are likely to have to support your code in the future, if then else statements properly laid out with indentation are probably a better way to go, even if you find yourself having to repeat return statements more than you would like.
Keep ternary statements for truly simple, one line, situations.

Related

Which JS concept can ensure the code runs? [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 1 year ago.
Improve this question
Can someone please help me understand what concept from JavaScript could ensure below line of code runs? feel free to make code changes to make it execute & provide output.
(20).add(10).multiply(2)
You can make use of prototype. Using .prototype, you are adding a property in the prototype chain to all numbers.
Any number can use it.
Number.prototype.add = function(x){
return this + x;
}
console.log((20).add(10));
Number.prototype.multiply = function(x){
return this * x;
}
console.log((20).multiply(10));
console.log((20).add(10).multiply(2));
Note: This is generally a bad practice, and should only be used for self-learning and practice.

Can every for/while loop be replaced with forEach, map, reduce, filter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
To me, it seems that whatever logic I would write in a for/while loop could be placed in the callback passed to map, reduce, filter. I'm sure you might have to modify the code some but in theory I can't image a situation where you're forced to use a for/while loop.
Can anyone provide examples where its required to use a for/while loop?
If the statement were false, no functional programming language could exist. In other words: all imperative solutions can be rewritten as declarative ones. However, there might be implementation details that can be imperative (hah) to your particular use case. For example, breaking nested loops based on complex conditions is something that is typically easier to implement in an imperative form.

How Does Javascript if statement indentation work? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
How many spaces should be used for each line of if statement at the beginning of the line?
suppose we have 10 lines of nested if statements, what is the formula to determine how many lines to be used? Also can a programmer use the tab key instead to obtain proper indentation for a statement.
if (a !== 0) {
if (b > 1) {
if (c < 1) {
Indentations and spaces is not a requirement it just helps your code to be more readable.
Unlike languages like Python in which whitespace is considered semantic by the compiler, the Javascript compiler ignores runs of whitespace altogether. In my experience, though, the most common indentation (tab) lengths are 2 spaces and 4. My personal preference is 4, but plenty of other people will say 2.
I will say, though, that if you're using more than 4, you're either going to need to break your more nested logic up into multiple lines, or you're going to end up with a lot of overruns -- even with only 4 spaces, I often find myself running over 80 characters.

Is there an IDE that highlights possible logical errors? If so, what is the best? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example, after spending an hour on the following javascript logic error, I finally realized the problem. Keep in mind that this if statement is embedded in a lot of other code.
for(i=0;i<alength;i++)
{
if(myvar = correct)
{
//ommitted irrelevent code
}
}
As you know, I was assigning the correct variable to myvar as well as comparing it, so it should have been:
if(myvar == correct)
After finally realizing the error, I was very annoyed that I didn't recognize it much faster. I have been programming for over 5 years, mostly in java, but this happens quite often and I was wondering if there is an IDE or something that can highlight possible logical errors.
You could have your code linted by JSHint.
There should be plugins for any major editors.
In this case, it would have told you "Expected a conditional expression and instead saw an assignment."

For/In Loop in java script [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 9 years ago.
Improve this question
I could understand what logic is going behind the condition inside the for loop but still i could not get whats going behind the screens. Is there any set of documentations to learn what are all the ways of putting parameters for a for loop !! ?
function getFieldNames(row) {
var FieldNames = []
for (var column in row) {
if (!row.hasOwnProperty(column))
continue;
FieldNames.push(column);
}
return FieldNames
}
Thank You .!
W3schools has documentation, and you can learn and test your code on the site.:
http://www.w3schools.com/js/js_loop_for.asp
Mozilla offers a more explicit defintion and gives examples:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
Here is an article that goes into a lot of detail and might be helpful:
http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/
EDIT: I do see w3schools has some bad formatting and practices, but the general explanation of loops is correct, and easy for someone starting out to understand.

Categories