Change or add character on string to loop


#1

hey I wanna ask how to looping through “string” and add some character between them
example

input: “8825879”
output: “882587-9” (add dash “-” because between numbers are the odd numbers )

i want to change it by not build in function
or not using es6


#2

So you want a dash only between odd numbers?


I trust you didn’t give up, just took a nap.

What is the one thing we can always keep in mind with dealing with objects? Are they iterable? If they are, then we have algorithms and methods to deal with them. If not then they must be pretty simple objects, then, wouldn’t we say?

function is_iterable(obj) {
  try {
    for (let x of obj) {
      return true;
    }
  } catch {
    return false;
  }
}

Given that the object is iterable that means we can explore its parts, index by index, character by character. We already know that strings are immutable so the plan is to use string slicing when it comes to reassigning the amended string.

Okay, that’s a function intended to validate inputs. Not what is asked for here. The input is iterable. First question answered.

Given that it is iterable, how might we single out the odd pairs?