Algoritmi Flashcards

1
Q

Sumiranje niza

A
var niz = [1, 2, 3, 4, 5];
var suma = 0;

niz.forEach(function (element) {
suma += element;
});

console.log(suma);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Produkt/umnozak niza

A
// Produkt
var niz = [1, 2, 3, 4, 5];
var produkt = 1;

niz.forEach(function (element) {
produkt *= element;
});

console.log(produkt);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Prosjek niza

A
// Prosjek
var niz = [1, 2, 3, 4, 5];
var suma = 0;
var brojac = 0;

niz.forEach(function (element) {
suma += element;
brojac++;
});

var prosjek = suma / brojac;

console.log(prosjek);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Najmanja vrijednost u nizu

A
// Najmanja vrijednost
var niz = [1, 2, 3, 4, 5];
var min = Infinity;

niz.forEach(function (element) {
if(element < min)
min = element;

});

console.log(min);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Najveca vrijednost u nizu

A
// Najveća vrijednost
var niz = [1, 2, 3, 4, 5];
var max = -Infinity;

niz.forEach(function (element) {
if(element > max)
max = element;

});

console.log(max);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Prebrojavanje

A
// Prebrojavanje
var niz = ['a', 'b', 'b', 'c', 'c', 'c'];
var ukupno = {}

niz.forEach(function (element) {
if(!ukupno[element])
ukupno[element] = 0;

ukupno[element]++;
});

console.log(ukupno);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Najčešća vrijednost u nizu

A
// Najčešća vrijednost
var niz = ['a', 'b', 'b', 'c', 'c', 'c'];
var ukupno = {}

niz.forEach(function (element) {
if(!ukupno[element])
ukupno[element] = 0;

ukupno[element]++;
});

var max = -Infinity;

Object.keys(ukupno).forEach(function (element) {
	var broj = ukupno[element]
if(broj > max) {
  	max = broj;
najcesci_element = element;   }

});

console.log(najcesci_element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Najrjeđa vrijednost

A
// Najrjeđa vrijednost
var niz = ['a', 'b', 'b', 'c', 'c', 'c'];
var ukupno = {}

niz.forEach(function (element) {
if(!ukupno[element])
ukupno[element] = 0;

ukupno[element]++;
});

var min = Infinity;

Object.keys(ukupno).forEach(function (element) {
	var broj = ukupno[element]
if(broj < min) {
  	min = broj;
najrjedi_element = element;   }

});

console.log(najrjedi_element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Sortiranje niza

A
// Sortiranje
var niz = [10, 5, 15, 25, 20];

niz.sort(function (a, b) {
return a - b;
});

console.log(niz);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Map metoda

A
// Map metoda
var niz = [1, 2, 3, 4, 5];
var novi_niz = niz.map(function (element) {
	// Povećaj svaki element niza za 1
	return element + 1;
});

console.log(novi_niz);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Filter metoda

A
// FIlter metoda
var niz = [10, 15, 5, 25, 20];
var novi_niz = niz.filter(function (element) {
	// Zadrži samo elemente koji su veći od 10
	return element > 10;
});

console.log(novi_niz);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Reduce metoda

A
// Reduce metoda
var niz = [1, 2, 3, 4, 5];
var novi_niz = niz.reduce(function (a, b) {
	// Zbroji sve elemente u nizu
	return a + b;
}, 0);

console.log(novi_niz);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Uklanjanje brojeva iz niza koji se ponavljaju

A
let arr = [1, 2, 3, 4, 6, 7, 8, 1, 5, 5, 6, 6, 7, 3, 12, 12, 34, 45, 45];
cleanArr = arr.filter(function(item, pos) {
    return arr.indexOf(item) == pos;
})

console.log(‘Clean arr ‘, cleanArr);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Dodavanje procenta na elemente niza

A
// add 17% to every element
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let arr17 = arr.map(function(element, index, array){

    return element * (17/100) + element;

});

console.log(arr17);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly