Final Flashcards

1
Q

The Two Types of Validation?

A

-Client-side validation (Using HTML, CSS and JavaScript)
-Server-side validation (Using server-side languages such as PHP)
However, it is important to know that server-side validation MUST always be performed even if client-side validation is being performed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the type attribute allow you to do?

A

< input type = “email” >

The “type” attribute allows developers to implement constraints on user input. Some “type” values such as “email” or “URL” automatically checks whether the value is a valid email or URL.

Some “type” attributes must be used with other attributes in order to enforce validation checks. For example, an ioput with type=”number” may also use the min and max attributes to enforce a minimum and maximum value on the user’s input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the required attribute do?

A

Can be used in an < input required> to force user to input data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the pattern attribute do? and provide examples

A

Country code: < input type=”text” name=”country_code”
pattern=”[A-Za-z]{{3}}” >

Input can be any letter case insensitive, {3} characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Three types of variables when routing?

A

req.query variables(?), req.body variables, req.params variables(/variable).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to use CSS to have the text change to red when user inputs invalid data?

A

:valid or :invalid pseudoclasses
EX
input:invalid {
border: 2px dashed red;
}

input:valid {
border: 2px solid black;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Connect, Prepare, Execute

A

Performed when trying to access a database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The default error message user interface provided by the browser may not be suitable for some applications.

Provide 2 examples that aren’t the default that use validation API.
Provide the following Custom error messages:
1. Custom error message text
2. Custom user Interface

A
  1. let email = document.getElementById(“mail”);

email.addEventListener(“input”, function (event) {
if (email.validity.typeMismatch) {
email.setCustomValidity(“I expect an e-mail!”);
} else {
email.setCustomValidity(“”);
}
});

  1. HTML CODE:
    < form novalidate >
    < input type=”email” id=”mail” name=”mail”>
    < span class=”error” aria-live=”polite”>< /span>
    JS code:
    let form = document.getElementsByTagName(‘form’)[0];
    let email = document.getElementById(‘mail’);
    let error = document.querySelector(‘.error’);

email.addEventListener(“input”, function (event) {
if (email.validity.valid) {
error.innerHTML = “”; // Reset the content of the message
error.className = “error”;
}
}, false);
form.addEventListener(“submit”, function (event) {

if (!email.validity.valid) {
  error.innerHTML = "I expect an e-mail!";
  error.className = "error active";
  event.preventDefault();
}   }, false);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Provide an example that doesn’t use validation API to display an error message?

A

let targetForm = document.getElementById(“simpleForm”);
targetElement.addEventListener(‘submit’, handlerFunction, false);

function handlerFunction(e){
  e.preventDefault();
  let usernameElement = document.getElementById("username");
  if(usernameElement.value.length < 8){
    console.log("Username must be more than 8 characters long");
    return false;
  }else{
    targetForm.submit();
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain how javascript can perform text validation?

A

While a few validation rules may be implemented using simple String functions, comparison operators and if statements, JavaScript can also use Regular Expressions for more complex pattern matching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show syntax for regex and its use

A

let regex = RegExp(‘[A-Za-z]{{3}}’);
#RegExp( [A-Za-z] <- all letters {{3}} <- exactly 3 characters
if (regex.test(string)){
}
else{}

regex.test returns a true or false value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Syntax to instantiate a new object and how to instantiate it using literal syntax?

A

var car = {}
var car = new Object() //literal syntax

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show the code for creating an object called car that has a make, year, and color. Create it using both Literal syntax and Dot syntax.

A

//Literal syntax
var car = {make:”Honda”, year:2007, color:”red”}
//Dot syntax
car.make = “Honda
car.year = 2007
car.color = “red”
note: for dot syntax, if the attribute doesn’t exist it will be created as the value is being assigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Given an object named Car, use bracket notation([ ]) to give it a property called make and assign it a value of Audi

A

Car[“make”] = “Audi”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given the list of Car properties in an array like so
let properties = [“make”, “model”, “year”]
How would you cycle through the list dynamically to apply the attributes for a Honda Civic 2007 to an empty object?

A

let values = [“Honda”, “Civic”, 2007]
let car = {}
for(let i =0; i < properties.length; i++){
car[properties[i]] = [values[i];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Difference between Objects and Arrays?

A

Both objects and arrays can hold multiple pieces of data.

However, if a piece of data can be referenced by a named property, then an object can be used to make the data structure more descriptive.

Arrays are a lightweight container for holding multiple pieces of data, especially collections of things. Arrays with numeric indexes do not the benefit of being descriptive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Difference between JSON and Javascript Objects?

A

JSON: Properties must be written in double quotes. Example: { “make”: “Honda”}
JSON can have the following types of values: string, number, JSON object, array, boolean, null

Javascript Objects: Properties are not written in double quotes. Can also contain othwe JavaScript values and types such as functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to loop through all properties/keys in an object?

A

Using for in
for (let key in object){
console.log(key +”=”+ object[key]);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to use getters and setters in an object?

A

let person = {
name: “Tenzing”,
get getName(){
return this.name;
}
}
//Using the getter
console.log(person.getName);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How are classes in Javascript different from classes in Java?

A

The “class” keyword, under the hood, uses using prototypal inheritance (out of scope of this course). The “class” keyword is “syntactic sugar” over functions and prototype.

Essentially Javascript mimics the action of a Java class using functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Write down a Javascript Class called Animal, with a constructor that takes name and age, and has a speak method where it says “ahh”

Then create a child class called Dog that also has an attribute called LicksPerSecond and when it speaks it says “ruff”

A

class Animal{
constructor(name, age){
this.name = name
this.age = age
}
speak(){
console.log(“ahh”)
}
}

class Dog extends Animal{
constructor(name, age, LicksPerSecond){
super(name, age);
this.LicksPerSecond = LicksPerSecond;
}
speak(){
console.log(“ruff”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Create a class called Circle that has a radius. This class also has a getter for area(math.PI*radius**2), and a setter for radius.

After you create a class instantiate a circle with radius 3, get the area, then set the radius to 5

A

class Circle{
constructor(radius){
this._radius = radius
}
get area(){
return Math.PI * this._radius ** 2
}
set radius(newRadius){
this._radius = newRadius
}
}

Circle c1 = new Circle(3);
console.log(c1.area);
c1.radius = 5;

23
Q

Is the syntax below correct for a setter?

set name(newName){
this.name = newName;
}

A

No when using setters in a class, use different names for assigning this.property = “value” and the setter property name

set name(newName){
this._name = newName;
}

24
Q

What does Object.keys() do? and provide an example

A

Lets you get all the keys/property names of an object and put it in an array
let person = {
name: “Tenzing”,
school: “Sheridan”,
program: “ICT”
}
let keys = Object.keys(person);
console.log(keys)
// Keys will be [“name”, “school”, “program”]

25
Q

What does Object.value() do?

A

accepts an object and returns an array of it’s values

let values = Object.value(person);
//values will be [“Tenzing”, “Sheridan”, “ICT”]

26
Q

What does Object.entries() do?

A

let entries = Object.entries(person)
//values will be [ [“name”,”Tenzing], [“school”, “Sheridan”], [“program”,”ICT”] ]
accepts an object and returns an array with arrays that hold 2 values(key and value of the object)

27
Q

Given an object called books, how would you iterate over all the key/values/entries?

A

for (let value of Object.values(book)) {
if (Array.isArray(value)) {
console.log(“genres:”)
for (let v of Object.entries(value)) {
console.log(“- “ + v[1]);
}
} else
console.log(value);
}

28
Q

Objects can be made up of _____ properties like _____, ______, ________ etc. as well as other objects and arrays. Arrays can be made up of objects or other arrays and _____ values.

A

Objects can be made up of scalar properties like strings, integers, booleans etc. as well as other objects and arrays. Arrays can be made up of objects or other arrays and scalar values.

Note, objects and arrays have the same makeup, but arrays hold values while objects hold properties/attributes of something.

Example:
let car1 = {make:”honda”} //Make is an attribute here
let cars = {car1, car2, car3} //Cars array acts like a container

29
Q

What is Node JS and how is it different from traditional web application?

A

Node.js is a runtime environment that allows developers to run JavaScript code on the server-side.

Unlike traditional web development where JavaScript is primarily used in the browser to enhance the user interface, node.js lets you use JavaScript to create server applications. It can perform common server-side actions such as processing requests and connecting to databases etc. In this course, we will use node.js to do just that. Keep in mind, that node.js can be run as a command line application without the need for a browser. (Other server-side languages can do that as well).

30
Q

Pros of using Node JS?

A

Pros:
-One language to rule them all. node.js allows developers to use the same language (JavaScript) for both client-side and server-side development. This can lead to increased developer productivity and code reusability.

-Lots of libraries. node.js has a rich ecosystem of open-source packages and libraries available through npm (Node Package Manager). Developers can easily find and integrate third-party modules, which accelerates development and reduces the need to reinvent the wheel.

-Non-blocking. node.js is built on an event-driven, non-blocking I/O (Input/Output) model. This means that it can handle a large number of simultaneous connections without getting bogged down, making it very efficient for applications that require high concurrency, such as real-time applications.

31
Q

Cons of using Node js?

A

Cons:
-Callbacks. Node.js relies heavily on callbacks, which can lead to complex and nested code structures, known as “callback hell”. This can make code harder to read and maintain.

-Not the best for heavy computational tasks. While Node.js is excellent for building real-time web applications and microservices, it may not be the best choice for all use cases. For example, if you need to perform heavy computation or work with complex algorithms, other languages like Python or Java may be more suitable.

-Learning Curve. If you’re new to asynchronous programming and event-driven development, Node.js can have a steep learning curve. Developers need to understand callbacks, promises, and async/await.

32
Q

Name the following cli commands we need to know?

A

pwd - path of current directory
cd .. - change to parent directory
ctrl+c - exit cli
cd - change to directory
ls - list content in current directory
clear - clear cli

33
Q

How to do the following on Node commands?

a. Check node version?
b. Check node package manager version?
c. How to initialize node package manager?
d. How to install a dependency with node?
e. How to run the server and the web application?

A

a. node -v

b. npm -v

c. npm init

d. npm install dependancyhere

e. node app.js(name of the node js file)

34
Q

What is the purpose of packages in Node JS?

A

While Node.js comes bundled with a set of useful libaries. Developers may want to use libraries that are not included in core node.js. Node.js allows us to add additional libraries to our app without having to code functionality from scratch

35
Q

Difference between PHP and Node.JS and why did we use PHP before? and why do we use Node.JS now?

A

one of the reasons we started with PHP was because receiving requests and processing responses without a library or framework was relatively easy for new learners.
With node.js, it’s a bit more complicated. But with the express package things like starting a server and managing requests and responses are alot simpler.

36
Q

Do the following code:

a. Install the express package

b. In the server code, call the express package to be used

c. Use Express on port 3000

A

a. npm install express

b. const express = require(“express”);

c. const app = express();
app.listen(3000, => {
console.log(“listening on port 3000”);
)

37
Q

What is routing?

A

Routing associates an incoming url (request for a resource) with a function.

38
Q

When using templating languages like hbs, where will the files be searched for by default?

A

In the views folder

39
Q

Write the following code, assume both route to /this-route
and
const app = express();
a. A simple GET route

b. A simple POST route

A

a. app.get(“/this-route”, (req, res) => {
});

b. app.post(“/this-route”, (req, res) => {
});

40
Q

With node routing, what are the two ways to send text or an HTML page to the client?

A

res.send(“html or text here”);

res.render(“index.hbs”);
or
res.render(“test.hbs”, {var1: val1, var2:val2});

41
Q

How to serve static files like .css or .js files with node?
Assuming the below:
const express = require(“express”)
const app = express();

A

app.use(express.static(“public”));

note: Make sure you have a public folder

42
Q

What are the 3 types of variables we can use to get information from the user?

A
  1. URL route parameters:
    Ex: Assume app.get(“/home/:routeVariable, (req,res) =>)
    req.params.routeVariable
  2. URL query parameters:
    Ex.Assume user submitted a form
    app.get(“/server-script”)
    req.query.(inputTagNameHere)
  3. POST variables(HTTP Headers)
    Ex Assume app.post(“/server-script”)
    req.body.(inputTagNameHere)
43
Q

When using app.post what must you first declare in your code?

A

app.use(
express.urlencoded({
extended: false,
})
);

44
Q

Using GET how are variables embedded in URLs?

A

When using form submission with GET:
http://yourwebsite.com/your-server-script?name1=value1&name2=value2&name3=value3
formatted like /server-script?key1=value1&key2=value2

45
Q

What is URL encoded String?

A

http://yourwebsite.com/your-server-script?name1=value1&name2=value2&name3=value3

The use of ? before key=value pairs and ampersand(&) between each pair. Like so:
/server-script?key1=value1&key2=value2

46
Q

What does google’s search URL look when searching the word interactive?

A

https://www.google.ca/search?q=interactive

/search - server script
? - start of variables
q=interactive - key-value pair(or variable definition)

47
Q

Upsides and Downsides of using GET over POST?

A

GET is good for sharing and bookmarking since the variables required to generate the page is in the URL.

The GET method produces a long string that appears in your server logs, in the browser’s Location: box and is recognized by search engines

The GET method is restricted to send a maximum number of characters, depending on the browser. Because of this GET cannot be used to send binary data.

48
Q

Upsides and Downsides of using POST over GET?

A

The POST method transfers information via HTTP headers.

The POST method does not have any restriction on data size to be sent.

Data sent by POST can be made secure since it can use Secure HTTP.

49
Q

What are the three things to do when connecting to a Database?

A

Connect, Prepare, Execute

50
Q

6 parts of a form?

A

form, action, method, input, name, submit

ex. < form action=”/server-script” method=”GET” >
FirstName< input name=”fname”>
< input type=”submit” >

51
Q

4 things required to configure a connection to a Database?

A

const mysql = require(“mysql2”);

const connection = mysql.createConnection({
host:
username:
password:
database:
});

Host, Username, Password, Database;

52
Q

Provide the code that passes an object called game that has a title of Tekken and rating of 5, into the index.hbs file and provide the code snippet between < ul > < /ul >, that uses {{#each data}}

A

NODEJS CODE:
const game = {
name:”Tekken”,
rating:5
}
res.render(“index.hbs”, {game:game});

HTML CODE:

< ul >
{{#each data}}
< li > {{item}} < /li >
{{/each}}
< /ul >

53
Q

Final Total: 50 points
13 points hard coding (One of these is HTML? forms?. 2nd one is to write a list of steps for something?database?)
Matching/Multiselect

A