采用数据结构和 returns 整数的函数

Function that takes data structure and returns integer

我收到了以下代码挑战,但我不确定该怎么做。作为一名前端开发人员,到目前为止我还没有使用过数据结构。任何解决方案或帮助将不胜感激。下面附上整个挑战。

const data = [
  { max: 5, min: -23, code: 'ATGCAGTG', epoch: '1465992000000' },
  { max: 66, min: 33, code: 'CGCCCCGG', epoch: '1466078400000' },
  { max: 93, min: 26, code: 'AGCGCATT', epoch: '1466164800000' },
  { max: 56, min: 44, code: 'CCAGCTCG', epoch: '1466251200000' },
  { max: 29, min: 19, code: 'GCTGTAGT', epoch: '1466337600000' },
  { max: 21, min: -66, code: 'TGGTGATT', epoch: '1466424000000' },
  { max: 91, min: 76, code: 'TCACAGCA', epoch: '1466510400000' },
  { max: 86, min: -14, code: 'TAGAGCCT', epoch: null },
  { max: 63, min: 23, code: 'TTCTCCCG', epoch: '1466683200000' },
];

INSTRUCTIONS
Write a function (which may use other functions) that takes the above data structure as input and returns an integer which is the largest difference between max and min, where code starts with the character 'T' or 'C' and contains the sequence 'AG', and epoch represents a weekday.

  • The fields max and min will be positive or negative integers, code and epoch will always be a string.
  • The field epoch may be null.
  • You may use javascript Date and Math functions. Please avoid using external libraries. Please code in javascript.

正如我评论中提到的,纪元位不清楚。除此之外,假设没有空时期,它应该像下面这样。您将不得不使用纪元过滤器。

const data = [
  { max: 5, min: -23, code: 'ATGCAGTG', epoch: '1465992000000' },
  { max: 66, min: 33, code: 'CGCCCCGG', epoch: '1466078400000' },
  { max: 93, min: 26, code: 'AGCGCATT', epoch: '1466164800000' },
  { max: 56, min: 44, code: 'CCAGCTCG', epoch: '1466251200000' },
  { max: 29, min: 19, code: 'GCTGTAGT', epoch: '1466337600000' },
  { max: 21, min: -66, code: 'TGGTGATT', epoch: '1466424000000' },
  { max: 91, min: 76, code: 'TCACAGCA', epoch: '1466510400000' },
  { max: 86, min: -14, code: 'TAGAGCCT', epoch: null },
  { max: 63, min: 23, code: 'TTCTCCCG', epoch: '1466683200000' },
];
const findCode = (data) => {
  const codes = data.filter(item => item.code.startsWith('T') || item.code.startsWith('C'));
  const filteredCodes = codes.filter(item => item.code.includes('AG'));
  const filteredEpochs = filteredCodes.filter(item => item.epoch);
  const filteredEpochsWithMaxMin = filteredEpochs.map(item => {
    const maxMin = {
      max: item.max,
      min: item.min,
    };
    return maxMin;
  });
  const maxMinDiff = filteredEpochsWithMaxMin.map(item => item.max - item.min);
  const maxMinDiffMax = Math.max(...maxMinDiff);
  return maxMinDiffMax;
}
console.log(findCode(data));

所以如果我没理解错的话,epoch 可能为空,但不能是星期六或星期日。如果是这种情况,这将是一个可能的解决方案

const findMax = (data) => {
 const result = data.filter(element => {
    const isWeekDay = new Date(+element.epoch)
    if(isWeekDay.toString().includes("Sat") || isWeekDay.toString().includes("Sun")){
        return false
    } else {
        return true
    }
}).filter(element => {
    if(element.code.slice(0,1) == "T" || element.code.slice(0,1) == "C"){
        return true
    } else {
        return false
    }
}).filter(element=> element.code.includes("AG")).map(element=> {
    element.difference = element.max - element.min
    return element
}).reduce((prev, curr) => {
      return Math.max(prev.difference, curr.difference)
})
 return result
}

console.log(findMax(data))