Skip to content

Instantly share code, notes, and snippets.

@shri
Created June 10, 2014 01:14
Show Gist options
  • Save shri/cc06c33222c21c93be64 to your computer and use it in GitHub Desktop.
Save shri/cc06c33222c21c93be64 to your computer and use it in GitHub Desktop.
[wearscript] pebblespanish
<html style="width:100%; height:100%; overflow:hidden">
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<script>
// Fisher-Yates shuffling algorithm (http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array)
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;
}
// The real Kimono+Pebble+Wearscript stuff
var choices = ["a", "b", "c"];
var answer = "b";
var question = "abc";
var currentChoiceIndex = 0;
var right = 0;
var total = 0;
// Display the given choice
function displayChoice(index) {
WS.pebbleSetTitle(question+"?");
WS.pebbleSetSubtitle(( index + 1 ) + ". " + choices[ index ]);
};
// Iterate over the choices using +1, -1
function nextChoice(iter) {
currentChoiceIndex = (currentChoiceIndex + iter)
% 3;
if (currentChoiceIndex<0){
currentChoiceIndex = currentChoiceIndex + 3;
}
displayChoice(currentChoiceIndex);
};
// Checks whether selection was correct, displays result
function selectChoice() {
total = total + 1;
if (answer == choices[currentChoiceIndex]) {
right = right + 1;
WS.pebbleSetTitle("Correct!");
} else {
WS.pebbleSetTitle("Wrong!");
}
WS.pebbleSetSubtitle(question + " means " + answer);
WS.pebbleSetBody("You're "+right+" of "+total);
};
// Sets up and displays new question
function displayQuestion(q, qanswer, choice1, choice2) {
question = q;
choices = shuffle([choice1, choice2, qanswer]);
answer = qanswer;
WS.pebbleSetBody("");
displayChoice(0);
WS.pebbleVibe(2);
}
// Button press handler
WS.gestureCallback('onPebbleSingleClick', function(button) {
switch (button) {
case "DOWN":
nextChoice(1);
break;
case "UP":
nextChoice(-1);
break;
case "SELECT":
selectChoice();
break;
default:
WS.log("invalid choice");
break;
}
});
// Spanish specific logic
var questionbank = [];
function loadSpanishQuestion() {
var temp = questionbank[Math.floor(Math.random()*questionbank.length)];
var c1 = questionbank[Math.floor(Math.random()*questionbank.length)];
var c2 = questionbank[Math.floor(Math.random()*questionbank.length)];
displayQuestion(temp.spanish, temp.english, c1.english, c2.english);
}
function kimonoCallback(data) {
questionbank = data.results.words;
loadSpanishQuestion();
setInterval(function(){ loadSpanishQuestion(); }, 1000*45);
}
function main() {
if (WS.scriptVersion(1)) return;
WS.pebbleSetBody("");
$.ajax({
url:"http://www.kimonolabs.com/api/2haum126?apikey=989877be85a3ca05477428c8b41d4fbe&callback=kimonoCallback",
crossDomain:true,
dataType:"jsonp"
});
}
window.onload = main;
</script>
</body>
</html>
{"name":"Example"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment