Duplicate score with concatenated variable

Hi everyone!!!
I’m doing a project where I have 6 random letters, and the student is supposed to find all the possible meaningful words that can be created with these letters at his disposal.
The score varies according to the number of letters used.
6 letters= 10 points
5 letters= 6 points
4 letters= 3 points
3 letters= 1 point
The problem arises when the student repeats the same word 2 times, because the score will be added back to the total, and I would not like that.
Is it possible to score only once for each word found?
I have been trying for days to no avail… :cold_sweat:
If it is possible I would also like it to be reported as an error in that case, or that the word has already been written…
Thank you for your help.
I also attach the video to make you understand better…

This should be possible. Without having attempted it - my initial approach would be as follows:

  1. Place all of the correct responses into an array. Perhaps an array for each number of letters. (6 letter array, 5 letter array, 4 letter array, and a 3 letter array)
  2. With a conditional statement - if answer exists - award the points and remove the answer from the array
  3. When a word is typed a second time - (if answer does not exist) - display message and do not award points

If I get a chance - I will see if I can work up a sample.

1 Like

Thank you very much for your reply Greg, I hope you can find the time, an example would be much appreciated.
Best regards :pray:

I did get this figured out but I will need a little time to write up the explanation.

1 Like

Ok - here is the breakdown. I went ahead and added the feedback message if the word has already been selected.

I realize my mock up may not work for you based on how you already have things created in your project - but I hope this is helpful to you in some way such that you might implement it in your own.

On Load Code

// This is the array that holds the answers for a given set of letters
// We have the word (all caps), point value, and a flag for whether
// the word has been found already. Adjust as desired
// I only added a few words for the sample

prez.variable("answers",[
{word: "CAROTA", points: 10, found: 0},
{word: "AORTA", points: 6, found: 0},
{word: "CART", points: 3, found: 0},
{word: "CAT", points: 1, found: 0}
]);

// This creates an empty array to store the learner's answer

prez.variable("myAnswer",[]);

// This function is used to build the displayed letters as they
// are selected on the screen

window.build = function() {
    var ansDisplay = prez.object("ansDisplay");
    ansDisplay.text(prez.variable("myAnswer").join(""));
};

// This is a search function to look up the submitted answer within the array.
// If the word is found and the flag found is set to 1 - show feedback
// If not flagged, go ahead and award the points, then flag the word.
window.lookUp = function(nameKey) {
    for (var i = 0; i < prez.variable("answers").length; i++) {
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 1) {
            prez.object("feedback").show();
        }
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 0) {
            prez.variable("score",prez.variable("score")+prez.variable("answers")[i].points);
            prez.variable("answers")[i].found = 1;
        }
    }
};

// This function simply shows the letters again and clears the answer array
// Adjust object names as needed.

window.restore = function() {
prez.object("Group Object_5").show();
prez.object("Group Object_8").show();
prez.object("Group Object_11").show();
prez.object("Group Object_14").show();
prez.object("Group Object_17").show();
prez.object("Group Object_20").show();
prez.variable("myAnswer",[]);
}

Quite a bit going on there - do be sure to update all object names and variable names to suit your project.

Here is the code on the letters.

// Add the letter to the array
prez.variable("myAnswer").push("A");

// Create the display showing the letters chosen
build();

// Hide the letter just clicked
prez.object(this.name()).hide();

// Hides the already found message when first letter is clicked
// Placed on every letter since we don't know which one will be first.
prez.object("feedback").hide();

Only one more place with code. - the SUBMIT button

// This combines all the letters submitted into a single string
var temp = prez.variable("myAnswer").join("");

// We attempt to look up the word in the answers array
lookUp(temp);

// Run the restore function to bring back all the letters so another can be submitted
restore();

3 Likes

Hi Greg,
I really don’t know how to thank you for your wonderful work, now I hope to be able to implement it in my project and I will let you know…
You have been really kind and helpful, and I am truly grateful.
Thank you from the bottom of my heart! :pray: :hearts:
Have a wonderful day!

1 Like

Hi Greg,
I was able to implement your code and it works great!
Only now I no longer display the words in the colored boxes after they have been accepted.
I also no longer have the delete button in case the student accidentally clicks on a letter and wants to go back…
and another thing I no longer have is the final feedback in case someone manages to spell all the words…
But somehow I will manage, you have already done way too much for me and I know that time is precious for everyone. :hugs:
Thank you again you are a special person! :pray:

I did not work those things into my sample but I think it can be realized.
Let me see what I can figure out this morning.

Here are some updates.

CANCEL BUTTON
All we have to do here is call our restore function and clear the answer display without performing the lookup function as we do with the submit button.

restore();

var ansDisplay = prez.object("ansDisplay");
    
ansDisplay.text("");

SHOW FOUND WORDS
For this I created boxes with the words and made them initially hidden.
I added a single line of code to reveal the box when the word is first found.
The trick here is in the naming of each of the boxes.
I named them with an f and the word in all caps like so…
fCAROTA
I used the letter f for found and the word in caps because that is how we find it in the array. Then I can concatenate it to form the object name and show it.

