I'm SO close to solving this problem :D

I don’t think that you need to fill array with hyphens.

I would fill it the word.

And to add hyphens - that is the next step. Remember, we have cool join() method for that

So just put string in an array?

function modifyMultiply (str,loc,num) {
//   console.log(str)
  const myArray = str.split(' ');
  const newArray = [];
  for (let i = 0; i < myArray.length; i++) {
    if (myArray[i] === myArray[loc]) {
  const newArrayHyphen = newArray.join('-')
  console.log(newArrayHyphen + newArrayHyphen + newArrayHyphen + newArrayHyphen)

  const myArrayHyphen = myArray.join('-')
//   console.log(myArray[loc])

lol i’m just so stumped

Let’s write bunch of pseudocode.

our function needs to deal with three params: string, position of word(loc), number of repetitions

Step number 1

get the word with the right position

You already did that by using split(’ '), and accessing the item with the index === loc

So we have myArray[loc] - we should use it below.

So step 2

create bunch of duplicates of the word. Number of duplicates === num

So here fill() comes into play. You need to fill some array with exactly num instances of the word

We need to get an array wiht length === num. And each item of this array should be: myArray[loc]

After that step 3.

convert array into string. In such string, words should be separated by hyphens

The last step is easily done by using join method properly. So do research about that one

When you will perform all of the above, stuff will be solved, and on codewars you will see, that all of the above can be done with single line of code

Haha this is very technical. Step 2: You need to fill some array with exactly num instances of the word

That’s what I’m struggling with

that’s why I suggested to use fill() method. Seems like perfect tool for this task.
You can also use for loop for this. You know how many words you need(num), so you can use it to write decent loop whci will push items to some array.

Also there are alternative approaches with repeat(), if you want to use not arrays, but string manipulation.

When I use fill method like above, it completely replaces the words with -

Perhaps there exists more than 1 way to use fill?

1 Like

Yes, but using these examples, it still replaces the words with hyphen. I have read this article and am not understanding which one I should use…

Which is why I suggested using a raw loop.

Programming is not about guessing the right syntax. It is about first creating a logical plan and second translating that plan to syntax.

The first solution I sent at the very top had the logic in place though.

function modifyMultiply (str,loc,num) {
//   console.log(str)
  const asString = str.split(' ');
  const repeat = asString[loc].repeat(num)

No, that solution is missing the logic for putting dashes between the repeated words.

And THAT is where I am stuck.

Right… and first you need to come up with a logical plan to do that before you start looking for the syntax to handle that plan.

If I tell you to grab a piece of paper and write ‘cat’ 5 times, with a dash between instead of a space, could you describe how you would do that?

  1. First I write ‘cat’
  2. Then I write…
  3. Then I write…
1 Like

I thought about getting the word that repeats itself to go into an array

but only one word gets added to the array

In my plan above I am using join to deal with hyphens.

If you are going to use repeat on string, try to think: what exactly do you want to repeat

And after applying repeat, maybe there will be more steps before returning result?

holy I think I got it…

function modifyMultiply (str,loc,num) {
//   console.log(str)
   const myArray = str.split(' ');
   const newArray = [];
   for (let i = 0; i < myArray.length; i++) {
[ 'string', 'string', 'string', 'string' ]

close - now you need the last piece: join them together with dashes

this may cause problems

Do we want number of repetitions be equal to the legth of array?

1 Like