DOM Manipulation – Odin Project Web Dev 101

I finished studying the Odin Project Web Dev 101 – DOM Manipulation.

Here were the learning outcomes and my answers.

Learning Outcomes:

  • What is DOM in relation to a webpage?

The DOM is the Document Object Model. It is a tree-like structure for the contents of a webpage.
There is a tree of “nodes” with different relationships. Like child or parent.

  • What’s the difference between a “node” and an “element”?

An element is a specific node in the DOM.
A node can include elements, text content inside an element, the document itself, “fragments, and even code comments (the ones that don’t display on the page, like between /* */ in CSS, in HTML, or // in JavaScript).

  • How do you target nodes with “selectors”?

You can use:

  1. CSS-style selectors.
  2. Relationship properties.

1. CSS-style selectors for:
Screenshot 2019-02-21 at 22.26.13.png

  • div.display
  • .display
  • #container > .display
  • div#container > div.display

2. Relationship properties (identify nodes based on relationships to nodes around it)

  • firstElementChild
  • previousElementSibling

You use these selections with a “Query Selector”.
e.g. document.querySelector(“.display”);

  • What are the basic methods for finding/adding/removing and altering DOM nodes?

Finding
– Use Query Selectors

Adding
– Create an element and then append it

document.createElement(tagName[, options])
then
parentNode.appendChild(childNode)

Removing
– Remove the node
e.g. parentNode.removeChild(div);

Altering
– Once you have referenced an element, you can add, edit, or remove things from it. Like add CSS styles or remove CSS classes.
E.g.,

To add an inline style of blue:
div.style.color = ‘blue’;
To add a new ID:
div.setAttribute(‘id’, ‘theDiv’); sets the id attribute of our div to theDiv.

  • What is the difference between a “nodelist” and an “array of nodes”?

It is a list of nodes. It looks like an array but a nodelist is an object, not an array. You cannot apply the same methods to it as you would to an array.
An array of nodes is when a nodelist is converted into an array, and the regular array methods will work on it.
To convert a nodelist to an array you can use a  spread operator ( . . . ) or Array.from().

  • How do “events” and “listeners” work? What are three ways to use events in your code?

Event
An event is generally something the computer can react to. Like something that the user does (an input) that can be tracked. Like a mouse click or button press.

Listener
A listener is set up to track when the event happens on a particular DOM node. An event listener listens for this input.
The event listener can be set up so that when it detects that the event has occurred, it runs more code to do something else.

E.g. If the event is a mouse click and the login button is set up with an event listener for mouse clicks. When the user clicks on the login button, the event listener detects that the button has been clicked on, it runs the code for showing the Username and Password input box above the login button.

You can set up these events in three ways:
1. Attaching Scripts to HTML elements as an attribute:

<button onclick=”showUserAndPassBox()”>Login</button>
2. Setting the “on_event_” property on the DOM object in your JavaScript:

// the html file
<button id=”btn”>Login</button>

// the JavaScript file
var btn = document.querySelector(‘#btn’);
btn.onclick = (e) => showUserAndPassBox();

3. Event Listeners in JavaScript:

// the html file
<button id=”btn”>Login</button>

// the JavaScript file
var btn = document.querySelector(‘#btn’);
btn.addEventListener(‘click’, (e) => {
showUserAndPassBox();
});

  • How does “bubbling” work?

If you apply an event listener to a piece of code that is embedded in another piece of code, the event listener will apply to the parent element as well.
E.g.,

This is the actual target of the event listener.

If you clicked anywhere on that div it will fire the event listener because it “bubbles” up.
The process is likened to bubbles because events bubble up from the inner element up through parents like a bubble in the water.

It is an efficient method for firing an event on multiple elements — starting from the innermost element — and “bubbling” up to outer elements.

(Note: Not all events bubble and you can stop events bubbling using the event.stopPropagation() method)

Advertisements

JavaScript Fundamentals Part 2

Last year, I continued studying on Fundamentals of Javascript in the Odin Project Web Development 101. I decided to start off the New Year by doing a review of what I had studied a month ago.

I did the first JavaScript “Test Yourself” questions in this blog post.

These are the second JavaScript “test yourself” questions here and my answers below.

  • What are the seven data types of javascript?
  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Object
  7. Symbol
  • Which data type is NOT primitive?

Object.
This is because objects store collections of data and more complex entities.
Whereas, numbers, strings, booleans, null, undefined, and symbol are represented directly. Also, those data types cannot be changed (although, the variables they may be stored in can change).

  • What is the difference between single, double, and backtick quotes for strings?

Single and double quotes are the same. But they need to be used consistently, not mixed – ‘Hi’ or “Hi”
not “Hi’

Backtick quotes are extended functionality quotes. They allow us to embed variables and expressions into a string using  backticks:
stringtext ${variable} stringtext `

  • Which type of quote lets you embed variables/expressions into a string?

Backticks.

  • How do you embed variables/expressions into a string?

Using the dollar sign and { } brackets together.
The variable or expression would be written within the curly brackets.

  • How do you escape characters in a string?

​​​​​You can escape characters, like a  single, double, or backtick quote with a backslash before them \’ \” \`

  • What is the difference between slice/substring/substr?

These are three ways to extract part of a string.
The parameters are different in each:

  1. slice(start, end)
  2. substring(start, end)
  3. substr(start, length)

Slice extracts part of a string and returns the extracted part in a new sting.
Substring is similar to slice. Substr is different because you choose the length of the string you want to get, rather than the end point of the original string.

  • What are methods?

A method is a bit of functionality that is built into the language. They are properties which contain a function. So, slice(), substring(), and substr() are methods.
Also, indexOf() and search() are methods.

  • What are the three logical operators and what do they stand for?

&& AND

|| OR

! NOT

  • What are the comparison operators?

> greater than
< less than
>= greater than or equals
<= less than or equals
== equality check
!= not equals

  • What is nesting?

When we put something inside something else.
In JavaScript, we can nest if statements within each other. For example,
if (condition) {
xyz } else if (condition) {
zyx } else {
abc }

  • What are truthy and falsy values?

A value that evaluates as true in a Boolean context is truthy.
A value that evaluates as false in a Boolean context is falsy.

  • What are the falsy values in Javascript?
  1. false
  2. undefined
  3. null
  4. 0
  5. NaN
  • What is the syntax for an if/else if/else conditional?

if (condition) {
xyz } else if (condition) {
zyx } else {
abc }

  • What is the syntax for a switch statement?

switch (expression) {
case choice 1:
run this code
break;
case choice 2:
run this code
break;
default:
actually, just run this code
}

  • What is the syntax for a ternary operator?

(condition) ? run this code : run this code instead;

  • What is the relationship between null and undefined?

Null is a value. It means “nothing, empty, no value”.
Whereas, Undefined means there is a variable but “value has not been assigned”.

If you test equality:
null == undefined would be true
null === undefined would be false

This is because with strict equality they need to be the same type to return true. Null is an object, but undefined is a type of value itself.

  • What are conditionals?

Conditionals are statements which execute a block of code is certain conditions are true.
Conditional statements include:

  1. if
  2. else
  3. else if
  4. switch

 

Git and GitHub

I studied a bit about Git and GitHub recently by following the track on Odin and watching Le Wagon’s Git and GitHub movie.

Yesterday, I created a repository on GitHub.

Then I cloned it on my local machine.

I used the command line to start tracking that repository with Git on my machine, then I created new files in the local repository.

I made a README.md file. Then I added it to the staging area. Then committed the file with a short message about what I did.

I repeated this process with a hello-world.txt file.

Then I pushed my changes to GitHub.

You can see the repository here.

It was a very simple thing but actually implementing what I had learned about Git, GitHub and the command line and seeing it come up on GitHub felt like a proud moment for me last night.

Odin Project – Community

I was working through the Odin Project course.

The first course is Web Development 101.
There is a lesson called “Your First Rails Application”.

After learning about the command line, you learn how to configure Git and GitHub.

Then you follow the steps to create a Ruby on Rails web application.
Which you can see here:
https://github.com/rebeccode/OdinSetup

After my initial commit after following all of the instructions – it didn’t work.

I couldn’t figure out why and I wasn’t sure what to do.

I went to the Odin Project Gitter – which is like a community forum and chat for people interested in the Odin project.

I posted my issue:

odinprojectstuck.png

I wasn’t expecting any response – but actually two or three people tried to help me.

One person helped to solve the issue and once I’d made the change he suggested it the code compiled and it worked!

odinprojectunstuck.png

Within 20 minutes of my posting my query on the Gitter chatroom I had managed to fix everything!

I felt this sense of appreciation and pride – for the first time I felt like I’d become part of a community here.

My first rails app can be found here.
It is the code provided by the Odin Project for a kind of car naming list. You can add new cars, edit ones already in the list, and erase them from the list.

I don’t really understand all of what I did there because I was just following instructions. As I progress through Odin it’s going to go back and teach topics in more detail.

Next, I’m going to be learning more in depth about Git and Github.

Coding summer school for kids in Koenji, Tokyo

I saw a computer school in Koenji, Tokyo which is advertising for a summer school for children.

programmingflyerset.png
Image © Rebeccode

The flyer is advertising a course using a program like Scratch to teach block based programming to grade 1-4 elementary school  students.

It is a three hour course over two days (for a total of 6 hours) for 9,800 yen.
That’s about the same price as two day tickets for children to Tokyo Disneyland – whereas investing in their future and having them try out coding could be seen as a valuable experience.

The flyer also mentioned that being an IT engineer or programmer was the number one job that Japanese boys dreamed of becoming in 2017 – at 24% of children surveyed.

Unfortunately this course has already finished as the summer vacation period is coming to an end.

Accenture Digital Skills Free Courses

FutureLearn (a mass open online course site) is partnered with Accenture and they are providing a bunch of free accredited courses with no deadline (i.e. you get a certificate at the end that you can show with a transcript of what you studied and you can take as long as you like to do them).

Their digital skills courses include:

  • User Experience (UX)
  • Retail
  • Digital Marketing
  • Web Analytics
  • Grow your career
  • Social Media
  • Mobile

Each course takes 2-3 weeks to complete at 1-2 hours per week.

You can look at the details on their website here.

I’m particularly interested in the UX course (that’s how I found this series of courses).

I signed up for them all and I’ll update here about my progress with them and a review at the end.

My first job rejection!

It’s Monday and I was in my office today. I’ve been using my lunch breaks for the past week at work to study programming.

Today I was still working on the “Learn the Command Line” course on Codecademy – I’m 53% through now.

Today I learned about redirecting the input from commands.
This allows you to combine actions – for example, sorting a file into alphabetical order, then erasing duplicate entries in the list, then outputting it to a new file.

I also got my first ever rejection email from an IT related job (which is not a surprise, considering I have only been researching the IT field for about 2 months).

Printscreen of email
Image © Rebeccode

I never thought I’d feel happy about a rejection – but I feel like it is a good thing for me.

I can see now I’m not good enough.

Each day I’m learning new things and the effort I put in will change my life.