An algorithm for finding a js array

topic description

evenly take the elements of a specified percentage coefficient from an array of variable length and return a new array. The new array must contain the first and last elements of the array of variable length

Mar.18,2022

var a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];

function getSubArr(arr , percent){
    // 
    let interval =parseInt( arr.length * percent *0.01);
    // 
    let backArr = [];
    backArr = arr.filter((item,index)=>{
        return (index+2) % interval === 0
    })
    // 
    let hasEnd = (arr.length-1) % interval === 0 ;
    return backArr = hasEnd ? backArr : backArr.concat(arr[arr.length -1])
}

console.log(getSubArr(a,10))

as I understand it,
/ / the new array contains the first and last terms of the old array and those items that meet the criteria
let arrNew = arrOld.filter ((item,index) = > {

)
if(index === 0 || index === arrOld.length) return true;
return xxx //xxx 

})


function average(arr,pd){
  var newArr=[];
  pd = 1/(pd.slice(0,pd.length-1)/100); //
  var ave = arr.length/pd; //
  pd = Math.ceil(pd); //
  newArr.push(arr[0]);
  for(var i=1;i<arr.length;iPP){
    if(newArr.length+1>=ave)
      break;
    if(i%pd==0) {newArr.push(arr[i])}; //
  }
  newArr.push(arr[arr.length-1]);
  return newArr;
} 
average(a,'33%');
Menu