Skip to content

Instantly share code, notes, and snippets.

@3lement3
3lement3 / app.js
Last active February 29, 2020 21:30
GetPost AJAX Response
const btn = document.querySelector('.btn-get-post');
const btnAddPost = document.querySelector('.btn-add-post');
const container = document.querySelector('.container');
function getPost(cb) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();
xhr.addEventListener('load', () => {
const responseJson = JSON.parse(xhr.responseText);
cb(responseJson);
@3lement3
3lement3 / DocumentFragment.js
Created February 20, 2020 08:45
DocumentFragment
/**
* @type {DocumentFragment}
*/
const fragment = document.createDocumentFragment();
const colors = ['tomato', 'orange', 'red', 'yellow'];
const text = ['this is tomato', 'this is orange', 'this is red', 'this is yellow'];
const styles = ['tomato', 'orange','red','yellow'];
colors.forEach((value, index) => {
const item = document.createElement('div');
@3lement3
3lement3 / app.js
Last active February 19, 2020 08:30
First char in string to upper case
let str = 'i am in the easycode';
str.split(/\s+/).map((char) => char[0].toUpperCase() + char.substring(1)).join(' ');
@3lement3
3lement3 / app.js
Created January 31, 2020 12:15
Function Array Sum Square
/**
* Function array sum square
* @param numbers
* @returns {number}
*/
function squareSum(numbers) {
let arrSum = numbers.map(value => value ** 2);
let sum = 0;
for (let i = 0; i < arrSum.length; i++) {
sum += arrSum[i];
@3lement3
3lement3 / app.js
Last active January 31, 2020 11:04
First Letter To Upper Case
/**
* Creat prototype for object STRING where first letter to upper case
*
* @returns {string}
*/
String.prototype.toJadenCase = function () {
return this.replace(/( |^)[а-яёa-z]/gi, function (string) {
return string.toUpperCase();
});
};
@3lement3
3lement3 / app.js
Last active January 31, 2020 09:49
Difference Array
/**
*
* @param a = array
* @param b = array
* @returns {*[]} difference between a and b
*/
function arr_diff(a, b) {
let aa = [];
let bb = [];
for (let i = 0; i < a.length; i++) {
@3lement3
3lement3 / app.js
Created January 29, 2020 07:15
XMLHttpRequest EXAMPLE
const btn = document.querySelector('.btn');
btn.addEventListener('click', function () {
var req = new XMLHttpRequest();
var API_KEY = 'API_KEY';
// Сохраняем адрес API
var url = 'https://translate.yandex.net/api/v1.5/tr.json/translate';
// Формируем полный адрес запроса:
url += '?key=' + API_KEY; // добавляем к запросу ключ API
url += '&text='; // текст для перевода
@3lement3
3lement3 / app.js
Created January 28, 2020 17:39
addEventListener
const link = document.querySelector('.link');
link.addEventListener('click', function (ev) {
ev.preventDefault();
console.log(ev.type);
});
const textarea = document.querySelector('textarea');
const key = document.querySelector('.key');
textarea.addEventListener('keydown', function (ev) {
if (ev.key === 'Enter') {
ev.preventDefault();
@3lement3
3lement3 / XMLHttpRequest.js
Last active January 28, 2020 16:49
XMLHttpRequest
/*
*XMLHttpRequest
*/
const request = new XMLHttpRequest();
request.open('Get', '/data.txt');
request.send();
request.onload = function () {
console.log(request.responseText);
};
/*
@3lement3
3lement3 / constructor.js
Last active January 31, 2020 11:22
Creat constructor
/**
* The Constructor Of The Parent
* @param firstName
* @param lastName
* @constructor
*/
function User(firstName, lastName) {
//this = {}
this.firstName = firstName;
this.lastName = lastName;