Методы JavaScript: indexOf с условием – изучение различных методов

Для пояснения: вам нужны различные методы в JavaScript, включающие функцию indexOfс условием. Вот несколько примеров:

  1. Использование indexOfс условием проверки наличия элемента в массиве:

    const array = [1, 2, 3, 4, 5];
    const element = 3;
    if (array.indexOf(element) !== -1) {
    console.log("Element exists in the array");
    } else {
    console.log("Element does not exist in the array");
    }
  2. Использование indexOfс условием для поиска первого вхождения подстроки в строке:

    const string = "Hello, world!";
    const substring = "world";
    if (string.indexOf(substring) !== -1) {
    console.log("Substring found in the string");
    } else {
    console.log("Substring not found in the string");
    }
  3. Использование indexOfс условием для поиска индекса первого элемента массива, удовлетворяющего определенному условию:

    const array = [1, 2, 3, 4, 5];
    const condition = (element) => element > 3;
    const index = array.findIndex(condition);
    if (index !== -1) {
    console.log("Index of the first element satisfying the condition:", index);
    } else {
    console.log("No elements satisfy the condition");
    }