For/In Loop in java script [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 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.

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.

How to use conditional (ternary) operator with nested if statements? [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 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.

Javascript: Renaming built in functions [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 4 years ago.
Improve this question
In Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector() tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
No.
Someone is going to come behind you to edit your code.
They will then have to track down your renaming function to actually see what it does.
Create an snippet in your IDE if it’s that much of an issue for you.

is rendering html using js a bad practice? [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
Please ignore my syntax.
This is just an example if this is a bad idea.
let's say I have select with 50 options but instead of typing out options 50 times.
would it be a good idea to use js and give it an array variable.
run through the variable.length then append it into html.
Is it bad to use js like this though and why?
No issues. Now clients are way too much powerful than they were earlier before.
So, utilizing some of its power never harms.
Now the problem .. rendering template on client is not an issue ..Angular, React all does the same.
While you are doing only a little which may consumes less than < 1 ms. So , just go ahead.
it is a better practice.
You are a programmer, you will never need to type out options 50 times :)

Javascript sorting vs .sort() [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 8 years ago.
Improve this question
I came across this question:.
Sort an array using javascript.
My ans: .sort() method, the other person asked me to use the bubble sort algorithm and sort it.
Does that make any difference?
As a practical matter, you are much better off using the built-in sort, which is much more efficient than a bubble sort, implemented in a much faster language than Javascript, and programmed by someone better at it than you.
As an academic assignment, though, it's a good one. You should write your own bubble sort and use the built-in to compare it for accuracy and speed.

Categories