SHARE
    TWEET
    Hasli4

    lists and ifelse

    Jun 19th, 2025
    978
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    1. // === Решения задач по массивам ===
    2. // 1. Добавить фрукт
    3. let fruits = ['яблоко', 'банан'];
    4. fruits.push('вишня');
    5. // fruits → ['яблоко', 'банан', 'вишня']
    6. // 2. Удалить число 30
    7. let nums = [10, 20, 30, 40, 50];
    8. nums.splice(2, 1);
    9. // nums → [10, 20, 40, 50]
    10. // 3. Заменить «зелёный» на «жёлтый»
    11. let colors = ['красный', 'зелёный', 'синий'];
    12. colors.splice(1, 1, 'жёлтый');
    13. // colors → ['красный', 'жёлтый', 'синий']
    14. // 4. Вставить «C» между «B» и «D»
    15. let letters = ['A', 'B', 'D', 'E'];
    16. letters.splice(2, 0, 'C');
    17. // letters → ['A', 'B', 'C', 'D', 'E']
    18. // 5. Найти и убрать «рыба»
    19. let animals = ['кот', 'собака', 'рыба', 'птица'];
    20. let idx = animals.indexOf('рыба');
    21. if (idx !== -1) {
    22. animals.splice(idx, 1);
    23. }
    24. // animals → ['кот', 'собака', 'птица']
    25. // 6. Сумма первого и последнего
    26. let values = [5, 15, 25, 35];
    27. let sum = values[0] + values[values.length - 1];
    28. values.push(sum);
    29. // values → [5, 15, 25, 35, 40]
    30. // 7. Очистить всё
    31. let data = [1, 2, 3, 4, 5];
    32. data.splice(0, data.length);
    33. // data → []
    34. // 8. Удалить последний
    35. let names = ['Аня', 'Борис', 'Вика', 'Гоша'];
    36. names.splice(names.length - 1, 1);
    37. // names → ['Аня', 'Борис', 'Вика']
    38. // 9. Добавить в начало через splice
    39. let queue = ['Игрок2', 'Игрок3'];
    40. queue.splice(0, 0, 'Игрок1');
    41. // queue → ['Игрок1', 'Игрок2', 'Игрок3']
    42. // 10. Условно добавить 'd'
    43. let list = ['a', 'b', 'c'];
    44. if (list.indexOf('d') === -1) {
    45. list.push('d');
    46. }
    47. // list → ['a', 'b', 'c', 'd'] (или без изменения, если 'd' уже есть)
    48. // === Решения задач на условные операторы ===
    49. // 1. Пустой ли массив?
    50. function isEmpty(arr) {
    51. if (arr.length === 0) {
    52. return "Пустой массив";
    53. } else {
    54. return "Есть элементы";
    55. }
    56. }
    57. // 2. Есть ли элемент
    58. function contains(arr, val) {
    59. if (arr.indexOf(val) !== -1) {
    60. return "Найдено";
    61. } else {
    62. return "Не найдено";
    63. }
    64. }
    65. // 3. Сравнение длин
    66. function compareLength(a, b) {
    67. if (a.length > b.length) {
    68. return "Первый больше";
    69. } else if (a.length < b.length) {
    70. return "Второй больше";
    71. } else {
    72. return "Длины равны";
    73. }
    74. }
    75. // 4. Безопасный доступ по индексу
    76. function getAt(arr, idx) {
    77. if (idx >= 0 && idx < arr.length) {
    78. return arr[idx];
    79. } else {
    80. return "Индекс вне диапазона";
    81. }
    82. }
    83. // 5. Тернарная проверка пустоты
    84. const isEmptyTernary = arr =>
    85. arr.length === 0 ? "Пустой массив" : "Есть элементы";
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /