256 Kilobytes

Answers in Client-Side | By Some Guy

Published 4 days agoFri, 11 Jan 2019 23:40:27 -0800 | Last update 4 days agoFri, 11 Jan 2019 23:56:13 -0800

32 views, 0 RAMs, and 1 comments

Profile Photo - Some Guy Some Guy
🗎 2220 🗨 4 🐏 4
Classic User

Dear sirs,

Please help. My code--shown below--is not working how I would expect. Does anyone know how to fix this?

// Define the function
function multiply(a, b){ a * b }

// Call the function
var x = 10;
var y = 3;
var z = multiply(x, y);

// Show the result. Should show "30" but instead shows "undefined"
alert(z);

Thanks in advance.

Edit: For reference, this seems to be one of the challenges on Code Wars, which you can find more of if you follow that link.

Download more RAM. 🐏 ⨉ 0 Posted by Some Guy 4 days ago

Edit History

• [2019-01-11 23:40 PST] Some Guy (4 days ago)
🕓 Posted at 11 January, 2019 23:40 PM PST

This is a patsy account used by the 256 Kilobytes staff to seed content. 

Profile Photo - August R. Garcia August R. Garcia Posting on Internet... Portland, OR
🗎 33 🗨 319 🐏 25
Site Owner

The function should state "return a * b;" instead of "a * b;"

// Define the function
function multiply(a, b){ return a * b; }

// Call the function
var x = 10;
var y = 3;
var z = multiply(x, y);

// Shows "30" 
alert(z);

The "return" keyword is what makes a value get passed out of the function and back to the "z" variable. Without it, the function just ends.

From the Mozilla Web Docs:

When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. For example, the following function returns the square of its argument, x, where x is a number.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

Download more RAM. 🐏 ⨉ 0 Posted by August R. Garcia 4 days ago

Edit History

• [2019-01-11 23:47 PST] August R. Garcia (4 days ago)
🕓 Posted at 11 January, 2019 23:47 PM PST

The CIA wants all code in the cloud under their lock and key.  They want to ban compilers and make people think HTML is computer programming. - Terry A. Davis

Post a New Comment

To leave a comment, login to your account or create an account.