1

I am trying to take an array containing decimal numbers and increase each value by a decimal number chosen by a user input.

Ultimately I am trying to take an HSL light value from a base color chosen by a user, and if it's less than 0.5 I want to render 5 divs with incremented light values starting at the user's light input. If the value is greater than 0.5 I want to render 5 divs with decremented light values starting at the user's input.

function App() {

  let i = 0,
  floats = [];
  const userInput = 0.3;

while (i < 0.5) {
  i = (i + 0.1).toFixed(1);
  floats.push(i);
  i = parseFloat(i);
}

console.log(userInput + floats);

I can't explain the results that I'm receiving in accurate terms. It shows the array values with the input value together in one array, except that there is no comma between the user input and the array. Here is an example of the results that I'm receiving.

0.30.1,0.2,0.3,0.4,0.5

4
  • Where is light declared? Commented Apr 14, 2019 at 3:11
  • I made an editing mistake. I originally made the const userInput as light. I changed it to make it clearer that the particular const was provided as an example user input, but I forgot to change it in the console.log. Commented Apr 14, 2019 at 3:14
  • Maybe I'm misunderstanding what you're expecting to receive instead of 0.30.1,0.2,0.3,0.4,0.5. Could you clarify please? Commented Apr 14, 2019 at 3:17
  • I want the floats values to increase by the userInput to have the end result be newArray = [0.4, 0.5, 0.6, .07] which is the result of adding the userInput to each index of the float array Commented Apr 14, 2019 at 3:21

1 Answer 1

1

Fixing your current code, you just need to use parseFloat to coerce both the user input and your cursor to get a number with a decimal value.

function App() {

  let i = 0,
    floats = [];
  const userInput = 0.3;

  while (i < 0.4) {
    i = (i + 0.1).toFixed(1);
    floats.push(parseFloat(i) + parseFloat(userInput));
    i = parseFloat(i);
  }

  console.log(floats);
}

App();

Sign up to request clarification or add additional context in comments.

1 Comment

I made a mistake by changing the const light to const userInput without also changing it in the console.log. I want the floats values to increase by the userInput to have the end result be newArray = [0.4, 0.5, 0.6, .07] which is the result of adding the userInput to each index of the float array.

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.