Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to get a score in javascript

Avatar

Level 3

form.jpg

Hi everybody,

Thanks to a user, I have got  this exemple. I used it to do a quiz. A user answers  this quiz and you will be given his score. In this exemple he must get the different languages in the good order (for instance: Russian, French, English, and Italian). So far it works, but I don't know how to do the javascript to obtain the score.

Script of the button (click_event):

for (var i = 0; i < ListBox1.length; i++)

{

    if (ListBox1.getItemState(i))

    {

        ListBox2.addItem(ListBox1.getDisplayItem(i), ListBox1.getSaveItem(i));

    }

}

for (var i = ListBox1.length; i >= 0; i--)

{

    if (ListBox1.getItemState(i))

    {

        ListBox1.deleteItem(i);

    }

}

Script of the listbox (change_event)

for (var i = 0; i < this.items.nodes.length; i++)

{

    if (xfa.event.change == this.items.nodes.item(i).value)

    {

        XFAUtil.setProperty(this, "selectedIndex", i, XFAUtil.ContentType.Integer);

        break;

    }

}

Script of the total box (?_event)

Now it's your turn!

Any help will be appreciated

Many thanks

1 Accepted Solution

Avatar

Correct answer by
Level 7

I'm not exactly sure how you'd want to score the languages. Something like, if they're in alphabetical order, the user gets 4 points for the first answer, 3 for the second, etc. Or if they're all in the correct order, then the user gets 10 points. Or the user is answering how many lnguages he speaks, and gets 1 point for one language, 2 points for two languages, etc. So, here's some examples.

Script for calculate event of total box:

//to give a score based on alpabetical order individually

this.rawValue = 0;

if (ListBox2.getDisplayItem(0) == "English") this.rawValue = parseInt(this.rawValue) + 4;

if (ListBox2.getDisplayItem(1) == "French") this.rawValue = parseInt(this.rawValue) + 3;

if (ListBox2.getDisplayItem(2) == "Italian") this.rawValue = parseInt(this.rawValue) + 2;

if (ListBox2.getDisplayItem(3) == "Russian") this.rawValue = parseInt(this.rawValue) + 1;

or

//to give a score based on alphabetical order if all correct

this.rawValue = 0;

if (ListBox2.length == 4 && ListBox2.getDisplayItem(0) == "English" && ListBox2.getDisplayItem(1) == "French" && ListBox2.getDisplayItem(2) == "Italian") this.rawValue = 10;

or

//to give a score based on number of languages spoken, 1, 3, 6, 10 pts.

this.rawValue = 0;

for (var i = 0; i<ListBox2.length; i++) this.rawValue = parseInt(this.rawValue) + i + 1;

Hopefully, those examples cover what kind of scoring you might want to do.

View solution in original post

5 Replies

Avatar

Correct answer by
Level 7

I'm not exactly sure how you'd want to score the languages. Something like, if they're in alphabetical order, the user gets 4 points for the first answer, 3 for the second, etc. Or if they're all in the correct order, then the user gets 10 points. Or the user is answering how many lnguages he speaks, and gets 1 point for one language, 2 points for two languages, etc. So, here's some examples.

Script for calculate event of total box:

//to give a score based on alpabetical order individually

this.rawValue = 0;

if (ListBox2.getDisplayItem(0) == "English") this.rawValue = parseInt(this.rawValue) + 4;

if (ListBox2.getDisplayItem(1) == "French") this.rawValue = parseInt(this.rawValue) + 3;

if (ListBox2.getDisplayItem(2) == "Italian") this.rawValue = parseInt(this.rawValue) + 2;

if (ListBox2.getDisplayItem(3) == "Russian") this.rawValue = parseInt(this.rawValue) + 1;

or

//to give a score based on alphabetical order if all correct

this.rawValue = 0;

if (ListBox2.length == 4 && ListBox2.getDisplayItem(0) == "English" && ListBox2.getDisplayItem(1) == "French" && ListBox2.getDisplayItem(2) == "Italian") this.rawValue = 10;

or

//to give a score based on number of languages spoken, 1, 3, 6, 10 pts.

this.rawValue = 0;

for (var i = 0; i<ListBox2.length; i++) this.rawValue = parseInt(this.rawValue) + i + 1;

Hopefully, those examples cover what kind of scoring you might want to do.

Avatar

Level 3

It works perfectly ! I cope with the first one !

Many thanks for the help.

Avatar

Level 3

  Euh ! One more question !

If the user is mistaken or if he wants to do again the quiz, is it possible to delete the content of the ListBox2 with the reset Button but without selecting all answers ?

Thanks

Avatar

Level 7

Basically you want a "reset" button, right? Do you care if the list is in a different order? I.e., can the "reset" button just move everything back over to the first box regardless of order? If so,

for (var i = 0; i < ListBox2.length; i++) ListBox1.addItem(ListBox2.getDisplayItem(i));

for (var i = ListBox2.length; i>= 0; i--) ListBox2.deleteItem(i);

Avatar

Level 3

What more could I ask for?

Thanks a million !