Advertisement
Hasli4

ForEach JS_2

Jul 12th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Решения задач по методу forEach
  2.  
  3. // Задача 1. Обнулить все флаги
  4. let emails1 = [
  5.   { subject: 'Привет', read: false },
  6.   { subject: 'Счёт',   read: false },
  7.   { subject: 'Напоминание', read: false }
  8. ];
  9. emails1.forEach(function(mail) {
  10.   mail.read = false;
  11. });
  12. console.log(emails1);
  13.  
  14.  
  15. // Задача 2. Перевести цены в копейки
  16. let prices = [99.5, 150, 200.25];
  17. let inCents = [];
  18. prices.forEach(function(price) {
  19.   inCents.push(Math.round(price * 100));
  20. });
  21. console.log(inCents); // [9950, 15000, 20025]
  22.  
  23.  
  24. // Задача 3. Подсветить чётные строки
  25. // Предполагается, что в HTML есть <ul id="list"><li>…</li></ul>
  26. const nodes = document.querySelectorAll('#list li');
  27. nodes.forEach(function(li) {
  28.   const n = parseInt(li.textContent, 10);
  29.   if (n % 2 === 0) {
  30.     li.style.backgroundColor = 'yellow';
  31.   }
  32. });
  33.  
  34.  
  35. // Задача 4. Собрать e‑mail‑адреса
  36. let users = [
  37.   { name: 'Иван', email: '[email protected]' },
  38.   { name: 'Оля',  email: '[email protected]' },
  39.   { name: 'Пётр', email: '[email protected]' }
  40. ];
  41. let emails = [];
  42. users.forEach(function(u) {
  43.   emails.push(u.email);
  44. });
  45. console.log(emails); // ['[email protected]', '[email protected]', '[email protected]']
  46.  
  47.  
  48. // Задача 5. Суммарная длина строк
  49. let words = ['apple','banana','cherry'];
  50. let totalLength = 0;
  51. words.forEach(function(w) {
  52.   totalLength += w.length;
  53. });
  54. console.log(totalLength); // 16
  55.  
  56.  
  57. // Задача 6. Выделить дубликаты
  58. let items = [1,2,2,3,3,3];
  59. let seen = [];
  60. let dups = [];
  61. items.forEach(function(v) {
  62.   if (seen.indexOf(v) === -1) {
  63.     seen.push(v);
  64.   } else {
  65.     dups.push(v);
  66.   }
  67. });
  68. console.log(dups); // [2,3,3]
  69.  
  70.  
  71. // Задача 7. Маркировка пройденных шагов
  72. let steps = ['Intro','Basics','Loops','Finish'];
  73. let completedSteps = [];
  74. let done = false;
  75. steps.forEach(function(step) {
  76.   if (!done) {
  77.     completedSteps.push(step);
  78.     if (step === 'Loops') {
  79.       done = true;
  80.     }
  81.   }
  82. });
  83. console.log(completedSteps); // ['Intro','Basics','Loops']
  84.  
  85.  
  86. // Задача 8. Обратный порядок без reverse()
  87. let arr = [1,2,3,4];
  88. let indices = Array.from(arr.keys()); // [0,1,2,3]
  89. indices.reverse().forEach(function(i) {
  90.   console.log(arr[i]);
  91. });
  92. // Выведет 4,3,2,1
  93.  
  94.  
  95. // Задача 9. Пропустить пустые строки
  96. let data = ['hello','','world','','!'];
  97. let nonEmpty = [];
  98. data.forEach(function(s) {
  99.   if (s !== '') {
  100.     nonEmpty.push(s);
  101.   }
  102. });
  103. console.log(nonEmpty); // ['hello','world','!']
  104.  
  105.  
  106. // Задача 10. Обновить каждый объект (добавить fullPrice)
  107. let products = [
  108.   { name: 'A', price: 100, tax: 0.2 },
  109.   { name: 'B', price: 200, tax: 0.1 }
  110. ];
  111. products.forEach(function(p) {
  112.   p.fullPrice = p.price + p.price * p.tax;
  113. });
  114. console.log(products);
  115. // [
  116. //   { name:'A', price:100, tax:0.2, fullPrice:120 },
  117. //   { name:'B', price:200, tax:0.1, fullPrice:220 }
  118. // ]
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement