Learn Functional Programming by Building a Spreadsheet - Step 102

I’ve got no clue what should i do next or what i did wrong as the hint advices “Your random function should return a random number between the first two numbers in the array.”
I’ve tried before with implicitli returning the value but it’s the same issue.

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 => {
    const numbers = nums.splice(0,2)
    return Math.random() * (Math.max(numbers) - Math.min(numbers)) + Math.min(numbers)
  }
}

// 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/126.0.0.0 Safari/537.36

Challenge Information:

Learn Functional Programming by Building a Spreadsheet - Step 102

1 Like

Hello, I’m glad to help.
Your issue so far:

  1. if nums is an array, nums.slice(0,2) will still return an array, so Math.max(numbers) will return NaN since you cannot put array as a peremeter into Math.max().
  2. return Math.random() * (Math.max(numbers) - Math.min(numbers)) + Math.min(numbers) is correct except the peremeter I mentioned above, also it says the result should use Math.floor method.
    Solution:
  3. You need to get the elements from the array and pass it as an peremeter into Math.max() and Math.min(). Tip: You can use spread operator
    for example: const min = Math.min(…nums);
  4. You need to use Math.floor
1 Like

Yeah, i forgot that it needs Math.floor(), but that i cannot pass array into min( ) and max() function was really helpful. Thank you very much <3.