prez.object("f"+prez.variable("answers")[i].word).show();

FOUND ALL WORDS
I created a box for the feedback and made it initially hidden. I then created a variable called counter and added a line to the lookUp function to increment the counter when a new word is found.

prez.variable("counter",prez.variable("counter")+1);

Then - every time we submit - we call a new function called checkAll which will check to see if the counter is equal to the length of the answers array. If it is - show the feedback.

window.checkAll = function() {
    if (prez.variable("counter") == prez.variable("answers").length) {
        prez.object("congrats").show();
    }
}

Hope this helps.
Again - I know this isn’t perhaps the direction you originally went so things in my sample may not line up with your own project. I am not sure what your comfort level is with JavaScript either so perhaps I made things more complicated for you. My apologies if that is the case. My hope was that the proof of concept would guide you further along.

1 Like

Hi Greg,
let’s say that my JavaScript level is low low, but I like to look and read codes so that I can always learn more, and I have to say that you would be a perfect teacher, you can explain the concepts very well and you can make them as simple as magic, but I really have a long way to bow down to you, and
I cannot thank you enough for what you are doing for me. :hearts:

Your code works perfectly! I love it!
All the words now appear in the colored boxes, and misspellings disappear when you click the SEND button. Yay!!! :hugs:

Only the final feedback I can’t get it to display.
I put the check.All function in the SEND button, but the final feedback doesn’t appear.
Am I doing something wrong? Should it be entered elsewhere?
I am attaching the code for the SEND key in question.

I’m noticing now that the “counter” variable also counts my errors.
Maybe that is why the feedback does not appear when finished?

Kind words - thank you.

I have the checkAll function placed in the onLoad area
You could perhaps leave that code there but you still need to request that the function be performed by calling it.
For the functions, the code basically just sits there holding the instructions for what to do until we say “Do it!”

// Do it!
checkAll();

Two other thoughts…

  1. Create the counter variable in AP also
    image

  2. Make sure that your feedback box is named congrats

Here is what my submit button looks like with the updates.

var temp = prez.variable("myAnswer").join("");

lookUp(temp);

restore();

checkAll();

And here is what my code looks like for the onLoad of the slide.

prez.variable("answers",[
{word: "CAROTA", points: 10, found: 0},
{word: "AORTA", points: 6, found: 0},
{word: "CART", points: 3, found: 0},
{word: "CAT", points: 1, found: 0}
]);
prez.variable("myAnswer",[]);

window.build = function() {
    var ansDisplay = prez.object("ansDisplay");
    ansDisplay.text(prez.variable("myAnswer").join(""));
};

window.lookUp = function(nameKey) {
    for (var i = 0; i < prez.variable("answers").length; i++) {
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 1) {
            prez.object("feedback").show();
        }
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 0) {
            prez.variable("score",prez.variable("score")+prez.variable("answers")[i].points);
            prez.variable("answers")[i].found = 1;
            prez.object("f"+prez.variable("answers")[i].word).show();
            prez.variable("counter",prez.variable("counter")+1);
        }
    }
};

window.restore = function() {
prez.object("Group Object_5").show();
prez.object("Group Object_8").show();
prez.object("Group Object_11").show();
prez.object("Group Object_14").show();
prez.object("Group Object_17").show();
prez.object("Group Object_20").show();
prez.variable("myAnswer",[]);
}

window.checkAll = function() {
    if (prez.variable("counter") == prez.variable("answers").length) {
        prez.object("congrats").show();
    }
}
1 Like

Hmm… I don’t think I tested that.
I will verify on my side.

1 Like

Ok - I do not see that on my end.

I believe it is due to the fact that you have the counter incrementing on every submit.
I included my counter only on a successful find from within the lookUp function.

Here is just the lookUp function

window.lookUp = function(nameKey) {
    for (var i = 0; i < prez.variable("answers").length; i++) {
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 1) {
            prez.object("feedback").show();
        }
        if (prez.variable("answers")[i].word === nameKey && prez.variable("answers")[i].found == 0) {
            prez.variable("score",prez.variable("score")+prez.variable("answers")[i].points);
            prez.variable("answers")[i].found = 1;
            prez.object("f"+prez.variable("answers")[i].word).show();
            prez.variable("counter",prez.variable("counter")+1);
        }
    }
};

You can remove that line from the submit button and add it to the lookUp function as shown above.

Sorry for the confusion.

1 Like

Yes yes, I had already created the counter variable, as well as named the feedback box with the congrat name.
Only I had added that line of code where I shouldn’t have.
Now the counter is ok, it doesn’t count errors anymore, but the final feedback still doesn’t show up, although I checked the code with yours and it’s all ok.
I will study it again, maybe I still missed something.
Thank you so much Greg!

Yay!!! I did it!!!
There was a duplicate line of code.
WOW WOW!!! :hugs:
I wish you all the best Greg!

2 Likes