[x]cube LABS React.js Interview
1. Tell me about experience and previous projects
2. Closure
3. Deep Copy & Shallow Copy'
4. Dynamic Scope
5. Memory Leak
6. React Hooks
7. Dynamic Routing
8. Redux & Redux Lifecycle
9. Causes of unnecessary Rerendering.
10. Debouncing,
11. Event Loop
12. Write a logic to sum of all numbers
const obj = { a: 1, b: { c: 2, d: { e: 3 } } };
Code:
function sumNestedValues(obj) {
let sum = 0;
function recursiveSum(value) {
if (typeof value === 'number') {
sum += value;
} else if (typeof value === 'object' && value !== null) {
for (let key in value) {
recursiveSum(value[key]);
}
}
}
recursiveSum(obj);
return sum;
}
const result = sumNestedValues(obj);
console.log(result); // Output: 6
Comments
Post a Comment