Skip to content

Lecture 6 - JavaScript Array and Object Deep Dive (Time: 2:03:56) #56

@Murad-Hasan

Description

@Murad-Hasan

In this lecture, Nayem vai with an array like
const number = [1,2,3,4,NaN,false,'',5,7,8]
For converting an array to a truthy string and making a custom array with string concatenation like
[ 1, 2, 3, 4, 5, 7, 8 ]
The following code is

const truthyValue = number.reduce((acc,cur, index) => {
    if (index === 0) acc += '[ '
    if (cur){
        acc += cur.toString() + (`${index < number.length - 1 ? ', ' : ''}`)
    }
    if (index === number.length - 1 ) acc+=' ]'
    return acc;
}, '')

But when I am going to use another array-like

const number = [1,2,3,4,NaN,false,'']

For this, why is the code not working?
Maybe, One reason is that the array doesn't end with a number.
Have anyone any idea about this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions