Advertisement
fionor

Untitled

Jan 8th, 2021
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. const prepareArray = (items: Array<number>, medianValue: number, direction: number) => {
  2. const data = items.map((x) => x * direction > medianValue * direction ? x : null);
  3.  
  4. const indexes = data.reduce((indexes: Array<number>, item, index) => {
  5. if (item) {
  6. if (data[index - 1] === null) indexes.push(index - 1);
  7. if (data[index + 1] === null) indexes.push(index);
  8. }
  9. return indexes;
  10. }, []);
  11.  
  12. let indexOffset = 1;
  13. indexes.forEach(index => {
  14. data.splice(index + indexOffset, 0, medianValue);
  15. indexOffset++;
  16. });
  17.  
  18. console.log(indexes);
  19.  
  20. return data;
  21. };
  22.  
  23. let testFunction = (items: Array<number>, medianValue: number) => {
  24. const hight = prepareArray(items, medianValue, 1);
  25. const low = prepareArray(items, medianValue, -1);
  26. return { hight, low };
  27. };
  28.  
  29. let data = testFunction([10, 40, 60, 70, 80, 90, 10, 10, 70, 80, 90], 50);
  30. console.log(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement