Home > Blockchain >  Can you use repeat() in this instance? JS
Can you use repeat() in this instance? JS

Time:04-05

Hello I am working on a practice program for customers taking a position in line at a deli. Except when I call the line() function it will end with the first name in the array. // `The line is currently customer name

How would one allow that single string output to contain all the customers in my array? //ex: `The line is currently customer1 customer2 customer3 etc...

I was thinking I could use the repeat function for this but am having trouble making it work. Am I thinking about that wrong, or is there another method to do so? Thanks!

let deli = []

const line = (array) => {
    if (array.length === 0) {
        console.log(`The line is currently empty.`)
    } else if (array.length > 0) {
        let count = 0
        for (let el in array) {
            count  
            let order = `${count}. ${array[el]}`
            let greeting = console.log(`The line is currently: ${order}`)
            return greeting
        }
    }
}

line(deli)

EDIT: Hello, sorry. People always get mad at me on here, I'm very new to programming and still figuring everything out.

Essentially I'm using this below function to push names into the array. And when you call line() it should show all the names added!

const takeANumber = (katzArr, customer) => {
    let count = 0
    katzArr.push(customer)
    for (i = 0; i < katzArr.length; i  )
        if (katzArr.length) {
            count = katzArr.length
            let greeting = console.log(
                `Welcome ${customer}. You are number ${count} in line.`
            )
            return greeting
        }
}

CodePudding user response:

You can use .join() method. It works on arrays. So for example you have an array-
const customers = ['customer1', 'customer2', 'customer3']

customers.join(', ') would return customer1, customer2, customer3

CodePudding user response:

Cheers

const animals = ['pigs', 'goats', 'sheep'];

const line = (array) => {
    if (array.length === 0) {
        console.log(`The line is currently empty.`)
    } else if (array.length > 0) {
        let count = [];
        for(let i = 0; i < array.length; i  ){
         count.push(`${array[i]} ${i   1}`)
        }
       console.log(`The line is currently ${count.join(',')}`)
    }
}
line(animals)
  • Related