Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md
2023-01-30 18:58:54 +02:00

2.7 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392cf Написання багаторазового JavaScript із функціями 1 https://scrimba.com/c/cL6dqfy 18378 write-reusable-javascript-with-functions

--description--

У JavaScript можна розділити код на багаторазові частини, які називаються функціями.

Приклад функції:

function functionName() {
  console.log("Hello World");
}

Ви можете викликати або активувати цю функцію, використавши дужки для написання її назви, ось так: functionName();. При кожному виклику функції на консоль буде виводитись повідомлення Hello World. При кожному виклику функції буде виконуватись код у фігурних дужках.

--instructions--

  1. Створіть функцію під назвою reusableFunction, яка друкує рядок Hi World на консоль.
  2. Викличте функцію.

--hints--

reusableFunction має бути функцією.

assert(typeof reusableFunction === 'function');

Якщо reusableFunction буде викликано, то вона повинна виводити рядок Hi World на консоль.

assert(testConsole());

Ви повинні викликати reusableFunction після її визначення.

const functionStr = reusableFunction && __helpers.removeWhiteSpace(reusableFunction.toString());
const codeWithoutFunction = __helpers.removeWhiteSpace(code).replace(/reusableFunction\(\)\{/g, '');
assert(/reusableFunction\(\)/.test(codeWithoutFunction));

--seed--

--after-user-code--


function testConsole() {
  var logOutput = "";
  var originalConsole = console;
  var nativeLog = console.log;
  var hiWorldWasLogged = false;
  console.log = function (message) {
    if(message === 'Hi World')  {
      console.warn(message)
      hiWorldWasLogged = true;
    }
    if(message && message.trim) logOutput = message.trim();
    if(nativeLog.apply) {
      nativeLog.apply(originalConsole, arguments);
    } else {
      var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
      nativeLog(nativeMsg);
    }
  };
  reusableFunction();
  console.log = nativeLog;
  return hiWorldWasLogged;
}

--seed-contents--


--solutions--

function reusableFunction() {
  console.log("Hi World");
}
reusableFunction();