Answers for "10.4.1.1. Returning a Value¶ // Functions"

0

10.4.1.1. Returning a Value¶ // Functions

/*To return a value from functions that we create, we can use a return 
statement. A return statement has the form:*/

return someVal;

//where someVal is any value.//


/*This function has a single parameter, n, which is expected to be a 
positive integer. It returns the sum 1+2+...+n.*/

function sumToN(n) {
   let sum = 0;
   for (let i = 0; i <= n; i++) {
      sum += i;
   }
   return sum;
}

console.log(sumToN(3));
Console Output

6
Posted by: Guest on June-24-2021

Code answers related to "10.4.1.1. Returning a Value¶ // Functions"

Code answers related to "Javascript"

Browse Popular Code Answers by Language