Skip to content

Instantly share code, notes, and snippets.

@nicolaair
nicolaair / decodeResponse.js
Created January 25, 2024 12:15 — forked from arimariojesus/decodeResponse.js
Decode a xml fetch response with ISO-8859-1 codification to UTF-8
const decodedResponse = await fetch('https://www.anysite.rss.xml', {
headers: {
'Content-Type': 'text/xml;charset=ISO-8859-1',
},
})
.then(r => r.arrayBuffer()) // resolve the response stream for buffer
.then(d => {
const dataView = new DataView(d); // creates a DataView object representing the buffer data
const isoDecoder = new TextDecoder('ISO-8859-1'); // creates an instance of the our decoder
const decodedString = isoDecoder.decode(dataView); // so we pass the stream of bytes for the decoder do its job
/**
* flatten.
*
* Дан массив, в котором могут храниться любые типы данных.
* Нужно реализовать функцию, которая разворачивает вложенные массивы в исходный массив.
* Данные остальных типов должны остаться без изменений.
* Решение должно учитывать любую вложенность элементов (т.е. не должно содержать рекурсивные вызовы).
* Встроенный метод Array.flat() использовать нельзя
*/
@nicolaair
nicolaair / SuperArray.js
Last active December 21, 2020 15:23
Школа Программистов Head Hunter. Второе домашнее задание по JS.
const utils = {
findMiddleIndex: (list) => Math.floor(list.length / 2)
};
class NotFoundException {
constructor(message) {
this.name = "Не найдено";
this.message = message;
}
}
@nicolaair
nicolaair / MyIterator.js
Last active December 16, 2020 23:20
Школа Программистов Head Hunter. Домашнее задание по JS.
class MyIterator {
// Конструктор в качестве аргументов принимает символы английского языка и длину перестановки
constructor(str, len) {
this.str = str;
this.len = len;
this.acc = [];
this.prepare();
}
@nicolaair
nicolaair / data-for-compare-lang.txt
Last active October 5, 2020 00:41
Data for Compare Lang
0. This is data for String from Remote!
@nicolaair
nicolaair / threeSum.js
Created July 24, 2020 23:41
JavaScript threeSum
function threeSum(nums, target = 0) {
if (nums.length < 3) return [];
const result = {}
nums.sort((a, b) => a - b).every((a, i) => {
let j = i + 1
let k = nums.length - 1
while (j < k) {
@nicolaair
nicolaair / fibonacci.js
Last active July 24, 2020 16:52
JavaScript Fibonacci
fibonacci(9) // 34
// fibonacci(10) // '0, 1, 1, 2, 3, 5, 8, 13, 21, 34'
function fibonacci(num) {
const result = [0, 1]
for (let i = 2; i <= num; i++) {
result.push(result[i - 2] + result[i - 1])
}
@nicolaair
nicolaair / countVowels.js
Created July 24, 2020 15:01
Count vowels
countVowels('Hello') // 2
function countVowels(str) {
return str.replace(/[^eyuioa]/g, '').length
}
@nicolaair
nicolaair / anagram.js
Last active July 24, 2020 17:23
JavaScript function anagram
anagram('finder2', 'Fr ie2n.d ,') // true
const anagram = (a, b) => [...a.toLowerCase()].sort().join('') === [...b.toLowerCase()].sort().join('')
// function anagram(firstStr, secondStr) {
// const strToArr = str => [...str.replace(/[^\A-Za-zА-Яа-я0-9]/g, '').toLowerCase()]
// const sortNat = str => str.sort((a, b) => new Intl.Collator(['en', 'ru'], { numeric: true }).compare(a, b))
// const sortedFirstStr = sortNat(strToArr(firstStr)).join('')
// const sortedSecondStr = sortNat(strToArr(secondStr)).join('')
@nicolaair
nicolaair / fizzBuzz.js
Created July 24, 2020 12:03
JavaScript FizzBuzz function
function fizzBuzz(num) {
return Array.from(Array(num)).map((_, key) => {
const num = key + 1
const fizzTest = num % 3 === 0
const buzzTest = num % 5 === 0
return fizzTest && buzzTest
? 'fizzbuzz'
: buzzTest
? 'buzz'