-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
1 lines (1 loc) · 1.88 KB
/
app.js
File metadata and controls
1 lines (1 loc) · 1.88 KB
1
function manage_alert(e){var t="<div class='alert alert-warning alert-dismissible fade show' role='alert'>"+e+"<button type='button' class='close' data-dismiss='alert' aria-label='Close'> \t\t\t \t<span aria-hidden='true'>×</span> \t\t\t\t</button> \t\t\t</div>";$(".alert_container").html(t),console.log("alert created"),$("#number").val("")}function getFactAjax(e){var t=new XMLHttpRequest;t.onreadystatechange=function(){4==this.readyState&&200==this.status&&(document.querySelector(".fact").innerText=this.responseText,console.log(this.responseText))},t.open("GET",e),t.send()}function get_url(e,t){if("Trivia"===t)url="http://numbersapi.com/"+e;else if("Date"===t){if(index=e.indexOf("/"),!(-1!==index&&3<e.length))return null;var n=e.slice(index+1),a=e.slice(0,index);if(12<n||n<1)return manage_alert("Invalid month, Please enter a month value between 1-12."),null;if(31<a||a<1)return manage_alert("Invalid day, Please enter a day value between 1-31."),null;if(29<a&&2==n)return manage_alert("Invalid day, Please enter a day value between 1-29 for February month."),null;url="http://numbersapi.com/"+(e=n+"/"+a)+"/date"}else url="http://numbersapi.com/"+e+"/math";return url}function display(){var e,t=document.getElementById("number"),n=t.value,a=document.getElementById("operation").value,l=document.getElementById("factContainer");"Date"==a?(t.setAttribute("type","text"),t.setAttribute("placeholder","dd/mm"),t.setAttribute("maxlength","5")):(t.setAttribute("type","number"),t.removeAttribute("placeholder"),t.removeAttribute("maxlength")),""===n||"Date"==a&&n.length<4?l.classList.add("d-none"):(l.classList.remove("d-none"),null!=(e=get_url(n,a))&&getFactAjax(e))}document.getElementById("number").addEventListener("input",display),document.getElementById("operation").addEventListener("change",display),$(".close").click(function(){$(this).parent().addClass("d-none"),console.log("alert closing handled")});