0

There is an array arr = [1, 2, "a", 4, 5];. Using loop (for...of) every element of array must be pushed inside another empty array result = [];.

But, inside arr there is an element that needed to be replaced inside loop. Here, we need to replace string "a" with number 3 and than push it inside array result.

In the end we need to get an array result = [1, 2, 3, 4, 5];. As you can see string "a" was replaced and instead number 3 was pushed.

!Important initial array arr shouldn't be mutated and loop is a required condition.

Example:

const arr = [1, 2, "a", 4, 5];
const result = [];

for(let elem of arr) {
  if(typeof elem === "string") {
    // changing elem to number 3 and pushing it into result array instead of "a"
  }
// ...
}

Tried use several different array methods, but it didn't help.

2
  • why not result.push(3)? also you could use map, btw what does the 3 refer to, is it always 3 or the index of the array Commented Apr 15, 2023 at 14:07
  • use for (let i = 0; i < arr.length; i++) if u want acces to index. or both map and forEach also gives access to index Commented Apr 15, 2023 at 14:11

3 Answers 3

1

You can replace the string "a" with the number 3 and push every element into a new array using the for...of loop as follows:

const arr = [1, 2, "a", 4, 5];
const result = [];

for (let elem of arr) {
  if (typeof elem === "string" && elem === "a") {
    // replace "a" with number 3 and push it into the result array
    result.push(3);
  } else {
    // push the current element into the result array
    result.push(elem);
  }
}

console.log(result); // [1, 2, 3, 4, 5]

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

1 Comment

Also note that ‘typeof elem === "string"’ is redundant with the strict equality check on “a”
0

Just use an Array.map

here 3 each time

const arr = [1, 2, "a", 4, 5, "b"];
const result = arr.map(item => typeof item === "string" ? 3 : item)
console.log(result)

Here index+1

const arr = [1, 2, "a", 4, 5, "b"];
const result = arr.map((item, i) => typeof item === "string" ? i + 1 : item)
console.log(result)

3 Comments

Allowed to only use loop, like for...of, for(let...)
.map is also an iterator...
yes, but a requirement mentioned allowing using only loop iterators
0

You may want to use Array.reduce here. Something like:

const reducer = (acc, v, i) => 
  [...acc, isNaN(+v) ? (acc[i-1] || 0) + 1 : +v];

const arr = [1, 2, "a", 4, 5];
const result = arr.reduce(reducer, []);
console.log(`in: ${JSON.stringify(arr)}`);
console.log(`out: ${JSON.stringify(result)}`);

const arr2 = ["b", 2, "a", 4, 5];
const result2 = arr2.reduce(reducer, []);
console.log(`in: ${JSON.stringify(arr2)}`);
console.log(`out: ${JSON.stringify(result2)}`);

const arr3 = ["b", 2, "a", 4, "5"];
const result3 = arr3.reduce(reducer, []);
console.log(`in: ${JSON.stringify(arr3)}`);
console.log(`out: ${JSON.stringify(result3)}`);

// or in increments by choice
const reducerFactory = step => (acc, v, i) => 
  [...acc, isNaN(+v) ? (acc[i-1] || 0) + step : +v];

const arr4 = ["b", 4, "a", 8, 10, "12"];
const result4 = arr4.reduce(reducerFactory(2), []);
console.log(`in: ${JSON.stringify(arr4)}`);
console.log(`out: ${JSON.stringify(result4)}`);
.as-console-wrapper {
    max-height: 100% !important;
}

Comments

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.