Randell Dawson e0e6334628
fix(curriculum): Consolidated comments for JavaScript Algorithms and Data Structures challenges - part 3 of 4 (#38264)
* fix: remove example code from challenge seed

* fix: remove declaration from solution

* fix: added sum variable back in

* fix: reverted description back to original version

* fix: added examples to description section

* fix: added complete sentence

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: corrected typo

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

* fix: reverted to original desc with formatted code

* fix: removed unnecessary code example from description section

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: failiing test on iterate through array with for loop

* fix: changed to Only change this line

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: Manish Giri <manish.giri.me@gmail.com>
Co-authored-by: moT01 <tmondloch01@gmail.com>
2020-03-25 16:07:13 +01:00

3.2 KiB

id, title, challengeType, videoUrl, forumTopicId
id title challengeType videoUrl forumTopicId
56533eb9ac21ba0edf2244bd Passing Values to Functions with Arguments 1 https://scrimba.com/c/cy8rahW 18254

Description

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or "passed") into a function when it is called are known as arguments. Here is a function with two parameters, param1 and param2:
function testFun(param1, param2) {
  console.log(param1, param2);
}

Then we can call testFun: testFun("Hello", "World"); We have passed two arguments, "Hello" and "World". Inside the function, param1 will equal "Hello" and param2 will equal "World". Note that you could call testFun again with different arguments and the parameters would take on the value of the new arguments.

Instructions

  1. Create a function called functionWithArgs that accepts two arguments and outputs their sum to the dev console.
  2. Call the function with two numbers as arguments.

Tests

tests:
  - text: <code>functionWithArgs</code> should be a function.
    testString: assert(typeof functionWithArgs === 'function');
  - text: <code>functionWithArgs(1,2)</code> should output <code>3</code>.
    testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(1,2); uncapture(); } assert(logOutput == 3);
  - text: <code>functionWithArgs(7,9)</code> should output <code>16</code>.
    testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(7,9); uncapture(); } assert(logOutput == 16);
  - text: You should call <code>functionWithArgs</code> with two numbers after you define it.
    testString: assert(/^\s*functionWithArgs\s*\(\s*\d+\s*,\s*\d+\s*\)\s*/m.test(code));

Challenge Seed




Before Test

var logOutput = "";
var originalConsole = console
function capture() {
    var nativeLog = console.log;
    console.log = function (message) {
        if(message) logOutput = JSON.stringify(message).trim();
        if(nativeLog.apply) {
          nativeLog.apply(originalConsole, arguments);
        } else {
          var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
          nativeLog(nativeMsg);
        }
    };
}

function uncapture() {
  console.log = originalConsole.log;
}

capture();

After Test

uncapture();

if (typeof functionWithArgs !== "function") { 
  (function() { return "functionWithArgs is not defined"; })();
} else {
  (function() { return logOutput || "console.log never called"; })();
}

Solution

function functionWithArgs(a, b) {
  console.log(a + b);
}
functionWithArgs(10, 5);