/* function a (text="called function alert") {
    alert(text);
} */
let a = (text="called function alert") => {alert(text)};
let cube = (number=1) => {number**3};
let avg = (a=0,b=0) => {(a+b)/2};
let sum = (a=0,b=0,c=0) => {a+b+c};
let intRnd =(min,max=0) => {
    if(max=0){
        return Math.floor(Math.random()*(min-max))+max;
    }   
    return Math.floor(Math.random()*(max-min))+min;
}
let greetAll = (...names) => {
    let greetArr = names;
    let someGreet = []
    for(let i=0; i<greetArr.length; i++){
        someGreet.push(names[i])
    }
    alert(someGreet);
}
/* greetAll('Kyrka','Vetal','Spydik','Vadik'); */

let sumArr = (...numarr) => {
   /*  let arrOfSum = numarr;
    let sum=0;
    for(let i=0; i<arrOfSum.length;i++){
        sum=sum+arrOfSum[i];
    }
    alert(sum); */
    return alert(numarr.reduce((a,b) => a+b));
}

/* sumArr(...[10,20,30]); */

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

function sortArray(array, property, direction=true) {
        if(direction === true){ 
    array.sort(function compare(a, b) {
        let result = (a[property] > b[property]) ? 1 :-1;
        return result;
    });
    return array;} else if(direction === false) {
        array.sort(function compare(a, b) {
            let result = (a[property] < b[property]) ? 1 :-1;
            return result;
        });
        return array;
    }
}

let sortedobj = sortArray(persons,"name");
console.log(sortedobj);

let mappedArray = ["1", {}, null, undefined, "500", 700].map((currentElement)=>{
   return typeof currentElement == 'string' ? Number(currentElement) : currentElement;
});

let reducedArray = ["0", 5, 3, "string", null].reduce((total,currentElement)=>{
     if (total == undefined){
       return total = +total;
    } 
    if( typeof total === "number" && typeof currentElement === "number"){
        return total * currentElement;
    }  else {
    return  total = total *1;
    } 

    

},1);
console.log(reducedArray);

function filterdObj(){
    return Object.filter = (obj, predicate) => 
                  Object.fromEntries(Object.entries(obj).filter(predicate));
            
}
let filter = filterdObj();
var phone = {
    brand: "meizu",
    model: "m2",
    ram: 2,
    color: "black",
};
console.log(filter(phone, ([key,value])=> value == "meizu" || key == "color"));

function sumrecurs(n) {
    if (n === 0){
        return 0;
    } else {
        return n + sumrecurs(--n)
    }
}


console.log(sumrecurs(5));


/* let map = function (obj, fn, ctx) {
    return Object.keys(obj).reduce((a, b) => {
      a[b] = fn.call(ctx || null, b, obj[b]);
      return a;
    }, {});
  };

  const x = map({name: "Иван", age: 17},function(key,value){
    var result = {};
    result[key+"_"] = value + "$";
    return result;
}); */
let f = function(key,value) {
    var result = {};
    result[key+"_"] = value + "$";
    return result;
}
let map = function(obj,f) {
    let result = {};
     Object.entries(obj).forEach((item)=>{
         result = {...result,...f(item[0],item[1])};
     })
     return result;
} 

let mappedObj
/* console.log(x); */

