Skip to content

Instantly share code, notes, and snippets.

@nickheal
Created November 20, 2020 15:12
Show Gist options
  • Save nickheal/536cab61e8cf0bd05ccc2f1e3d69762d to your computer and use it in GitHub Desktop.
Save nickheal/536cab61e8cf0bd05ccc2f1e3d69762d to your computer and use it in GitHub Desktop.
Complicated clear code
function myFunction(name, telephone, email) {
const requiredFields = name && telephone;
if (!requiredFields) {
const modalContainer = document.getElementById('modal-container');
const modal = document.createElement('div');
const basicString = 'Encountered a problem submitting the form. Missing '
if (!name && !telephone) {
modal.textContent = basicString + 'name & telephone fields.';
} else if (!name) {
modal.textContent = basicString + 'name field.';
} else {
modal.textContent = basicString + 'telephone field.';
}
modalContainer.appendChild(modal);
return;
}
window.fetch('https://myapi.com', {
method: 'POST',
body: JSON.stringify({
name,
telephone,
email
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment