PDA

View Full Version : random questions w/o repetition



shinichi7
8th January 2013, 14:37
hi newbie here and even in QT... i can't work on non-repeating question to display...please help


function riddles1() {
var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
var rs

db.transaction(function(tx) {
rs = tx.executeSql('SELECT * FROM riddle')
})

//randomized riddle
for ( var i = 1; i < (rs.rows.length - 1); i++) {
var n = Math.floor(Math.random() * (rs.rows.length))

}}

alizadeh91
8th January 2013, 15:28
what do you mean by var!?

amleto
8th January 2013, 16:47
looks like c#. Which rather begs the question "what does this have to do with Qt?"

wysota
8th January 2013, 20:26
Looks more like JavaScript...

ChrisW67
8th January 2013, 21:55
Yes, Javascript the other language we had to have as part of QML. SQL local storage appears in several places in the Qt docs. Doesn't help shed any light on the OP's actual problem/question though.

wysota
8th January 2013, 22:42
I'm guessing OP wants to randomly choose a row from the database and wants assurance that rows already chosen will not be picked again.

shinichi7
10th January 2013, 10:51
var is variable... yes it's javascript... i use sql to store my riddles question. it's randomized the riddles but it repeats... how to eliminate the one that already given?

sir, what is OP? and how to use it? no idea at all

Santosh Reddy
10th January 2013, 10:53
Only way is to keep track of questions/riddles which were already asked :)

wysota
10th January 2013, 10:58
var is variable... yes it's javascript... i use sql to store my riddles question. it's randomized the riddles but it repeats... how to eliminate the one that already given?
Read all the questions into an array and then when a riddle is asked, remove it from the array. This way the array will always contain only unasked riddles. If you need it to be persistant, have a bool field in the database marking whether the question was asked or not and update it accordingly.


sir, what is OP?
Original poster --- you :)

shinichi7
10th January 2013, 11:09
so i have to do it again in array format... oh my :eek:
:D now i know what OP is...

here the complete js code


function riddles() {
var db = openDatabaseSync ("riddleDB", "1.0", "riddle database", 10000)
var rs

db.transaction(
function(tx) {
tx.executeSql ('CREATE TABLE IF NOT EXISTS riddle(riddle_id NUMBER,' +
'riddle_question VARCHAR2, riddle_answer VARCHAR2, score NUMBER)');

rs = tx.executeSql('Select riddle_id from riddle');
if (rs.rows.length == 0) {
tx.executeSql('INSERT INTO riddle VALUES(1, "What goes up but never goes down?", "Age", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(2, "What pet is always on the floor?", "Carpet", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(3, "The more you take the more you leave behind", "Footsteps", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(4, "What is the only room from which no one can enter or leave?", "Mushroom", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(5, "The person who makes it, sells it. The person who buys it never uses it and the person who uses it doesnt know they are. What is it?", "Coffin", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(6, "What goes round the house and in the house but never touches the house?", "Sun", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(7, "What is in seasons, seconds, centuries and minutes but not in decades, years or Days?", "N", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(8, "The more you have of it, the less you see. What is it?", "Darkness", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(9, "What is black when you get it, red when you use it, and white when you are all through with it?", "Charcoal", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(10, "My life can be measured in hours, I serve by being devoured.Thin, I am quick Fat, I am slow Wind is my foe.", "Fire", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(11, "I build up castles.I tear down mountains.I make some men blind,I help others to see.What am I?", "Sand", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(12, "What is it that after you take away the whole, some still remains?", "Wholesome", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(13, "I can be long, or I can be short. I can be grown, and I can be bought. I can be painted, or left bare. I can be round, or square. What am I?", "Fingernails", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(14,"What has a neck and no head, two arms but no hands?", "Shirt", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(15, "Remove six letters from this sequence to reveal a familiar English word. BSAINXLEATNTEARS", "Banana", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(16, "I am white, I am round, but not always around. Sometimes you see me, sometimes you dont. What am I?", "Moon", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(17, "I have an end but no beginning, a home but no family, a space without room. I never speak but there is no word I cannot make. What am I?", "Keybord", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(18, "I am an insect, & the first half of my name reveals another insect. Some famous musicians had a name similar to mine. What am I? ", "Beetle", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(19, "It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it youll die. What is it?", "Nothing", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(20, "I never was, am always to be. No one ever saw me, nor ever will. And yet I am the confidence of all, To live and breathe on this terrestrial ball. What am I?", "Tomorrow", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(21, "There was a green house. Inside the green house there was a white house. Inside the white house there was a red house. Inside the red house there were lots of babies. What is it?", "Watermelon", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(22, "If you can purchase 8 eggs for 26 cents, how many can you buy for a cent and a quarter?", "Eight", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(23, "If you look you cannot see me. And if you see me you cannot see anything else. I can make anything you want happen, but later everything goes back to normal. What am I?", "Imagination", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(24, "I have a head and tail but no body. What am I?", "Coin", 10, "music.mp3")');
tx.executeSql('INSERT INTO riddle VALUES(25, "When you have me, you feel like sharing me. But, if you do share me, you dont have me. What am I?", "Secret", 10, "music.mp3")');

}
})
}


//Riddle_Question

function riddles1() {
var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
var rs
var QCount
//var ridCount

db.transaction(function(tx) {
rs = tx.executeSql('SELECT * FROM riddle')
})

//randomized riddle
for ( var i = 1; i < (rs.rows.length - 1); i++) {
var n = (Math.random() * (rs.rows.length))
if (null != rs.rows.length) {
QCount++;
}
}

hidden.text = rs.rows.item(n).riddle_id
txtQuestion.text = rs.rows.item(n).riddle_question
ridCount++
answers.text = ""
}

function score1() {
var db = openDatabaseSync("riddleDB","1.0","riddle database", 10000)
var rs

db.transaction(function(tx) {
rs = tx.executeSql('SELECT * FROM riddle where riddle_id = "' + parseInt(hidden.text) + '"')
})

if (answers.text == rs.rows.item(0).riddle_answer)
ridScore = ridScore + 1

if ((answers.text != rs.rows.item(0).riddle_answer))
riddlemaster.state = "wrong"
}

wysota
10th January 2013, 14:21
You don't have to use arrays if you use the second solution I suggested.