· 8 years ago · Mar 31, 2018, 04:30 PM
1var fizzFuzzDB = openDatabase('fizzFuzzDB', '1.0', 'Fizz Fuzz Database', 2 * 1024 * 1024);
2fizzFuzzDB.transaction(function (tran) {
3 // create the tables
4 tran.executeSql('CREATE TABLE IF NOT EXISTS NUMBERS (NUMBER)');
5 tran.executeSql('CREATE TABLE IF NOT EXISTS REPLACES (NUMBER, REPLACEMENT, PRIORITY)');
6
7 // ensure the tables are empty
8 tran.executeSql('DELETE FROM NUMBERS');
9 tran.executeSql('DELETE FROM REPLACES');
10
11 // populate the tables with relevant data
12 for (var i = 1; i <= 100; i++) {
13 tran.executeSql('INSERT INTO NUMBERS (NUMBER) VALUES (?)',[i]);
14 }
15 tran.executeSql('INSERT INTO REPLACES (NUMBER, REPLACEMENT, PRIORITY) VALUES (?, ?, ?)', [3, "Fizz", 1]);
16 tran.executeSql('INSERT INTO REPLACES (NUMBER, REPLACEMENT, PRIORITY) VALUES (?, ?, ?)', [5, "Buzz", 2]);
17
18 // generate and print the list
19 tran.executeSql("SELECT IFNULL( " +
20 "(SELECT GROUP_CONCAT(REPLACEMENT, '') " +
21 "FROM REPLACES R " +
22 "WHERE N.NUMBER % R.NUMBER = 0 " +
23 "ORDER BY PRIORITY), N.NUMBER) AS RESULT " +
24 "FROM NUMBERS N", [], function (tran, data) {
25 for (var i in data.rows) {
26 console.log(data.rows[i]["RESULT"]);
27 }
28 });
29});