Hone logo
Hone
Problems

Scalar Replacement in JavaScript Arrays

Scalar replacement is a common task in data processing where you need to replace all occurrences of a specific value (a "scalar") within an array with a new value. This is useful for cleaning data, standardizing values, or performing transformations on array elements. Your task is to write a JavaScript function that efficiently performs this scalar replacement.

Problem Description

You are required to implement a function called replaceScalar that takes three arguments: an array of numbers (arr), a scalar value (scalar), and a replacement value (replacement). The function should iterate through the array and replace every element that is equal to the scalar with the replacement value. The function should modify the original array in place and return the modified array.

Key Requirements:

  • The function must modify the original array directly (in-place modification).
  • The function should handle arrays containing any number of elements.
  • The function should correctly replace all occurrences of the scalar value.
  • The function should not modify elements that are not equal to the scalar value.

Expected Behavior:

The function should iterate through the input array arr. For each element, it should check if the element is equal to the scalar. If it is, the element should be replaced with the replacement value. After iterating through the entire array, the modified array should be returned.

Edge Cases to Consider:

  • Empty array: The function should handle an empty array gracefully without errors.
  • Scalar not present: The function should handle the case where the scalar value is not present in the array.
  • Array with mixed data types: While the problem specifies an array of numbers, consider how your solution would behave if the array contained other data types (e.g., strings, booleans). While not required to handle this explicitly, understanding the implications is good practice.

Examples

Example 1:

Input: [1, 2, 3, 2, 4, 2]
scalar: 2
replacement: 5
Output: [1, 5, 3, 5, 4, 5]
Explanation: All occurrences of the value 2 are replaced with 5.

Example 2:

Input: [1, 2, 3, 4, 5]
scalar: 6
replacement: 7
Output: [1, 2, 3, 4, 5]
Explanation: The scalar value 6 is not present in the array, so no changes are made.

Example 3:

Input: []
scalar: 10
replacement: 20
Output: []
Explanation: The input array is empty, so the function returns an empty array.

Constraints

  • arr will be an array of numbers.
  • scalar will be a number.
  • replacement will be a number.
  • The length of arr can be between 0 and 1000 (inclusive).
  • The function must complete within a reasonable time (e.g., less than 1 second) for arrays of length 1000.

Notes

Consider using a for loop or the forEach method to iterate through the array. In-place modification is key; avoid creating a new array unless absolutely necessary for efficiency. Think about how you can directly update the elements of the original array.

Loading editor...
javascript