Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let a = [1,2,3,4,5,6,7,8,9];
  2. /// 1
  3. {
  4.     let s = ``;
  5.     for(let i = 0; i < a.length; i+=2)
  6.         s += a[i];    
  7.     console.log(`Задание 1\nЭлементы с чётным индексом: ` + s);
  8. }
  9. /// 2
  10. {
  11.     let s = ``;
  12.     for(let i = 0; i < a.length; i++)
  13.         if(a[i] % 2 == 0)
  14.             s += a[i];    
  15.         console.log(`Задание 2\nВсе чётные элементы массива: ` + s);
  16. }
  17. /// 3
  18. {
  19.     let n = 0;
  20.     for(let i = 0; i < a.length; i++)
  21.         if(a[i] > 0)
  22.             n++;
  23.     console.log(`Задание 3\nКоличество положительных чисел в массиве: ` + n);
  24. }
  25. /// 4
  26. {
  27.     let n = 0;
  28.     for(let i = 1; i < a.length; i++)
  29.         if(a[i-1] < a[i])
  30.             n++;
  31.     console.log(`Задание 4\nКоличество элементов больше предыдущего: ` + n);
  32. }
  33. /// 5
  34. {
  35.     let n = 0;
  36.     for(let i = 1; i < a.length; i++)
  37.         if(a[i] > 0 && a[i-1] > 0 || a[i] < 0 && a[i-1] < 0)
  38.             n++;
  39.     console.log(`Задание 5\nКоличество пар с одинаковым знаком: ` + n);
  40. }
  41. /// 6
  42. {
  43.     let n = 0;
  44.     for(let i = 1; i < a.length - 1; i++)
  45.         if(a[i-1] < a[i] && a[i] > a[i+1])
  46.             n++;
  47.     console.log(`Задание 6\nКоличество локальных максимумов: ` + n);
  48. }
  49. /// 7
  50. {
  51.     let t;
  52.     for(let i = 0; i < Math.round(a.length / 2); i++)
  53.     {
  54.         t = a[i];
  55.         a[i] = a[a.length - i - 1];
  56.         a[a.length - i - 1] = t;
  57.     }
  58.     console.log(`Задание 7\nПеревёрнутый массив: ` + a);
  59. }
  60. /// 8
  61. {
  62.     let tempI, k = 0, tmp;
  63.     for(let i = 1; i < a.length; i++)
  64.     {
  65.         for(let j = k; j < a.length; j++)
  66.         {
  67.             if(j == k)
  68.                 tempI = j;
  69.             if(a[j] < a[tempI])
  70.                 tempI = j;
  71.         }
  72.         tmp = a[k];
  73.         a[k] = a[tempI];
  74.         a[tempI] = tmp
  75.         k++;
  76.     }
  77.     console.log(`Задание 8\nОтсортированный массив: ` + a);
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement