* Improve the clarity on a challenge sentence * add string type and remove double quotes Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
		
			
				
	
	
		
			120 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
---
 | 
						|
id: 56533eb9ac21ba0edf2244bd
 | 
						|
title: Passing Values to Functions with Arguments
 | 
						|
challengeType: 1
 | 
						|
videoUrl: 'https://scrimba.com/c/cy8rahW'
 | 
						|
forumTopicId: 18254
 | 
						|
dashedName: passing-values-to-functions-with-arguments
 | 
						|
---
 | 
						|
 | 
						|
# --description--
 | 
						|
 | 
						|
<dfn>Parameters</dfn> 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 <dfn>"passed"</dfn>) into a function when it is called are known as <dfn>arguments</dfn>.
 | 
						|
 | 
						|
Here is a function with two parameters, `param1` and `param2`:
 | 
						|
 | 
						|
```js
 | 
						|
function testFun(param1, param2) {
 | 
						|
  console.log(param1, param2);
 | 
						|
}
 | 
						|
```
 | 
						|
 | 
						|
Then we can call `testFun` like this: `testFun("Hello", "World");`. We have passed two string arguments, `Hello` and `World`. Inside the function, `param1` will equal the string `Hello` and `param2` will equal the string `World`. Note that you could call `testFun` again with different arguments and the parameters would take on the value of the new arguments.
 | 
						|
 | 
						|
# --instructions--
 | 
						|
 | 
						|
<ol><li>Create a function called <code>functionWithArgs</code> that accepts two arguments and outputs their sum to the dev console.</li><li>Call the function with two numbers as arguments.</li></ol>
 | 
						|
 | 
						|
# --hints--
 | 
						|
 | 
						|
`functionWithArgs` should be a function.
 | 
						|
 | 
						|
```js
 | 
						|
assert(typeof functionWithArgs === 'function');
 | 
						|
```
 | 
						|
 | 
						|
`functionWithArgs(1,2)` should output `3`.
 | 
						|
 | 
						|
```js
 | 
						|
if (typeof functionWithArgs === 'function') {
 | 
						|
  capture();
 | 
						|
  functionWithArgs(1, 2);
 | 
						|
  uncapture();
 | 
						|
}
 | 
						|
assert(logOutput == 3);
 | 
						|
```
 | 
						|
 | 
						|
`functionWithArgs(7,9)` should output `16`.
 | 
						|
 | 
						|
```js
 | 
						|
if (typeof functionWithArgs === 'function') {
 | 
						|
  capture();
 | 
						|
  functionWithArgs(7, 9);
 | 
						|
  uncapture();
 | 
						|
}
 | 
						|
assert(logOutput == 16);
 | 
						|
```
 | 
						|
 | 
						|
You should call `functionWithArgs` with two numbers after you define it.
 | 
						|
 | 
						|
```js
 | 
						|
assert(
 | 
						|
  /functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(
 | 
						|
    code.replace(/\s/g, '')
 | 
						|
  )
 | 
						|
);
 | 
						|
```
 | 
						|
 | 
						|
# --seed--
 | 
						|
 | 
						|
## --before-user-code--
 | 
						|
 | 
						|
```js
 | 
						|
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-user-code--
 | 
						|
 | 
						|
```js
 | 
						|
uncapture();
 | 
						|
 | 
						|
if (typeof functionWithArgs !== "function") { 
 | 
						|
  (function() { return "functionWithArgs is not defined"; })();
 | 
						|
} else {
 | 
						|
  (function() { return logOutput || "console.log never called"; })();
 | 
						|
}
 | 
						|
```
 | 
						|
 | 
						|
## --seed-contents--
 | 
						|
 | 
						|
```js
 | 
						|
```
 | 
						|
 | 
						|
# --solutions--
 | 
						|
 | 
						|
```js
 | 
						|
function functionWithArgs(a, b) {
 | 
						|
  console.log(a + b);
 | 
						|
}
 | 
						|
functionWithArgs(10, 5);
 | 
						|
```
 |