Skip to main content

Command Palette

Search for a command to run...

Conceptual Question for the interview

INterview question for Javascript ??

Updated
โ€ข1 min readโ€ขView as Markdown
Conceptual Question for the interview
S
"Programming is a craft, more than it is science" said by someone, supported by me ๐Ÿคฃ

Q)Take an array and loop its original Properties. ?

and:- Original properties means you may use an array prototype to insert addition property in it.

i.e. This means you should only loop through the original values of the array, ignoring any extra properties.๐Ÿค”

Array.prototype.extraproperty = "SAYAN";
const  mynewArray = [1,2,3,4,5]
for(let v in mynewArray){
    console.log(v)
}

Output:- 
0
1
2
3
4
extraproperty

Understanding

Correct and:-

for(let v in myNewarray){
    if(myNewArray.hasownproperty(v){
        console.log(v)
}
}

output:-
0
1
2
3
4

Concept:-- By using prototype we can insert new prototype in an array. but those will not be considered as original properties.

So, to get original props just add a check to array elements using "hasownproperty" syntax.

the "hasownproperty()" method in js in used to check if an object has a property that belong only to itself and is not inherited its prototype chain.๐Ÿ™‚