Last active
October 11, 2017 11:29
-
-
Save shah-smit/d2c14e2fd0ee4c088524cc23a8638a8c to your computer and use it in GitHub Desktop.
Helper for Axperiance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function httpGet(theUrl) | |
{ | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request | |
xmlHttp.send( null ); | |
return xmlHttp.responseText; | |
} | |
function httpPost(theUrl,obj) | |
{ | |
console.log(obj) | |
var xhr = new XMLHttpRequest(); | |
xhr.open("POST", theUrl, true); | |
//Send the proper header information along with the request | |
xhr.setRequestHeader("Content-type", "application/json"); | |
xhr.setRequestHeader("id", obj.id); | |
xhr.setRequestHeader("user_id", obj.user_id); | |
xhr.setRequestHeader("score", obj.score); | |
xhr.setRequestHeader("cat_id", obj.cat_id); | |
xhr.setRequestHeader("time_taken", obj.time_taken); | |
xhr.setRequestHeader("no_of_questions",obj.no_of_questions); | |
xhr.onreadystatechange = function() {//Call a function when the state changes. | |
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { | |
console.log("done"); | |
} | |
} | |
xhr.send(null); | |
} | |
function httpPatch(theUrl,user_score) | |
{ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("PATCH", theUrl, true); | |
//Send the proper header information along with the request | |
xhr.setRequestHeader("Content-type", "application/json"); | |
xhr.setRequestHeader("id", user_score.id); | |
xhr.setRequestHeader("update_time", user_score.time_taken); | |
xhr.setRequestHeader("update_score", user_score.score); | |
xhr.setRequestHeader("update_no_of_question", user_score.no_of_questions); | |
xhr.onreadystatechange = function() {//Call a function when the state changes. | |
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { | |
console.log("done"); | |
} | |
} | |
xhr.send(null); | |
} | |
function httpGetOptions(quesId,theUrl = "https://arck9app.azurewebsites.net/api/getOptions") | |
{ | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request | |
xmlHttp.setRequestHeader("quesid",quesId) | |
xmlHttp.send( null ); | |
return xmlHttp.responseText; | |
} | |
function httpGetAsync(theUrl, callback) | |
{ | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.onreadystatechange = function() { | |
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) | |
callback(xmlHttp.responseText); | |
} | |
xmlHttp.open("GET", theUrl, true); // true for asynchronous | |
xmlHttp.send(null); | |
} | |
function httpPostAsync(theUrl,obj,callback) | |
{ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("POST", theUrl, true); | |
//Send the proper header information along with the request | |
xhr.setRequestHeader("Content-type", "application/json"); | |
xhr.onreadystatechange = function() {//Call a function when the state changes. | |
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { | |
callback(xhr.responseText); | |
} | |
else if(xhr.status == 409){ | |
callback(xhr.responseText); | |
} | |
} | |
xhr.send(JSON.stringify(obj)); | |
} | |
function str_pad_left(string,pad,length) { | |
return (new Array(length+1).join(pad)+string).slice(-length); | |
} | |
/** | |
* Getting Data from the server | |
* It is a not a feasible way to do it. But it is the one of the best/easiest form | |
*/ | |
function getDataFromServer() | |
{ | |
httpGetAsync("https://arck9app.azurewebsites.net/api/getQuestions",function(data) | |
{ | |
questions = shuffle(JSON.parse(data)); | |
httpGetAsync("https://arck9app.azurewebsites.net/api/getOptions",function(data1) | |
{ | |
options = shuffle(JSON.parse(data1)); | |
httpGetAsync("https://arck9app.azurewebsites.net/api/getCorrectOptions",function(data2) | |
{ | |
correctAnswer = JSON.parse(data2); | |
startQuiz(); | |
}); | |
}); | |
}); | |
} | |
/** | |
* | |
* @param {AnyArray} array | |
* This method is stuffle the array such that questions and options are not same for all the users | |
*/ | |
function shuffle(array) { | |
var currentIndex = array.length, temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment