-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
421 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Person { | ||
constructor(name, age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
sayHello() { | ||
console.log(`Hello, my name is ${this.name}`); | ||
} | ||
|
||
introduce() { | ||
console.log(`I am ${this.age} years old`); | ||
} | ||
|
||
celebrateBirthday() { | ||
this.age++; | ||
console.log(`Happy birthday! Now I am ${this.age} years old`); | ||
} | ||
} | ||
|
||
// Пример использования: | ||
|
||
const person1 = new Person('John', 25); | ||
person1.sayHello(); // Hello, my name is John | ||
person1.introduce(); // I am 25 years old | ||
person1.celebrateBirthday(); // Happy birthday! Now I am 26 years old | ||
|
||
const person2 = new Person('Alice', 30); | ||
person2.sayHello(); // Hello, my name is Alice | ||
person2.introduce(); // I am 30 years old | ||
person2.celebrateBirthday(); // Happy birthday! Now I am 31 years old |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function getSumOfDeletedLetters(word1, word2) { | ||
// второе слово в обратный порядок | ||
const reversedWord2 = word2.split('').reverse().join(''); | ||
let sum = 0; | ||
// по каждой букве первого слова | ||
for (let i = 0; i < word1.length; i++) { | ||
if (word1[i] !== reversedWord2[i]) { | ||
sum++; | ||
} | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
// Пример использования | ||
const word1 = 'code wars'; | ||
const word2 = 'hack er rank'; | ||
const result1 = getSumOfDeletedLetters(word1, word2); | ||
console.log(result1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function countElements(arr) { | ||
let count = 0; | ||
|
||
function countRecursive(subArr) { | ||
for (let i = 0; i < subArr.length; i++) { | ||
if (Array.isArray(subArr[i])) { | ||
countRecursive(subArr[i]); | ||
} else { | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
countRecursive(arr); | ||
|
||
return count; | ||
} | ||
|
||
// Пример использования | ||
const array = [1, 2, [3, 4, [5, 6]], 7, [8, 9]]; | ||
console.log(countElements(array)); // Output: 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function build (pyramidHeight) { | ||
for (let i = 1; i <= pyramidHeight; i++) { | ||
let row = ''; | ||
for(let j = 1; j <= (pyramidHeight - i); j++) { | ||
row += ' '; | ||
} | ||
for(let k = 1; k <= (2 * i - 1); k++) { | ||
row += '*'; | ||
} | ||
console.log(row); | ||
} | ||
} | ||
|
||
console.log(build(5)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function convertToCamelCase(input) { | ||
var words = input.split(/[-_]/); // Разделить строку по тире или нижнему подчеркиванию | ||
for (var i = 1; i < words.length; i++) { | ||
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase(); | ||
} // Соединить слова, пропустив разделители | ||
var camelCase = words.join(""); | ||
return camelCase; | ||
} | ||
|
||
// Пример использования | ||
var input = "hello-world"; | ||
var camelCase = convertToCamelCase(input); | ||
console.log(camelCase); // Выводит "helloWorld" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function transformString(str) { | ||
let lowerCaseStr = str.toLowerCase(); | ||
let result = ""; | ||
for (let i = 0; i < lowerCaseStr.length; i++) { | ||
let currentChar = lowerCaseStr[i]; | ||
let count = lowerCaseStr.split(currentChar).length - 1; | ||
if (count === 1) { | ||
result += "("; | ||
} else { | ||
result += ")"; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// Пример использования: | ||
let inputString = "din"; | ||
let transformedString = transformString(inputString); | ||
console.log(transformedString); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function findMissingLetter(array) { | ||
for (let i = 0; i < array.length - 1; i++) { | ||
if (array[i + 1].charCodeAt(0) - array[i].charCodeAt(0) > 1) { | ||
return String.fromCharCode(array[i].charCodeAt(0) + 1); | ||
} | ||
} | ||
} | ||
|
||
//Пример использования: | ||
console.log(findMissingLetter(['a', 'b', 'c', 'e'])); | ||
console.log(findMissingLetter(['O','Q','R','S'])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function flattenMap(map, parentKey = '') { | ||
let flatMap = {}; | ||
|
||
for (let key in map) { | ||
if (typeof map[key] === 'object') { | ||
let nestedMap = flattenMap(map[key], parentKey + key + '/'); | ||
flatMap = { ...flatMap, ...nestedMap }; | ||
} else { | ||
flatMap[parentKey + key] = map[key]; | ||
} | ||
} | ||
|
||
return flatMap; | ||
} | ||
|
||
|
||
let hierarchicalMap = { | ||
prop1: 'value1', | ||
prop2: { | ||
nestedProp1: 'value2', | ||
nestedProp2: { | ||
deeplyNestedProp: 'value3' | ||
} | ||
} | ||
}; | ||
|
||
let flatMap = flattenMap(hierarchicalMap); | ||
console.log(flatMap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Node { | ||
constructor(value, left=null, right=null) { | ||
this.value = value; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
|
||
class BinaryTree { | ||
constructor(root) { | ||
this.root = root; | ||
} | ||
|
||
maxsum() { | ||
// Рекурсивная функция для обхода дерева и подсчета максимальной суммы | ||
const traverse = (node, currentSum) => { | ||
if (node === null) { | ||
return currentSum; | ||
} | ||
|
||
// Вычисляем сумму для левого и правого поддерева | ||
const leftSum = traverse(node.left, currentSum + node.value); | ||
const rightSum = traverse(node.right, currentSum + node.value); | ||
|
||
// Возвращаем максимальную сумму из левого и правого поддерева | ||
return Math.max(leftSum, rightSum); | ||
}; | ||
|
||
// Начинаем обход с корневого узла и текущей суммой 0 | ||
return traverse(this.root, 0); | ||
} | ||
} | ||
|
||
// Пример использования | ||
const leaf1 = new Node(4); | ||
const leaf2 = new Node(3); | ||
const leaf3 = new Node(5); | ||
const leaf4 = new Node(6); | ||
const node3 = new Node(2, leaf1, leaf2); | ||
const node2 = new Node(1, leaf3, leaf4); | ||
const root = new Node(0, node2, node3); | ||
|
||
const tree = new BinaryTree(root); | ||
console.log(tree.maxsum()); //должна вывести: 11 (0 -> 1 -> 5 -> 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function mergeArrays(numbers, letters) { | ||
const mergedArray = []; | ||
const maxLength = Math.max(numbers.length, letters.length); | ||
for (let i = 0; i < maxLength; i++) { | ||
// Если есть элемент в числовом массиве на данной позиции, добавляем его в объединенный массив | ||
if (i < numbers.length) { | ||
mergedArray.push(numbers[i]); | ||
} | ||
|
||
// Если есть элемент в массиве с буквами на данной позиции, добавляем его в объединенный массив | ||
if (i < letters.length) { | ||
mergedArray.push(letters[i]); | ||
} | ||
} | ||
|
||
return mergedArray; | ||
} | ||
|
||
// Пример использования | ||
const numbers = [1, 2, 3, 4, 5]; | ||
const letters = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
|
||
const mergedArray = mergeArrays(numbers, letters); | ||
console.log(mergedArray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function moveZerosToEnd(arr) { | ||
let zeros = []; | ||
const nonZeros = arr.filter((element) => { | ||
if (element === 0) { | ||
zeros.push(element); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
return nonZeros.concat(zeros); | ||
} | ||
|
||
// Пример использования: | ||
|
||
const inputArray = [false, 0, 1, "hello", 0, 3, "world"]; | ||
const resultArray = moveZerosToEnd(inputArray); | ||
console.log(resultArray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function getPermutationsWithoutDuplicates(string) { | ||
const result = []; | ||
// Проверка на непустую входную строку | ||
if (!string || typeof string !== 'string') { | ||
throw new Error('Invalid input. Please provide a non-empty string.'); | ||
} | ||
function permute(str, prefix = '') { | ||
if (str.length === 1) { | ||
result.push(prefix + str); | ||
return; | ||
} | ||
|
||
for (let i = 0; i < str.length; i++) { | ||
const currentChar = str[i]; | ||
const remainingChars = str.slice(0, i) + str.slice(i + 1); | ||
|
||
// Проверка на дубликаты | ||
if (str.indexOf(currentChar) !== i) { | ||
continue; | ||
} | ||
|
||
permute(remainingChars, prefix + currentChar); | ||
} | ||
} | ||
|
||
permute(string); | ||
|
||
// Удаление дубликатов | ||
const uniquePermutations = [...new Set(result)]; | ||
|
||
return uniquePermutations; | ||
} | ||
|
||
// Пример использования | ||
const inputString1 = 'abc'; | ||
const permutations = getPermutationsWithoutDuplicates(inputString1); | ||
console.log(permutations); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function productFib(prod) { | ||
let [a, b] = [0, 1]; | ||
while (a * b < prod) { | ||
[a, b] = [b, a + b]; | ||
} | ||
return [a, b, a * b === prod]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function modifyString(input) { | ||
const words = input.split(' '); | ||
const modifiedWords = words.map(word => { | ||
const firstLetter = word.charAt(0); | ||
const restOfWord = word.slice(1); | ||
const modifiedWord = restOfWord + firstLetter + 'ау'; | ||
return modifiedWord; | ||
}); | ||
const result = modifiedWords.join(' \t'); | ||
return result; | ||
} | ||
|
||
// Пример использования | ||
const inputString12 = 'привет всем'; | ||
const modifiedString = modifyString(inputString12); | ||
console.log(modifiedString); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function snail(arr) { | ||
const n = arr.length; | ||
const result = []; | ||
if (n !== arr[0].length) { | ||
return null; | ||
} | ||
let row = 0, col = 0; | ||
let count = n; | ||
while (count > 0) { | ||
for (let i = 0; i < count; i++) { | ||
result.push(arr[row][col++]); | ||
} | ||
col--; | ||
row++; | ||
for (let i = 0; i < count - 1; i++) { | ||
result.push(arr[row++][col]); | ||
} | ||
row--; | ||
col--; | ||
for (let i = 0; i < count - 1; i++) { | ||
result.push(arr[row][col--]); | ||
} | ||
col++; | ||
row--; | ||
for (let i = 0; i < count - 2; i++) { | ||
result.push(arr[row--][col]); | ||
} | ||
row++; | ||
col++; | ||
count -= 2; | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function recursiveSum(num) { | ||
if (num < 10) { | ||
return num; | ||
} | ||
let digitsSum = 0; | ||
while (num > 0) { | ||
digitsSum += num % 10; | ||
num = Math.floor(num / 10); | ||
} | ||
return recursiveSum(digitsSum); | ||
} | ||
|
||
// Пример использования | ||
const number = 123456789; | ||
const result = recursiveSum(number); | ||
console.log("Рекурсивная сумма числа", number, "до однозначного значения равна", result); | ||
|
Oops, something went wrong.