JS Dasar: 45. Function return value

by istiq20

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Function return value</title>
  </head>
  <body>
    <script>
      // membuat function dengan parameter dan return value
      function sayHello(firstName, lastName) {
        const say = `Hello ${firstName} ${lastName}`;
        return say;
      }

      // memanggil function dan menangkap return valuenya
      const result = sayHello("Eko", "Khannedy");
      document.writeln(`<p>${result}</p>`);

      // membuat function dengan return value lebih dari satu
      function getFinalValue(value) {
        if (value > 90) {
          return "A";
        } else if (value > 80) {
          return "B";
        } else if (value > 70) {
          return "C";
        } else if (value > 60) {
          return "D";
        } else {
          return "E";
        }
      }

      // menghentikan eksekusi dengan return
      const finalValue = getFinalValue(76);
      document.writeln(`<p>${finalValue}</p>`);

      function isContains(array, searchValue) {
        for (const element of array) {
          console.info(`Iterasi Element ${element}`);
          if (element === searchValue) {
            return true;
          }
        }
        return false;
      }

      const array = [1, 32, 4324, 3, 43, 53, 53, 43, 43, 535, 35, 5];
      const search = 43;
      const found = isContains(array, search);
      document.writeln(`<p>${found}</p>`); // true (43 dengan index ke-4 atau yang pertama kali ditemukan)
    </script>
  </body>
</html>