0

I'm working on a problem that is expecting a const array to deeply equal a new set of values.

How would I go about this?

Instructions: Iterate through the array and multiply a number by 10 if it is greater than or equal to 5.

What I have:

const timesTenIfOverFive = [23, 9, 11, 2, 10, 6]; for(var i = 0; i < timesTenIfOverFive.length; i++){ if (timesTenIfOverFive[i] >= 5) { console.log(timesTenIfOverFive[i]*10); } else console.log(timesTenIfOverFive[i]); } // console.log(timesTenIfOverFive); // -> should print [230, 90, 110, 2, 100, 60] 
2
  • 1
    equal a new set of values : const result = timesTenIfOverFive.map(e => e >= 5 ? e * 10 : e) Commented Jul 8, 2018 at 2:52
  • ‘const’ doesn’t give an immutable value, it gives an immutable assignment, with which you cannnot reassign the variable, but still able to manipulate non-primitive data structure (object, array, etc) Commented Jul 8, 2018 at 9:26

1 Answer 1

1

const only means that the variable in question can't be reassigned - you can still mutate it. (Non-primitives, like objects and arrays, can be mutated.) The question is asking you to iterate over the array and mutate it, which is pretty easy with a for loop or forEach - on each iteration, assign the result to timesTenIfOverFive[i]:

const timesTenIfOverFive = [23, 9, 11, 2, 10, 6]; for(var i = 0; i < timesTenIfOverFive.length; i++){ if (timesTenIfOverFive[i] >= 5) { timesTenIfOverFive[i] *= 10; } } console.log(timesTenIfOverFive);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.