0
\$\begingroup\$

I have one question list (q_arr) and the user can either skip or attempt that question. In the end of last question, I display all question with their selected answer and "N/A" if not attempted.

Attempted question list is r_arr, where q property is the question number from q_arr list (q:1 means the first element of q_arr).

    (function() {
    
    let r_arr = [{q:1, a:true},{q:3, a:false}];
    let q_arr = [
                { q: 'one', o: [true, false] },
                { q: 'two', o: [true, false] },
                { q: 'three', o: [true, false] },
                { q: 'four', o: [true, false] }
            ];
    
    
    //desired output: [{q:"one", a: true}, {q:"two", a: "N/A"}, {q:"three", a: false}, {q:"four", a:"N/A"}]
    
    
    let op = q_arr.filter((s, idx, arr) => {
        // console.log("s", s, idx, arr);
        let id = idx + 1;
        r_arr.map((f) => {
            if (id === f.q) {
                f.q = s.q;
            }
        });
    });
    
    
    console.log("r_arr", r_arr); 
    })();

Now I again need to filter with q_arr and change to get not attempted question list and merge with this new r_arr and this seems a lengthy way.

Kindly suggest a way to solve this problem in fewer lines in ES6.

\$\endgroup\$
3
  • \$\begingroup\$ Placeholders like // here it run the loops for the 12 times or more, based on q_arr make the code difficult to review. A fully working demonstration with your real code would be preferable. (Press Ctrl-M in the question editor to make a demo.) \$\endgroup\$ Commented Nov 24, 2017 at 20:37
  • \$\begingroup\$ @200_success AFAIK this empty else branch with the comment is (was) just for documentation purposes and not a placeholder. \$\endgroup\$
    – le_m
    Commented Nov 24, 2017 at 20:53
  • 1
    \$\begingroup\$ Regarding your variable names: Instead of q_arr = [{q: ..., o: ...}] I suggest more descriptive names which highlight the role instead of the datatype such as questions = [{text: ..., choices: ...}]. \$\endgroup\$
    – le_m
    Commented Nov 24, 2017 at 21:07

1 Answer 1

1
\$\begingroup\$

If you index the answers to the index of the questions you can do this:

let r_arr = {
            0: true,
            3: false
};
let q_arr = [
            { q: 'some question one', o: [true, false] },
            { q: 'some question two', o: [true, false] },
            { q: 'some question three', o: [true, false] },
            { q: 'some question four', o: [true, false] }
        ];

var a = q_arr.map((q, i) => ({'q': q.q, 'o': i in r_arr ? r_arr[i]  : 'na'}) )
console.log(a)

You'll need to account for the fact that js arrays are zero indexed.

\$\endgroup\$
7
  • \$\begingroup\$ Thank you very much. I am not clear on second thing, what you suggested that I need to re-write q_arr = [[ true, false], [true,false]...] then where do I define the question text? \$\endgroup\$
    – xkeshav
    Commented Nov 24, 2017 at 20:54
  • \$\begingroup\$ I'm sorry, I didn't put it together that that was the text. I thought you were just indexing the questions. Let me post an edit. \$\endgroup\$
    – Mark M
    Commented Nov 24, 2017 at 20:55
  • \$\begingroup\$ ok. But you have suggested the next stage when I get the new r_arr. I am looking to change the way I get the new r_arr or just map with old r_arr and q_arr \$\endgroup\$
    – xkeshav
    Commented Nov 24, 2017 at 20:58
  • \$\begingroup\$ If i store all skipped question that will make unnecessary request to store the data in db and page will not get next question till it is not stored, lthough it took very less seconds but why we burden un necessarily to db. I am using html local storage \$\endgroup\$
    – xkeshav
    Commented Nov 24, 2017 at 20:59
  • 1
    \$\begingroup\$ Instead of r_arr = {0: true, 3: false}; I'd suggest r_arr = {0: 0, 3: 1}; where the property value is the index of the answer. Of course for boolean answer choices this doesn't matter, but for more complicate choices, e.g. text fields, this might be a cleaner design. \$\endgroup\$
    – le_m
    Commented Nov 24, 2017 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.