• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

javascript code - using cpExtra local variables - challenge

Explorer ,
May 03, 2020 May 03, 2020

Copy link to clipboard

Copied

Hi there:

 

I need some guidance about using local storage variables to solve a problem. (I am using cpExtra's local storage capability, meaning that any variable name I pre-pend with LS goes into local stoarage.)

 

I want to add data to a particular LS var for several days, (or for five projects), and then on the fifth day, use the accumulated values as part of the data to be displayed to the learner.

 

More details:

In a series I'm developing, where one course (cp project) is delivered each day, a learner identifies between 0 and 5 words they want to review. They perform that review and are done with that section. I want to store the 0-5 words in an LS var, which is straightforward.

On day 2 (Tuesday), the learner gets a new set of words to consider, and can once again, identify between 0 and 5 words for further review. This process continues each day of the week, meaning that that learner will have identified between 0 and 25 words for review.

Day 5, Friday, is different - it's Word Review Day from the rest of the week. The learner will, as before, identify between 0 and 5 words for review. If the number of words for review is less than 5, then more words will be added from the accumulated list of words from Mon - Thur.

 

For example:
Day - Words to Review
1 - dog, cat
2 - car, boat
3 - tree, flower
4 - apple, orange
5 - fan, light

 

The eight words from Days 1-4 should be available in a variable on Day 5:
dog, cat, car, boat, tree, flower, apple, orange

 

Since the learner identified 2 words to review on Day 5, then 3 words will be added from these 8 so that there will be a total of 5 words to review.

My problem is that I have not been able to accumulate the values during the week, I think because I have some sort of circular logic.

Consider this partial code:

 

//This array comes from concat result and is used to prime new array
let revwords3 = window.cpAPIInterface.getVariableValue("LS_array");

//Concatenate 
let revwords2 = revwords3.concat(revwords);   //NOTE - assume revwords contains the words the learner wants to review

//Get list from LS
let revwords_list = revwords2.join(", ");

//Now assign revwords_list to LS_array
window.cpAPIInterface.setVariableValue("LS_array", revwords2); //NOTE - using LS_array here and in the first statement may be a problem

//Now assign revwords2 to LS_list
window.cpAPIInterface.setVariableValue("LS_list", revwords_list);

 


Both LS_array and LS_list have the correct values after the code is run. The problem is that on a subsequent run through the code, where I identify more words, I only get back the current words, not the ones from before.

 

My expectation, using the example above, is that revwords2 would contain these values:

 

Day - Words to Review
1 - dog, cat
2 - dog, cat, car, boat
3 - dog, cat, car, boat, tree, flower
4 - dog, cat, car, boat, tree, flower, apple, orange

 

Here's some js code that does mostly what I want, without the cP API:

 

//This array comes from concat result and is used to prime new array
let LS_revwords = ['monkey','cat','horse'];

//This array comes from learner's selection of words to review
let revwords = ['hose', 'bucket', 'truck'];

//Concatenate 
let revwords2 = LS_revwords.concat(revwords);

//Get list from LS
let revwords_list = revwords2.join(", ");

//let LS_revwords = revwords2;

//Now assign revwords_list to LS_array
//Now assign revwords2 to LS_list

console.log(LS_revwords);
console.log(revwords);
console.log(revwords2);
console.log(revwords_list);

['monkey', 'cat', 'horse' ]
[ 'hose', 'bucket', 'truck' ]
[ 'monkey', 'cat', 'horse', 'hose', 'bucket', 'truck' ]
monkey, cat, horse, hose, bucket, truck

 

Any advice would be most appreciated.

 

Thanks

TOPICS
Advanced

Views

250

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation
Resources
Help resources