OneCompiler

day1

130

// q1 =================================================================================================================
function countTrue(arrr){
let count = 0;
arrr.forEach( function(item){
if( item == true) count++;
})
// console.log(count)

// // reduce
   let res1=   arrr.reduce( (acc, item ) =>  (item == true ? acc++ : acc, acc), 0); 

let res = arrr.reduce( function( acc, item ){
   if( item == true){
       acc++;
   }
   return acc;
}, 0)

   // here we are using () for return single line
//    console.log(res)

}

countTrue([true, false, false, true, false]) // 2

countTrue([false, false, false, false]) // 0

countTrue([]) // 0

// q2 =================================================================================================================

function redundant(str){
return function(){
// console.log(str)
}
}

const f1 = redundant("apple")
f1() // "apple"

const f2 = redundant("pear")
f2() // "pear"

const f3 = redundant("")
f3() // ""

// q3 =================================================================================================================

function num_of_digits(number){
let r = String(number).split("").length

  //   second appoarch
  let str =   String(number)
  let count = 0
  for( i in str){
      count++;
  }
//   console.log(count)

}

num_of_digits(1000) // 4

num_of_digits(12) // 2

num_of_digits(1305981031) // 10

num_of_digits(0) // 1

// q3 =================================================================================================================

function addName( obj , key , value){
// obj[key] = value;
let res = { ...obj , key: value}
// console.log(res)

}

addName({}, "Brutus", 300) // { Brutus: 300 }

addName({ piano: 500 }, "Brutus", 400) // { piano: 500, Brutus: 400 }

addName({ piano: 500, stereo: 300 }, "Caligula", 440) // { piano: 500, stereo: 300, Caligula: 440 }

// q4 =================================================================================================================

const generationObj = {
'-3': { m: "great grandfather", f: "great grandmother" },
"-2": { m: "grandfather", f: "grandmother" },
"-1": { m: "father", f: "mother" },
0: { m: "me!", f: "me!" },
1: { m: "son", f: "daughter" },
2: { m: "grandson", f: "granddaughter" },
3: { m: "great grandson", f: "great granddaughter" },
};

function generation( num , gen){
let res = generationObj[num][gen]
// console.log(res)
}

generation(2, "f") // "granddaughter"

generation(-3, "m") // "great grandfather"

generation(1, "f") // "daughter"

// q5 =================================================================================================================

// he values false, null, 0, "", undefined, and NaN are falsey.
function compact(arr){
const r = arr.filter( item => item);
// console.log(r)
}

compact([0, 1, false, 2, "", 3]); // => [1, 2, 3]

// q6 =================================================================================================================

//function curring
function add(a){
return function(b){
// console.log(a+b)
}
}

add(10)(20) // 30

add(0)(20) // 20

add(-30)(80) // 50

// function exp(a){
// return function (b){
// return function (c){
// return a+b+c;
// }
// };
// };

// const r = exp(10)(20)(5);
// console.log(r)

// q7 =================================================================================================================

// Make a Circle with OOP se aage