Learn Functional Programming by Building a Spreadsheet - Step 102

Tell us what’s happening:

Error: 4. Your random function should return a whole number (integer).

I was asked to use Math.random() and Math.floor() for this step.
I’m confused is the Math.floor() method not supposed to round down & return the whole number (integer)??

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

const spreadsheetFunctions = {
  sum,
  average,
  median,
  even: nums => nums.filter(isEven),
  someeven: nums => nums.some(isEven),
  everyeven: nums => nums.every(isEven),
  firsttwo: nums => nums.slice(0, 2),
  lasttwo: nums => nums.slice(-2),
  has2: nums => nums.includes(2),
  increment: nums => nums.map(num => num + 1),
  random: nums => Math.floor(Math.random() * (nums[1] - nums[2]) + nums[2])
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 102

This property should use the first two numbers from an array to generate a random whole number.

How do you access the first two items in an array?

nums.slice(0,2)
I’ve tried using this method but it did not work.

Remember that arrays use zero-based indexing, so the first item in an array is not at index 1.

1 Like

Thank you.
I also had to restructure my - (max - min) + min
why does it matter if i have nums[0] as a max and nums[1] as a min?