Full-Stack Developer Question:
Download Job Interview Questions and Answers PDF
Tell me how would you empty the array below?
Answer:
Var emptyArray = [‘this’, ‘array’, ‘is’, ‘full’];
This deceptively simple question is designed to test your prospective coder’s awareness of mitigating potential bugs when solving problems. The easiest method would be to set “emptyArray” equal to “[ ]”—which creates a new empty array. However, if the array is referenced anywhere else, the original array will remain unchanged. A more robust method would be “emptyArray.length – 0;”—which not only clears the array but updates all reference variables that point to this original array. Some possible solutions are listed below:
emptyArray.length = 0;
emptyArray.splice(0, emptyArray.length);
while(emptyArray.length){
emptyArray.pop();
}
emptyArray = []
This deceptively simple question is designed to test your prospective coder’s awareness of mitigating potential bugs when solving problems. The easiest method would be to set “emptyArray” equal to “[ ]”—which creates a new empty array. However, if the array is referenced anywhere else, the original array will remain unchanged. A more robust method would be “emptyArray.length – 0;”—which not only clears the array but updates all reference variables that point to this original array. Some possible solutions are listed below:
emptyArray.length = 0;
emptyArray.splice(0, emptyArray.length);
while(emptyArray.length){
emptyArray.pop();
}
emptyArray = []
Download Full-Stack Developer Interview Questions And Answers
PDF
Previous Question | Next Question |
Tell us the difference between classical inheritance and prototypal inheritance? | Do you know what is a RESTful Web Service? |