diff --git a/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/solution.md b/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/solution.md new file mode 100644 index 00000000..35b51d9e --- /dev/null +++ b/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/solution.md @@ -0,0 +1,18 @@ +Answer: **1 and 3**. + +Both commands result in adding `text` "as text" into the `elem`. + +Here's an example: + +```html run height=80 +
+
+
+ +``` diff --git a/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/task.md b/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/task.md new file mode 100644 index 00000000..e127bc0e --- /dev/null +++ b/2-ui/1-document/11-modifying-document/1-createtextnode-vs-innerhtml/task.md @@ -0,0 +1,13 @@ +importance: 5 + +--- + +# createTextNode vs innerHTML vs textContent + +We have an empty DOM element `elem` and a string `text`. + +Which of these 3 commands do exactly the same? + +1. `elem.append(document.createTextNode(text))` +2. `elem.innerHTML = text` +3. `elem.textContent = text` diff --git a/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.md b/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.md new file mode 100644 index 00000000..15238fcf --- /dev/null +++ b/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.md @@ -0,0 +1,53 @@ +First, let's make HTML/CSS. + +Each component of the time would look great in its own ``: + +```html +
+ hh:mm:ss +
+``` + +Also we'll need CSS to color them. + +The `update` function will refresh the clock, to be called by `setInterval` every second: + +```js +function update() { + let clock = document.getElementById('clock'); +*!* + let date = new Date(); // (*) +*/!* + let hours = date.getHours(); + if (hours < 10) hours = '0' + hours; + clock.children[0].innerHTML = hours; + + let minutes = date.getMinutes(); + if (minutes < 10) minutes = '0' + minutes; + clock.children[1].innerHTML = minutes; + + let seconds = date.getSeconds(); + if (seconds < 10) seconds = '0' + seconds; + clock.children[2].innerHTML = seconds; +} +``` + +In the line `(*)` we every time check the current date. The calls to `setInterval` are not reliable: they may happen with delays. + +The clock-managing functions: + +```js +let timerId; + +function clockStart() { // run the clock + timerId = setInterval(update, 1000); + update(); // (*) +} + +function clockStop() { + clearInterval(timerId); + timerId = null; +} +``` + +Please note that the call to `update()` is not only scheduled in `clockStart()`, but immediately run in the line `(*)`. Otherwise the visitor would have to wait till the first execution of `setInterval`. And the clock would be empty till then. diff --git a/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.view/index.html b/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.view/index.html new file mode 100644 index 00000000..5c0319b6 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/10-clock-setinterval/solution.view/index.html @@ -0,0 +1,58 @@ + + + + + + + + +
+ hh:mm:ss +
+ + + + + diff --git a/2-ui/1-document/11-modifying-document/10-clock-setinterval/source.view/index.html b/2-ui/1-document/11-modifying-document/10-clock-setinterval/source.view/index.html new file mode 100644 index 00000000..ecf5df99 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/10-clock-setinterval/source.view/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/2-ui/1-document/11-modifying-document/10-clock-setinterval/task.md b/2-ui/1-document/11-modifying-document/10-clock-setinterval/task.md new file mode 100644 index 00000000..f5c52557 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/10-clock-setinterval/task.md @@ -0,0 +1,9 @@ +importance: 4 + +--- + +# Colored clock with setInterval + +Create a colored clock like here: + +[iframe src="solution" height=100] diff --git a/2-ui/1-document/11-modifying-document/4-clear-elem/solution.md b/2-ui/1-document/11-modifying-document/4-clear-elem/solution.md new file mode 100644 index 00000000..437e8b58 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/4-clear-elem/solution.md @@ -0,0 +1,32 @@ + +First, let's see how not to do it: + +```js +function clear(elem) { + for (let i=0; i < elem.childNodes.length; i++) { + elem.childNodes[i].remove(); + } +} +``` + +That won't work, because the call to `remove()` shifts the collection `elem.childNodes` every time, so elements every time start from index `0`. So `i` should not increase in the loop at all. + +The `for..of` loop also does the same. + +The right variant would be: + +```js +function clear(elem) { + while (elem.firstChild) { + elem.firstChild.remove(); + } +} +``` + +And also there's a simpler variant: + +```js +function clear(elem) { + elem.innerHTML = ''; +} +``` diff --git a/2-ui/1-document/11-modifying-document/4-clear-elem/task.md b/2-ui/1-document/11-modifying-document/4-clear-elem/task.md new file mode 100644 index 00000000..5c14caa3 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/4-clear-elem/task.md @@ -0,0 +1,20 @@ +importance: 5 + +--- + +# clear + +Create a function `clear(elem)` that removes everything from element. + +```html run +
    +
  1. Hello
  2. +
  3. World
  4. +
+ + +``` diff --git a/2-ui/1-document/11-modifying-document/5-why-aaa/solution.md b/2-ui/1-document/11-modifying-document/5-why-aaa/solution.md new file mode 100644 index 00000000..940c57e9 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/5-why-aaa/solution.md @@ -0,0 +1,5 @@ +The HTML in the task is incorrect. That's the matter. There may be no text inside the ``, only table-specific tags. + +The question can be easily solved by exploring the DOM in the browser tools. Then we'll see that the browser placed the text `"aaa"` *before* the table. + +The HTML standard thoroughly specifies how to process bad HTML, and the behavior of the browser here is correct. diff --git a/2-ui/1-document/11-modifying-document/5-why-aaa/task.md b/2-ui/1-document/11-modifying-document/5-why-aaa/task.md new file mode 100644 index 00000000..f46a01af --- /dev/null +++ b/2-ui/1-document/11-modifying-document/5-why-aaa/task.md @@ -0,0 +1,23 @@ +importance: 1 + +--- + +# Why "ааа" remains? + +Run the example. Why `table.remove()` does not delete the text `"aaa"`? + +```html height=100 run +
+ aaa + + + +
Test
+ + +``` diff --git a/2-ui/1-document/11-modifying-document/6-create-list/solution.md b/2-ui/1-document/11-modifying-document/6-create-list/solution.md new file mode 100644 index 00000000..cd3037c8 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/6-create-list/solution.md @@ -0,0 +1 @@ +Please note using `textContent` to assign the `
  • ` content. diff --git a/2-ui/1-document/11-modifying-document/6-create-list/solution.view/index.html b/2-ui/1-document/11-modifying-document/6-create-list/solution.view/index.html new file mode 100755 index 00000000..071645e8 --- /dev/null +++ b/2-ui/1-document/11-modifying-document/6-create-list/solution.view/index.html @@ -0,0 +1,24 @@ + + + +

    Create a list

    + + + + + diff --git a/2-ui/1-document/11-modifying-document/6-create-list/task.md b/2-ui/1-document/11-modifying-document/6-create-list/task.md new file mode 100644 index 00000000..43b0a34a --- /dev/null +++ b/2-ui/1-document/11-modifying-document/6-create-list/task.md @@ -0,0 +1,19 @@ +importance: 4 + +--- + +# Create a list + +Write an interface to create a list from user input. + +For every list item: + +1. Ask a user about its content using `prompt`. +2. Create the `
  • ` with it and add it to `