Advertisement
sluchaynayakotya

gus 2

Jun 30th, 2020
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // list of (random?) numbers, and random sum of pair (neighbours?)
  2. const n = 42;
  3. let a;
  4. let sum;
  5. {
  6.   a = Array(n).fill().map(() => Math.random()*(n+1)|0);
  7.   const i = Math.random()*(n-1)|0;
  8.   sum = a[i]+a[i+1];
  9. }
  10. // result
  11. const i = (function find_neighbours_index(a, sum) {
  12.   for (let i = 0; i < a.length-1; ++i) if (a[i]+a[i+1]===sum) return i;
  13.   throw Error();
  14. })(a, sum);
  15.  
  16. console.log(`given list (${n} e.):`);
  17. a.forEach((e,i) => console.log(i,a[i]));
  18. console.log(`calculated sum: ${sum}, found: ${a[i]+a[i+1]}==${a[i]}+${a[i+1]} at index: ${i}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement