0
0
Vueframework~20 mins

State machines with composables in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Machine Mastery in Vue
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component using a state machine composable?

Consider a Vue 3 component that uses a composable to manage a simple toggle state machine with states 'off' and 'on'. The component shows the current state and a button to toggle it.

What will the component display after clicking the toggle button twice?

Vue
import { ref } from 'vue';

function useToggleMachine() {
  const state = ref('off');
  function toggle() {
    state.value = state.value === 'off' ? 'on' : 'off';
  }
  return { state, toggle };
}

export default {
  setup() {
    const { state, toggle } = useToggleMachine();
    return { state, toggle };
  },
  template: `<div><p>State: {{ state }}</p><button @click="toggle">Toggle</button></div>`
};
A<div><p>State: off</p><button>Toggle</button></div>
B<div><p>State: on</p><button>Toggle</button></div>
C<div><p>State: off</p><button>Toggle</button></div> after two clicks
D<div><p>State: on</p><button>Toggle</button></div> after two clicks
Attempts:
2 left
💡 Hint

Each click toggles the state between 'off' and 'on'. Starting from 'off', two toggles bring it back to 'off'.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a composable for a state machine with states 'idle', 'loading', and 'success'?

Choose the correct Vue composable code that initializes a state machine with three states: 'idle', 'loading', and 'success', starting at 'idle'. It should provide a method to transition to 'loading' and then to 'success'.

A
import { ref } from 'vue';
export function useMachine() {
  const state = ref('idle');
  function start() { state = 'loading'; }
  function finish() { state = 'success'; }
  return { state, start, finish };
}
B
import { reactive } from 'vue';
export function useMachine() {
  const state = reactive({ current: 'idle' });
  function start() { state.current = 'loading'; }
  function finish() { state.current = 'success'; }
  return { state, start, finish };
}
C
import { ref } from 'vue';
export function useMachine() {
  const state = ref('idle');
  function start() { state.value = 'loading'; }
  function finish() { state.value = 'success'; }
  return { state, start, finish };
}
D
import { ref } from 'vue';
export function useMachine() {
  let state = 'idle';
  function start() { state = 'loading'; }
  function finish() { state = 'success'; }
  return { state, start, finish };
}
Attempts:
2 left
💡 Hint

Remember to use ref for reactive primitive values and update state.value.

🔧 Debug
advanced
2:00remaining
What error does this Vue composable code produce?

Analyze the following composable code for a state machine. What error will it cause when used in a component?

Vue
import { ref } from 'vue';

export function useCounterMachine() {
  const count = ref(0);
  function increment() {
    count += 1;
  }
  return { count, increment };
}
ATypeError: count is not a function
BTypeError: Assignment to constant variable
CReferenceError: count is not defined
DNo error, works correctly
Attempts:
2 left
💡 Hint

Check how count is updated inside increment.

state_output
advanced
2:00remaining
What is the value of 'status' after calling 'next()' three times in this state machine composable?

Given this composable managing states 'start', 'middle', and 'end' in order, what is the value of status after calling next() three times?

Vue
import { ref } from 'vue';

export function useStepMachine() {
  const states = ['start', 'middle', 'end'];
  const index = ref(0);
  const status = ref(states[index.value]);

  function next() {
    if (index.value < states.length - 1) {
      index.value++;
      status.value = states[index.value];
    }
  }

  return { status, next };
}
A'end'
B'middle'
C'start'
Dundefined
Attempts:
2 left
💡 Hint

Think about how the index changes and when it stops incrementing.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the benefit of using composables for state machines in Vue?

Choose the best explanation for why composables are useful when implementing state machines in Vue applications.

AComposables enforce strict typing on all state transitions at compile time.
BComposables automatically generate UI elements for each state without manual coding.
CComposables replace Vue's reactivity system with a simpler state management approach.
DComposables allow state machines to be reused across components with isolated reactive state and logic encapsulation.
Attempts:
2 left
💡 Hint

Think about how composables help organize and reuse code in Vue.