JS Flashcards

1
Q

save to local storage

A

localStorage.setItem(“key”, value)

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

convert from js object to JSON string

A

JSON.stringify(myObject)

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

get from local storage

A

localStorage.getItem(itemKey)

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

Flex

A

Container
display:flex
flex-direction: row/column row-reverse
justify-content: space-between
align-items: center, flex-start, flex-end, stretch

Items
flex-shrink
flex-grow
flex-basis

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

CSS descendant selectors

A

space, for example: ul.my-things li , List items that are descendants of the “my-things” list

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

General Sibling combinator selector

A

Tilde ~ selects all elements that are next siblings of a specified element
li.red ~ li , selects all the li’s that come after an li with a class of red

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

Select all elements that match various combinators (and)

A

just type them together without spaces

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

apply same css to different selectors (or)

A

separate them using commas

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

direct child selector

A

< , just the direct children

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

Css Adjacent Sibling Combinator

A

Selects only one sibling that comes after a selector
li.red + li, selects only the one li that comes after the li with the class of red

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

css pseudoclasses

A

hover, li:hover
:focus (buttons & form inputs when clicked)
:checked
:disabled
li:first-child

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

pseudo class to select the first child, last child, or n child, only child, first of type, last of type

A

li.red:first-child (only selects the first li if it has the class of red)
li:last-child
li:nth-child()
li:only-child (selects the li only if its the only child inside a block)
li:first-of-type (the first item of type li inside a block)
li:nth-of-type()
li:last-of-type

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

pseudo selector not

A

li:not(.green), pseudo selector that doesn’t have the class of green

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

pseudo elements

A

div.red::before, div.red::after Adds styling before or after de div with a class of red

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

Styling data attribute selectors. simple and with a particular value

A

[data-red]{ }

[data-red=”true”]{ }

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

attribute selector that matches the beginning

A

[data-red^=”12”]

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

attribute selector that matches the end

A

[data-red]$=”69”

<div></div>

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

Red data attribute selector that has 34 somewhere

A

<div></div>

[data-red]*=”34”

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

Adds a text to an element

A

element.innerText =”my new text” (visible content in a node)
or element.textContent= “My new text” (all content)

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

clone a template

A

const template = document.querySelector(“#myTemplate”)
const templateClone = template.content.cloneNode(true)

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

add a newElement to a listElement

A

listElement.appendChild(newElement)

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

check edge case inside a function where the input text is empty

A

if(inputText === ‘ ‘) return

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

add to an array

A

myArray.push(myText)

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

save to local storage, and what if saving an array value

A

localStorage.setItem(“keyName”, stringValue)

if the stringValue is an array
localStorage.setItem(“keyName”, JSON.stringify(stringValue))

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

load from local storage

A

function loadItems(){
const storedItems = localStorage.getItems(KEY_NAME)
return JSON.parse(storedItems) || [] // converts to js object or array

}

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

event listener when we interact with a list

A

list.addEventListener(“change”,e=>{
e.target.matches(“[data-checkbox-name]”)
})

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

create a unique value by milisecond

A

const uniqueValue = new Date().valueOf().toString()

28
Q

add a data attribute ‘todoId’ in js to an element

A

element.dataSet.todoId = “123”

29
Q

get the parent of a clicked item

A

e.target.closest(‘.parentName’)

30
Q

remove an element from the screen

A

element.remove()

31
Q

get all the buttons dynamically

A

document.addEventListener(‘click’, e => {
if(!e.target.matches(‘.buttonClass’)) return
})

32
Q

import node js modules

A

const myExportedThings = require(./user.js)
console.log(myExportedThings.name)

—- file to export:
module.exports{
name: name,
age:age
}

33
Q

hide from the UI, mantain the element space

A

visibility:hidden/visible

34
Q

hide from the UI, take no space

A

display:none/block

35
Q

toggle a class called show in js

A

element.classList.toggle(‘show’)

36
Q

fire once a click event. (should be removed after clicking)

A

button.addEventListener(‘click’,()=>{
//dostuff
},{once:true})

37
Q

create a button with js and give it the class of red, and attach it to a grid element

A

const myButton = document.createElement(‘button’)
myButton.classList.add(“red”)
myGrid.appendChild(myButton)

38
Q

Select all dataset with foo

A

Document.querySelectorAll(“[data-foo]”)

39
Q

fetch request

A

fetch(“URL”)
.then(
response=>{return response.json() }
.then(
data=>{console.log(data)}
)

40
Q

classList functions

A

div.classList.add(“foo”)
div.classList.remove(“foo”)
div.classList.toggle(“foo”)
div.classList.replace(“foo”, “bar”)

41
Q

convert string to integer

A

parseInt(“3”)

42
Q

switch

A

switch(x){
case “oranges”:
break;
case “apples”:
break;
default;
}

43
Q

make a copy of an object

A

animal2= JSON.parse(JSON.stringify(animal))

44
Q

agregar y quitar al final de un arreglo

A

myArray.push(‘red’)
myArray.pop()

45
Q

quitar y poner en el frente de un arreglo

A

myArray.shift()
myArray.unshift(‘red’)

46
Q

myArray.splice(2,1)

A

el index en el que queremos quitar, y el numero de elementos. Queremos quitar un elemento en el índice 2. Se modifica el arreglo

47
Q

Var disadvantages

A

Breaks scope, has hoisting, allowed redeclaration

48
Q

check if a variable is NaN

A

isNaN(variable)

49
Q

constructor function

A

function User(name,age){
this.name = name
this.age = age
}

const grace = new User(¨grace¨, 39)

50
Q

class

A

class User(name,age) {
constructor(){
this.name = name
this.age = age
}
function sayName(){
console.log(this.name)
}
}

const user1 = newUser(“grace”,28)
user1.sayName()

51
Q

code a select element

A

Red
Green

52
Q

html types <input type=””

A

text, checkbox, radio, file, tel, url, hidden, color

53
Q

Match a label with an input

A

The for with the id
Color

54
Q

whats a private variable

A

a variable only available in its own class starts with #, example #internalOnlyVariable and its not available to the children

55
Q

whats a protected variable

A

a variable that cant be accessed outside of its class, but can be accessed by the children

56
Q

event que se activa cuando escribes letra por letra en un input

A

input

57
Q

event que se activa cuando escribes algo en un input y das clic afuera

A

change

58
Q

Diferencia entre filter y find al filtrar un arreglo de objetos

A

Filter regresa un arreglo [{}], find regresa el objeto {}

59
Q

Sets the active html element in the document. It can only be applied to one element

A

Document.querySelector(‘x’).focus()

60
Q

promises
explain Promise.all
new Promise.all([
Promise.resolve(‘1’),
Promise.reject(‘error on 2’),
Promise.resolve(‘3’),
]).then(messages=> console.log(messages)).catch(error=> console.error(error))

A

//logs the first to fail or an array with all the resolves [‘1’,’2’,’3’]

61
Q

explain Promise.any

A

prints the first that succeeds. (only succeed not fail)

62
Q

explain Promise.race

A

prints the first that succeeds or fails

63
Q

explain Promise.allSettled

A

waits for all to finish and then gives the status of each[{status: rejected, reason: ‘error on 1’}]

64
Q

code a fetch using async await

A

async function fetchData(){
const response = await fetch(‘http’)
const data = await response.json()
console.log(data.map(item => data.name))
}

65
Q

post

A

fetch(‘http:///’,{
headers: {“content-type”:”application/json”},
body: JSON.stringify({ title: ‘new post’}),
method: POST,
})

66
Q

what are the 2 types of fetch requests

A

async or promise based

67
Q
A