window.jsConsoleState = {
  lines: ["This is a JavaScript console.\nTo clear history, run clear()\nTo exit, run exit()"],
  isLooping: true
}

const jsConsole = () => {
  // Show the previous lines above the prompt
  const input = prompt(window.jsConsoleState.lines.join("\n"))
  // Push user input for next prompt
  window.jsConsoleState.lines.push(input)
  // Evaluate user input
  let result
  try {
    result = window.eval(input)
  } catch (err) {
    result = err
  }
  // Quit execution if the user exited
  if (!window.jsConsoleState.isLooping) {
    return
  } else {
    // Push the evaluation return value and then display the next prompt
    window.jsConsoleState.lines.push("> " + result)
    jsConsole()
  }
}

window.exit = () => {
  window.jsConsoleState.isLooping = false
}

window.clear = () => {
  window.jsConsoleState.lines = []
}

// Initiate
jsConsole()