How to stop javascript code being broken when going into visual editor

Is there any way to stop this

Don’t have JavaScript in your editor. It’s dirty and difficult to maintain (as you’ve found out).

Instead, you could:

  1. Use a page template
  2. Use custom fields (post meta)
  3. Use conditional code in your functions.php

For 3), as an example:

function wpse_202946_script() {
    if ( is_page( 'id_or_title_or_slug' ) ) {
        wp_enqueue_script(
            'my-theme-script-name',
            get_template_directory_uri() . '/path/to/script.js',
            null, // Dependencies
            null, // Version
            true  // In footer
        );
    }
}

add_action( 'wp_enqueue_scripts', 'wpse_202946_script' );