Left And Right

beginner1 of 8

Learn about left and right in fp-ts either

Code Editor

01-left-and-right.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('getUserById', () => {
  const users = [
    { id: 1, name: 'Alice' },
    { id: 2, name: 'Bob' },
    { id: 3, name: 'Charlie' },
  ]

  it('returns left with error for invalid ID', () => {
    const result = getUserById(0, users)
    expect(result).toEqual({ _tag: 'Left', left: 'Invalid ID' })
  })

  it('returns right with user if found', () => {
    const result = getUserById(2, users)
    expect(result).toEqual({ _tag: 'Right', right: { id: 2, name: 'Bob' } })
  })

  it('returns left with error if user not found', () => {
    const result = getUserById(99, users)
    expect(result).toEqual({ _tag: 'Left', left: 'User not found' })
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises