I'm SO close to solving this problem :D

Hello there. I am soooooooo close to solving this problem and it feels great. The final step is where I am having trouble. Here is my code below. Also, here is link to the challenge (: The part I cannot figure out is how to add the hyphens after the last letter of each string.

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

I’m not sure that you want to join together all these repetitions of this string yet. It could work though. The repeat method doesn’t give you the ability to insert another character, so you would have to do that before calling repeat

Thanks for your response; I appreciate your assistance. The logic behind me doing that was to repeat the string, so just for my notes, that would work if we didn’t need to insert special character, correct? I was repeating it with the number of the num variable

Hint: you can do a liitle bit of research about Array.prototype.fill() - this method is very helpful for this problem.

2 Likes

Thank you. I will do that

It seems like if I were to do that, everything except last index will be replaced with -.

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

  const repeat = asString[loc].repeat(num)

  
  
} 

Maybe I will need to find a way to put spaces in there and replace the spaces with -?

Your variable name asString is very confusing. You have an array.

What is the thing that you want many copies of? It isn’t quite -

Did you read an MDN article about fill? I was kinda hoping that you will catch some examples like this:

console.log(Array(3).fill(4));                 // [4, 4, 4]
1 Like

I did, but it looks like it just replaces it with 4 instead of putting it between. MDN wording always throws me off lol. The only way I can think of was thinking of something like this?

const fruits = ["Banana", " ", "Orange", " ", "Apple", " ", "Mango"];

const newArray = [];

for (let i = 0; i < fruits.length; i++) {
    if (fruits[i] === " ") {
        newArray.push('-')
    } else if (typeof fruits[i] === 'string')
        newArray.push(fruits[i])
}
console.log(newArray)

See?

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

  
  
} 
[ '-', '-', '-', 'string' ]

ok, how about fill some array with the word we needed first.

After that we can use some other method - to convert it to string with hyphens

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

You can always use a simple for loop and basic if statements if you are getting lost in the advanced string and array methods.

1 Like

Here is the array . .

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

I changed the variable name to make more sense

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

} 

ok, do you have more questions? Looks like you are on a right path.

I keep doing what you told me to read, but it replaces all indexes except the last one with - instead of putting - between each one

How does your code look like right now?

function modifyMultiply (str,loc,num) {
//   console.log(str)
  const myArray = str.split(' ');
  console.log(myArray)
  console.log(myArray.fill('-', 0, 3))

  
//   console.log(asString(3).fill('-'))
  const repeat = asString[loc].repeat(num)

  
  
} 

Now, step 2 states to ‘then use the second parameter, which will be an integer, to find the corresponding word in the given string.’ Which in this case, is string

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

  
//   console.log(asString(3).fill('-'))
  const repeat = asString[loc].repeat(num)

  
  
}