The One Flashcards
(157 cards)
What are 2 ways to check if there’s at least 1 ‘item’ of a certain thing exist in an array;
myArray.includes(item)
myArray.some(thing => thing === item)
How to get what key is pressed on an keyboard event?
evt.key;
How to create, in one line, a 2D array with x rows and y cols, using Array
const new2DArray = Array.from({ length: x }, () => new Array(y));
How to toggle the ‘foo’ class on a given element?
element.classList.toggle("foo");
How to select the sibling immediately before or immediately after an element using javascript?
const preElement = element.previousElementSibling;
const nextElement = element.nextElementSibling;
How to assign bar into foo if bar is truthy, otherwise assign baz?
const foo = bar || baz
How to assign bar into foo if bar is not nullish (null or undefined), otherwise assign baz?
const foo = bar ?? baz
How to assign bar into foo if baz is truthy, otherwise assign baz?
const foo = baz && bar
How to insert an element right before, or right after an element? Without utilizing the parent element
node.insertAdjacentElement("beforebegin", nodeToInsert);
node.insertAdjacentElement("afterend", nodeToInsert);
What does the ‘every()’ method of Array take in as a parameter?
It takes in a function
What is one way to check if all the elements in an array are the same?
myArray.every((val) => val === myArray[0]);
What’s the difference between the ‘findIndex’ method and the ‘indexOf’ method of an array?
‘IndexOf’ takes in an element as parameter
‘findIndex’ takes in a function as parameter.
Both methods returns the index of the first element;
What data structure do you need to use for breathFirstSearch
A queue
How to write an async function that fetches some data?
const response = await fetch("<http://example.com>");
const data = await response.json();
What are the 2 ways to iterate over a Set? given: const mySet = new Set([1,1,2,2,3]);
for(const item of mySet){}
mySet.forEach(item => {})
How to clone a Set?
const newSet = new Set([...oldSet]);
How to get the list of children for a given node?
const children = element.children
How to get the bounding box of an element?
node.getBoundingClientRect();
How to create, in one line, a 1D array with n items filled with ‘Infinity’?
const newArray = Array(n).fill(Infinity);
How to get the X and Y mouse cursor position when an event happens?
event.clientX
event.clientY
Given display: grid
, what one CSS property results in a grid with 3 equal columns?
grid-template-columns: 1fr 1fr 1fr
What are the two ways to turn a Set into an array? given: const mySet = new Set([1,1,2,2,3]);
Array.from(mySet);
[...mySet]
How to select the sibling of an element using javascript where the sibling has the id ‘foo’?
Array.from(element.parentNode.children).find(el => el.id === 'foo'
How to remove all child of an element?
container.innerHTML = "";