Best explaination https://javascript.info/async-await
In real situations, the promise may take some time before it rejects. In that case there will be a delay before await throws an error.
We can catch that error using try..catch, the same way as a regular throw:
async function f() {
try {
let response = await fetch('/no-user-here');
let user = await response.json();
} catch(err) {
// catches errors both in fetch and response.json
console.log(err);
}
}
f();
Use innerHTML for creating new elements. Use insertAdjascentHTML for more secure. Use textContent for inside text.
Stores in brower until hard reload/localstorage.clear(). Can be used to store data that will persist through a soft refresh. Syntax is similiar to objects with dot notation. The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.
It should be noted that data stored in either localStorage or sessionStorage is specific to the protocol of the page.
The keys and the values are always strings (note that, as with objects, integer keys will be automatically converted to strings).
The forEach() method executes a provided function once for each array element.
var array1 = ['a', 'b', 'c'];
array1.forEach(function(element) {
console.log(element);
});
The map() method creates a new array with the results of calling a provided function on every element in the calling array. Higher-Order function.
var array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
The map function creates a new array by calling a specific function on each element in an initial array. For example, if you have an array of strings in the form "MM-DD" that represent birthdays and you want to convert each element to be in a different format, you could use the map function to create a new array with new elements.
var bdays = ['08-14', '10-04', '04-21'];
// we want a new array where the birthdays will be in the format: MM/DD
// the elem parameter will be each element from the original array
var bdays2 = bdays.map(function(elem) {
return elem.replace('-', '/');
});
console.log(bdays2); // => ['08/14', '10/04', '04/21']
Another simple example using the map function to round an array of numbers up in JavaScript:
var arr = [1.5, 2.56, 5.1, 12.33];
// round each number up in an array
var rounded = arr.map(Math.ceil);
console.log(rounded); // => [2, 3, 6, 13]
The filter() method creates a new array with all elements that pass the test implemented by the provided function. Higher-Order function.
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
The filter function creates a new array with all elements from an original array that pass a certain functions test. For example, you can use the filter function to create a new array of only positive values, like below. The function being called takes in an argument which is the value of the current element in the array.
var nums = [-4, 3, 2, -21, 1];
var pos = nums.filter(function(el) {
return el > 0;
});
console.log(pos); // => [3, 2, 1]
You can also, for example, filter out all objects in a data file that have incorrect or undefined values. In the example below, we filter out all elements that have an incorrect age value.
var data = [
{name: 'daniel', age: 45},
{name: 'john', age: 34},
{name: 'robert', age: null},
{name: 'jen', age: undefined},
{name: null, age: undefined}
];
// dataMod will now contain only the first two objects in the data array
var dataMod = data.filter(function(el) {
if (el.name != undefined && el.age != undefined) {
return true;
}
else {
return false;
}
});
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. Higher-Order function.
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
The reduce function applies a specific function to all the elements in an array and reduces it to a single value. The reduce function has actually been used in several of the challenge solutions, one example being Mean Mode. We can use the reduce function to add up all the numbers in an array for example. The four arguments the reduce function takes are:
- previous value
- current value
- current index
- original array
var nums = [1, 2, 3, 4];
var sum = nums.reduce(function(prevVal, curVal, curIndex, origArr) {
return prevVal + curVal;
});
console.log(sum); // => 10
The sort() method sorts an array alphabetically:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
The reverse() method reverses the elements in an array.
You can use it to sort an array in descending order:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // First sort the elements of fruits
fruits.reverse(); // Then reverse the order of the elements
By default, the sort() function sorts values as strings.
This works well for strings ("Apple" comes before "Banana").
However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect result when sorting numbers.
You can fix this by providing a compare function:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
//Use the same trick to sort an array descending:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following 8.2. eslint: array-callback-return
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
// good
[1, 2, 3].map((x) => x + 1);
// bad - no returned value means `acc` becomes undefined after the first iteration
[[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
const flatten = acc.concat(item);
});
// good
[[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
const flatten = acc.concat(item);
return flatten;
});
// bad
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird') {
return author === 'Harper Lee';
} else {
return false;
}
});
// good
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird') {
return author === 'Harper Lee';
}
return false;
});
Use array spreads ... to copy arrays.
// bad
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i += 1) {
itemsCopy[i] = items[i];
}
// good
const itemsCopy = [...items];
To convert an iterable object to an array, use spreads ... instead of Array.from.
const foo = document.querySelectorAll('.foo');
// good
const nodes = Array.from(foo);
// best
const nodes = [...foo];
Use object destructuring when accessing and using multiple properties of an object. eslint: prefer-destructuring
Why? Destructuring saves you from creating temporary references for those properties.
// bad
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
return `${firstName} ${lastName}`;
}
// good
function getFullName(user) {
const { firstName, lastName } = user;
return `${firstName} ${lastName}`;
}
// best
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
Use array destructuring. eslint: prefer-destructuring
const arr = [1, 2, 3, 4];
// bad
const first = arr[0];
const second = arr[1];
// good
const [first, second] = arr;
Use object destructuring for multiple return values, not array destructuring.
Why? You can add new properties over time or change the order of things without breaking call sites.
// bad
function processInput(input) {
// then a miracle occurs
return [left, right, top, bottom];
}
// the caller needs to think about the order of return data
const [left, __, top] = processInput(input);
// good
function processInput(input) {
// then a miracle occurs
return { left, right, top, bottom };
}
// the caller selects only the data they need
const { left, top } = processInput(input);
When you must use an anonymous function (as when passing an inline callback), use arrow function notation. eslint: prefer-arrow-callback, arrow-spacing
Why? It creates a version of the function that executes in the context of this, which is usually what you want, and is a more concise syntax.
Why not? If you have a fairly complicated function, you might move that logic out into its own named function expression.
// bad
[1, 2, 3].map(function (x) {
const y = x + 1;
return x * y;
});
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
If the function body consists of a single statement returning an expression without side effects, omit the braces and use the implicit return. Otherwise, keep the braces and use a return statement. eslint: arrow-parens, arrow-body-style
Why? Syntactic sugar. It reads well when multiple functions are chained together.
// bad
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
`A string containing the ${nextNumber}.`;
});
// good
[1, 2, 3].map((number) => `A string containing the ${number + 1}.`);
// good
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
return `A string containing the ${nextNumber}.`;
});
// good
[1, 2, 3].map((number, index) => ({
[index]: number,
}));
// No implicit return with side effects
function foo(callback) {
const val = callback();
if (val === true) {
// Do something if callback returns true
}
}
let bool = false;
// bad
foo(() => bool = true);
// good
foo(() => {
bool = true;
});
In case the expression spans over multiple lines, wrap it in parentheses for better readability.
Why? It shows clearly where the function starts and ends.
// bad
['get', 'post', 'put'].map((httpMethod) => Object.prototype.hasOwnProperty.call(
httpMagicObjectWithAVeryLongName,
httpMethod,
)
);
// good
['get', 'post', 'put'].map((httpMethod) => (
Object.prototype.hasOwnProperty.call(
httpMagicObjectWithAVeryLongName,
httpMethod,
)
));
Always include parentheses around arguments for clarity and consistency. eslint: arrow-parens
Why? Minimizes diff churn when adding or removing arguments.
// bad
[1, 2, 3].map(x => x * x);
// good
[1, 2, 3].map((x) => x * x);
// bad
[1, 2, 3].map(number => (
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
));
// good
[1, 2, 3].map((number) => (
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
));
// bad
[1, 2, 3].map(x => {
const y = x + 1;
return x * y;
});
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
Avoid confusing arrow function syntax (=>) with comparison operators (<=, >=). eslint: no-confusing-arrow
// bad
const itemHeight = (item) => item.height <= 256 ? item.largeSize : item.smallSize;
// bad
const itemHeight = (item) => item.height >= 256 ? item.largeSize : item.smallSize;
// good
const itemHeight = (item) => (item.height <= 256 ? item.largeSize : item.smallSize);
// good
const itemHeight = (item) => {
const { height, largeSize, smallSize } = item;
return height <= 256 ? largeSize : smallSize;
};
Enforce the location of arrow function bodies with implicit returns. eslint: implicit-arrow-linebreak
// bad
(foo) =>
bar;
(foo) =>
(bar);
// good
(foo) => bar;
(foo) => (bar);
(foo) => (
bar
)
Return the number that only appears once in the array
function findUniq(arr) {
arr.sort((a,b)=>a-b);
return arr[0]==arr[1]?arr.pop():arr[0];
}
findUniq([ 3,3,3,10,3,3,3 ]);
The try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
The try statement consists of a try block, which contains one or more statements. {} must always be used, even for single statements. At least one catch clause, or a finally clause, must be present. This gives us three forms for the try statement:
try...catch
try...finally
try...catch...finally
A catch clause contains statements that specify what to do if an exception is thrown in the try block. That is, you want the try block to succeed, and if it does not succeed, you want to pass control to the catch block. If any statement within the try block (or in a function called from within the try block) throws an exception, control is immediately shifted to the catch clause. If no exception is thrown in the try block, the catch clause is skipped.
The finally clause executes after the try block and catch clause(s) execute but before the statements following the try statement. It always executes, regardless of whether an exception was thrown or caught.
You can nest one or more try statements. If an inner try statement does not have a catch clause, the enclosing try statement's catch clause is entered.
try {
try {
throw new Error('oops');
}
catch (ex) {
console.error('inner', ex.message);
}
finally {
console.log('finally');
}
}
catch (ex) {
console.error('outer', ex.message);
}
// Output:
// "inner" "oops"
// "finally"
try {
try {
throw new Error('oops');
}
catch (ex) {
console.error('inner', ex.message);
throw ex;
}
finally {
console.log('finally');
}
}
catch (ex) {
console.error('outer', ex.message);
}
// Output:
// "inner" "oops"
// "finally"
// "outer" "oops"
The below XMLHttpRequests is not very used as of 2019 due to the introduction of Fetch, which I will describe underneath it.
Use XMLHttpRequest (XHR) objects to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.
Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML.
If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the EventSource interface. For full-duplex communication, WebSockets may be a better choice.
XMLHttpRequest() The constructor initializes an XMLHttpRequest. It must be called before any other method calls. Properties This interface also inherits properties of XMLHttpRequestEventTarget and of EventTarget.
XMLHttpRequest.onreadystatechange An EventHandler that is called whenever the readyState attribute changes.
XMLHttpRequest.readyState Read only Returns an unsigned short, the state of the request.
Fetch
Basic syntax-using promises
fetch(url)
.then(function(res) {
console.log(res);
})
.catch(function(error) {
console.log(error)
});
const url = "https;//api.coindesk.com/v1/bpi/currentprice.json";
fetch(url)
.then(function(data){
console.log(data);
console.log("status code was: " + data.status)
});
Parsing JSON with Fetch
fetch(url).then(function(res) {
return res.json();
});.then(function(data){
console.log(data);
}).catch(function(){
console.log('problem!)
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.
The fetch specification differs from jQuery.ajax() in two main ways:
The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.
By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
Have a look at the following code:
const response = await fetch('http://example.com/movies.json');
const myJson = await response.json();
console.log(JSON.stringify(myJson));
Here we are fetching a JSON file across the network and printing it to the console. The simplest use of fetch() takes one argument — the path to the resource you want to fetch — and returns a promise containing the response (a Response object).
This is just an HTTP response, not the actual JSON. To extract the JSON body content from the response, we use the json() method (defined on the Body mixin, which is implemented by both the Request and Response objects.)
Handling Fetch Errors
fetch(url)
.then(function(res){
if(!res.ok) {
throw Error(404);
}
return res;
}).then(function(response) {
console.log('ok');
}).catch(function(error) {
console.log(error);
});
Two Sum Write a function that takes an array of numbers (integers for the tests) and a target number. It should find two different items in the array that, when added together, give the target value. The indices of these items should then be returned in a tuple like so: (index1, index2).
For the purposes of this kata, some tests may have multiple answers; any valid solutions will be accepted.
The input will always be valid (numbers will be an array of length 2 or greater, and all of the items will be numbers; target will always be the sum of two different items from that array).
function twoSum(numbers, target) {
for (var i = 0; i < numbers.length-1; i++) {
for (var j = i+1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) return [i, j];
}
}
}
Sort The Odd You have an array of numbers. Your task is to sort ascending odd numbers but even numbers must be on their places.
Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.
function sortArray(array) {
const odd = array.filter((x) => x % 2).sort((a,b) => a - b);
return array.map((x) => x % 2 ? odd.shift() : x);
}
Less Pretty
function sortArray(array) {
var odds = [];
//loop, if it's odd, push to odds array
for (var i = 0; i < array.length; ++i) {
if (array[i]%2 !== 0) {
odds.push(array[i]);
}
}
//sort odds from smallest to largest
odds.sort(function(a,b){
return a-b;
});
//loop through array, replace any odd values with sorted odd values
for (var j = 0; j < array.length; ++j) {
if (array[j]%2 !== 0) {
array[j] = odds.shift();
}
}
return array;
}
Write a function called rotate which takes an array and a number, and moves each element however many spaces the number is to the right. For the value at the end of the array, rotate should move it back to the beginning.
function rotate(arr,num){
let amount = num % arr.length;
for(let i = 0;i < amount;i++){
arr.unshift(arr.pop())
}
return arr
}
rotate([1,2,3], 1) // [3,1,2]
rotate([1,2,3], 2) // [2,3,1]
rotate([1,2,3], 3) // [1,2,3]
Write a function called makeXOGrid which takes in two parameters, rows and columns, and returns an array of arrays with the number of values in each subarray equal to the columns parameter and the number of subarrays equal to the rows parameter. The values in the sub-arrays should switch between "X" and "O".
function makeXOGrid(rows,amount){
var finalArr = []
var startWithX = true
for(var i=0; i<rows; i++){
var newRow = []
for(var j=0; j<amount; j++){
if(startWithX){
newRow.push("X")
}
else {
newRow.push("O")
}
startWithX = !startWithX
}
finalArr.push(newRow)
}
return finalArr;
}
makeXOGrid(1,4)
/*/
[["X","O","X","O"]]
/*/
makeXOGrid(3,2)
/*/
[["X","O"],["X","O"],["X","O"]]
/*/
makeXOGrid(3,3)
/*/
[["X","O","X"],["O","X","O"],["X","O","X"]]
/*/
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N.
function findOutlier(arr){
let even = [];
let odd = [];
for(let i = 0;i < arr.length;i++){
if(arr[i] % 2 === 0){
even.push(arr[i])
}else {
odd.push(arr[i])
}
}
if(even.length > odd.length){
return Number(odd)
}else {
return Number(even)
}
}
(findOutlier([0, 1, 2]),1) // returns odd
(findOutlier([1, 2, 3]), 2) // returns even
*clean version
function findOutlier(int){
var even = int.filter(a=>a%2==0);
var odd = int.filter(a=>a%2!==0);
return even.length==1? even[0] : odd[0];
}