0

I'm trying to simplify a javascript function so it's more readable

Currently I'm trying to change this function below.

export default function generateSentimentLineGraphPoints(items) {
  /**
   * Generate array of 24 Zeros
   */
  let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
  const nOfEntries = Array.apply(null, Array(24)).map(
    Number.prototype.valueOf,
    1
  ); 
.....
}

to more simply use this function,

function generateTime() {
  /**
   * Generate array of 24 Zeros
   */
  let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
  const nOfEntries = Array.apply(null, Array(24)).map(
    Number.prototype.valueOf,
    1
  );
  return values, nOfEntries;
}

with,

function generateTime() {
  /**
   * Generate array of 24 Zeros
   */
  let values = Array.apply(null, Array(24)).map(Number.prototype.valueOf, 0);
  const nOfEntries = Array.apply(null, Array(24)).map(
    Number.prototype.valueOf,
    1
  );
  return values, nOfEntries;
}

This isn't working for some reason - i'm not sure why, it won't load the graph properly. Any ideas?

1
  • 1
    I think it needs more code to clarify. How are you using the old and new functions ? What graph are not loading ? Did you get any error message? Also, another thing is that JS does not allow returning multiple values, so you either return the 2 values as an object return {values, nOfEntries} and use destructing when calling the function: let {values, nOfEntries} = generateTime(), or return as an array return [values, nOfEntries] and caling it by: let [values, nOfEntries] = generateTime(). See this link for more info: javascripttutorial.net/javascript-return-multiple-values Commented Jul 11, 2021 at 10:00

1 Answer 1

1

return values, nOfEntries; returns the value of nOfEntries, it doesn't return values at all. A JavaScript function can only return one thing. You're using the comma operator there, which evaluates its left-hand operand (values), throws away the result, then evaluates its right-hand operator (nOfEntries) and takes that result as the operation result.

To return multiple values, wrap them in an array or object:

return [values, nOfEntries];

or

return {values, nOfEntries};

Side note: That code to create an array filled with a given value is unnecessarily complex. Back in the day it probably should have just been a function that uses a loop, but these days you can use Array.prototype.fill:

let values = Array(24).fill(0);
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a simpler way to do the nOfEntries code as well?
@LeCoda - The original code is exactly the same as for values but with 1 instead of 0, so...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.