Skip to content

Instantly share code, notes, and snippets.

View Drugak's full-sized avatar

Vitaliu Drugak Drugak

View GitHub Profile
// Factorial
function factorial(n) {
if(n === 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
factorial(5) //= 120
@Drugak
Drugak / findBrackets.js
Created February 15, 2016 10:03
Find correct close brackets
function findBrackets(str){
var brackets = "{}[]()";
var result = [];
var position;
for (var i = 0, max = str.split('').length; i < max; i++) {
position = brackets.indexOf(str.split('')[i]);
if (position > -1) {
if (position % 2 === 0) {
@Drugak
Drugak / Diagonal_Difference.js
Created January 12, 2016 13:35
Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals.
var a = [
[1,2,3,5],
[1,5,9,5],
[1,5,0,8],
[9,5,0,5]
]
function diagonalsX() {
var result = 0;
@Drugak
Drugak / JS-Algoritms: Stack-Exercises.js
Last active January 10, 2023 12:27
An example of a real-world stack is a Pez dispenser. Imagine that your virtual Pezdispenser is filled with red, yellow, and white colors and you don’t like the yellowones. Write a program that uses a stack (and maybe more than one) to remove theyellow ones without changing the order of the other candies in the dispenser.
function Stack() {
this.dataStore = [];
this.top = 0;
this.clear = clear;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
}
function Stack() {
this.dataStore = [];
this.top = 0;
this.clear = clear;
this.push = push;
this.pop = pop;
this.peek = peek;
this.length = length;
}
function factorial(n) {
if(n === 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
# Exercises
Write a function that inserts an element into a list only if the element to be insertedis larger than any of the elements currently in the list.
Larger can mean either greaterthan when working with numeric values, or further down in the alphabet, whenworking with textual values.
function List () {
this.array = [2,36,"a","d"];
this.add = add;
this.check = check;
this.print = print;