Answer the question
In order to leave comments, you need to log in
How does this code work?
Please tell me in simple words how the recursive function that checks the keys works in this code
<!DOCTYPE html>
<html>
<body>
<div id="container"></div>
<script>
let data = {
"Рыбы": {
"форель": {},
"лосось": {}
},
"Деревья": {
"Огромные": {
"секвойя": {},
"дуб": {}
},
"Цветковые": {
"яблоня": {},
"магнолия": {}
}
}
};
function createTree(container, obj) {
container.innerHTML = createTreeText(obj);
}
function createTreeText(obj) { // отдельная рекурсивная функция
let li = '';
let ul;
for (let key in obj) {
li += '<li>' + key + createTreeText(obj[key]) + '</li>';
}
if (li) {
ul = '<ul>' + li + '</ul>'
}
return ul || '';
}
createTree(container, data);
</script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Information about the execution process of a running function is stored in its execution context.
An execution context is a special internal data structure that contains information about a function call. It includes a specific place in the code where the interpreter is located, local variables of the function, and other service information.
One function call has exactly one execution context associated with it.
When a function makes a nested call, the following happens:
'<li>' + key + сюда + '</li>'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question