js_hw_06.md 2.0 KB

cube

function cube(a){
	return a*a*a
}

console.log(cube(1));
console.log(cube(2));
console.log(cube(3));

avg2

function avg2(a,b){
	return (a+b)/2
}
console.log(avg2(1,2)) 
console.log(avg2(10,5)) 

sum3

function sum3(a,b,c){
	return a+b+c;
}
console.log(sum3(1,2,3)) // => 6
console.log(sum3(5,10,100500)) // => 100515
console.log(sum3(0,5,10))// => 15

intRandom

function intRandom(a,b){
	return (Math.round(Math.random()*(b-a)+a));
}


console.log(intRandom(2,15)) // возвращает целое случайное число от 2 до 15 (включительно)
console.log(intRandom(-1,-1)) // вернет -1
console.log(intRandom(0,1)) // вернет 0 или 1
console.log(intRandom(0,10)) // вернет 0 до 10 включительно

sum

function sum(...params){
	return params.reduce((a,b)=> a + b)

}

console.log(sum(1)) // => 1
console.log(sum(2)) // => 2
console.log(sum(10,20,40,100)) // => 170

avg

function avg(...params){
	 a = params.reduce((a,b)=> a + b)
	 return(a/params.length)
}

console.log(avg(1)) // => 1
console.log(avg(2)) // => 2
console.log(avg(10,20,40,100)) // => 42.5

sort

var persons = [
    {name: "Иван", age: 17},
    {name: "Мария", age: 35},
    {name: "Алексей", age: 73},
    {name: "Яков", age: 12},
	]

function sort(persons, age, true){
	return persons.sort((a,b,)=>(a.age > b.age)|| sort? true: fosle);
}

sort(persons,'age',);

map

["1", {}, null, undefined, "500", 700].map(x => ["string", "number"].indexOf(typeof x) > -1 ? Number(x): x)

array reduce

var arr = ["0", 5, 3, "string", null].map(x=>["string", "number"].indexOf(typeof x) > -1 ? Number(x): x);
// сделать филтр на элементы если абра кадабра то 1; 
 console.log(arr);
arr.reduce(function(a,b){
	return parseInt(a*b);
});