// //cube
// let cube = n => n*n*n
// alert(cube(10))

// //avg2
// let avg2 = (a,b) => (a+b)/2
// alert (avg2 (10,7))

// //sum3
// let sum3 = (a,b,c) => {
//     a = a || 0;
//     b = b || 0;
//     c = c || 0;
//     var result = a+b+c
//     alert (result)
// }

// sum3(10,5,17)
// sum3(10,5)

// //intrandom
// let intRandom = (low,high) =>Math.round(Math.random()*((high || 0) - (low ||0)) + (low || 0))
// alert(intRandom(10,5))

// //greetAll

// let greetAll = (arguments) => {
//     let str = " "
//     for (let i = 0;i < arguments.length;i++){
//       str += arguments[i]
//     }
//     str.slice(0 , -1)
//     alert ("Hello" + "," + str)
// }
// greetAll("SuperMan")

// //sum (//nerabotaet)

// let sum = (arguments) =>{
//     var result = 0;
//     for (var i = 0;i <arguments.length; i++){
//     result += arguments[i]}
// }

// alert(sum(1,5,6,7,10))

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

function sort(field, boolean = true) {
    if (boolean) {
        return (a, b) => a[field] > b[field] ? 1 : -1
    }else {
        return (a, b) => a[field] < b[field] ? 1 : -1
    }
}

console.log(persons.sort(sort('age', false)))


//arr.map
let arr = ["1", {}, null, undefined, "500", 700];

let newArr = arr.map(function(a){
    return typeof(a) === 'string' ? parseInt(a) : a;
});

console.log(arr);
console.log(newArr);

//arr.reduce
var arr = ["0", 5, 3, "string", null]
var result = arr.reduce(function(previousValue, currentValue){
if(typeof(currentValue)==='number'){
previousValue*=currentValue;
};
return previousValue
 }, 1)
 alert(result);

 //object filter

 var phone = {
    brand: "meizu",
    model: "m2",
    ram: 2,
    color: "black",
};

function filter (object , name ,value) {
  let result = {}
  for (key in object) {
    if (name == key || object[key] == value){
      result[key] = object [key]
    }
  }
  return result
}
console.log (filter(phone,'color',2))

//object map
let obj = map({name: "Иван", age: 17},function(key,value){
    var result = {};
    result[key+"_"] = value + "$";
    return result;
})

function map (object , callback){
    let obj = {}
    for(let key in object) {
        Object.assign(obj,callback(key, object[key]));
    }  
    return obj
}

console.log(obj);

//рекурсия (считает сумму до какого-то числа)
function sumTo(n) {
    if (n == 1) return 1;
    return n + sumTo(n - 1);
  }
  
  alert( sumTo(10) );
  