JSFiddle - React, Tailwind, and code Playground

by Andrea Ercolino

HTML

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<form>
  <input type="radio" id="type_person" name="type" value="person"/>
  <input type="radio" id="type_company" name="type" value="company" checked/>
  <input type="text" id="first_name" name="first_name" value="John"/>
  <input type="text" id="last_name" name="last_name" value="Doe"/>
  <input type="text" id="email" name="email" value="[email protected]"/>
  <input type="text" id="company_name" name="company_name" value="ACME"/>
  <input type="text" id="phone" name="phone" value="12-3"/>
</form>

JavaScript

jQuery(function ($) {
    $('body').click(function () {
	    console.log(solution($));
    });
});

function solution($) {
//	debugger;
    // write your code in JavaScript (Node.js 4.0.0)
    //
    // you can access DOM Tree using DOM Object Model:
    //    document.getElementById
    // or using jQuery:
    //    $('some_tag')
    //
    // you can write to stdout for debugging purposes, e.g.
    // console.log('this is a debug message');


    // NOTES
    // (1) it's not clear if validation shoul fail only for ('', '') or also for ('anything', '') and ('', 'anything')
    
    var result = false;
    var form$ = $('form');
    
    var selectedType = form$.find('input[name="type"]:checked').val();
    switch (selectedType) {
        
        case 'person':
            result = ValidatePerson();
        break;
        
        case 'company':
            result = ValidateCompany();
        break;
        
        default:
            throw new Error('Expected a radio button with states "person" and "company".');
        break;  // hehe... a coding style clash ("Unreachable 'break' after 'throw'.")
    
    }
    
    return result;
    
    
    function ValidatePersonName() {
        // assuming it's valid iif not both first_name and last_name are empty
        var first_name = form$.find('#first_name').val();
        var last_name = form$.find('#last_name').val();
        var result = (first_name + last_name).search(/\w/) > -1;
        return result;
    }
    
    function ValidateEmail() {
        // assuming it's valid iif not both first_name and last_name are empty
        var email = form$.find('#email').val();
        var pieces = email.split('@');
        var result = pieces.length === 2 &&
                pieces[0].search(/^[A-Za-z0-9.]{1,64}$/) === 0 &&
                pieces[1].search(/^[A-Za-z0-9.]{1,64}$/) === 0;
        return result;
    }
    
    function ValidateCompanyName() {
        var company_name = form$.find('#company_name').val();
...