Check null value in a quiz - vue.js

I did a quiz with vue.js and i would like to check if a value is null before put it in the console.log. I Can't find how to do that...
I have a lot of questions to put in this quiz but some of them just need to be ignored in the final result. I just want to bypass all the questions with value:null.
"use strict";
window.onload = function() {
var quiz = {
title: 'Quizz',
questions: [{
text: "Question 1",
responses: [{
text: 'a',
value: null,
},
{
text: 'b',
value: null,
}
]
},
{
text: "Question 2",
responses: [{
text: 'a',
value: '1',
},
{
text: 'b',
value: '2',
}
]
}
]
};
var app = new Vue({
el: '#app',
data: {
quiz: quiz,
questionIndex: 0,
userResponses: Array(),
show: true
},
methods: {
// Go to next question
next: function() {
console.log(this.userResponses);
this.questionIndex++;
},
// Go to previous question
prev: function() {
this.questionIndex--;
},
score: function() {
//find the highest occurence in responses
var userResponses = Array.prototype.concat.apply([], this.userResponses);
var modeMap = {};
var maxEl = userResponses,
maxCount = 1;
for (var i = 0; i < userResponses.length; i++) {
var el = userResponses[i];
if (modeMap[el] == null)
modeMap[el] = 1;
else
modeMap[el]++;
if (modeMap[el] > maxCount) {
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
}
}
});
}

I find it, just add : var userResponses = this.userResponses.filter(Boolean);

Related

Using Custom Sort with Track Scores set to True is still showing score as null

So I'm setting a default query in my React Native app. Essentially I'm trying to set a sortOrder based on the elementOrder values. My partner used this same piece of code in his web app and it works for him. It doesn't seem to work on my end. The score exists if I remove the custom sort, which is normal due to what I've read in the docs. When I'm using a custom sort, then I should add track_scores: true. My score is still coming up as null.
I am not sure how to debug this situation. Can someone point me in the right direction? Thanks! Here's my code and let me know if you need to see anything. Unfortunately I don't have access to Kibana. I'm just console logging the list item and it's properties.
const defaultQueryConfig = {
track_scores: true,
sort: {
_script: {
type: 'number',
script: {
lang: 'painless',
source: `
int sortOrder = 0;
if (doc['elementOrder'].value == 1) {sortOrder = 3}
else if (doc['elementOrder'].value == 3) {sortOrder = 2}
else if (doc['elementOrder'].value == 2) {sortOrder = 1}
sortOrder;
`,
},
order: 'desc',
},
},
query: {
function_score: {
query: {
match_all: {},
},
functions: [
{
filter: {
match: {
categoryType: 'earth',
},
},
weight: 100,
},
{
filter: {
match: {
categoryType: 'water',
},
},
weight: 90,
},
{
filter: {
match: {
categoryType: 'fire',
},
},
weight: 80,
},
{
filter: {
match: {
thingExists: false,
},
},
weight: 2,
},
],
score_mode: 'multiply',
},
},
};

Is it possible to autoselect and immediately jump to a certain value in an ion-picker?

I'm currently working on an ionic-app that uses ion-pickers, now here's my question:
If you have a certain value that is part of the possibly choosable values of the picker, can you jump to it and select it by default when you try to open the picker just like they did with the datetime-picker?
Because I have a lot of values for some of my pickers, it would be much easier and user-friendly, if it would just immediately jump to the earlier selected value or to a default value somewhere in the middle so that they don't have to scroll down the entire pickervalues-list if they misclicked or something. Now I know that this very nice feature is already implemented for the datetime-picker but I didn't find a possibility to use it on the ion-picker.
So does it exist for the ion-picker yet or is that part yet under development?
I found the answer myself after digging around in the forums (here's the link: https://forum.ionicframework.com/t/solved-how-to-preselect-ion-picker-items/163425)
Basically you only need to use:
picker.columns[0].selectedIndex = index;
An alternative is to set the default ion-picker's option on itself build as shown below:
const objOptions: PickerOptions = {
buttons: [
{
text: 'Cancel',
role: 'cancel'
},
{
text: 'Confirm',
handler: (value) => {
console.log(`Got Value ${value}`);
}
}
],
columns: [{
name: 'Animals',
options: [
{ text: 'Dog', value: 'Dog' },
{ text: 'Cat', value: 'Cat'}
],
selectedIndex: 0
}]
};
const objPicker = await pickerController.create(objOptions);
objPicker.present();
if you want to select by text or value
let columns: PickerColumn[] = [
{
name: 'Animals',
options: [
{ text: 'Dog text', value: 'Dog' },
{ text: 'Cat text', value: 'Cat'}
],
},
];
//autoselect by value
let index = columns[0].options.findIndex(x => x.value === 'Cat');
columns[0].selectedIndex = index > -1 ? index : 0;
//autoselect by text
index = columns[0].options.findIndex(x => x.text === 'Cat text');
columns[0].selectedIndex = index > -1 ? index : 0;
//autoselect by index
columns[0].selectedIndex = 1;
const objOptions: PickerOptions = {
buttons: [
{
text: 'Cancel',
role: 'cancel'
},
{
text: 'Confirm',
handler: (value) => {
console.log(`Got Value ${value}`);
}
}
],
columns: columns
};
const objPicker = await pickerController.create(objOptions);
objPicker.present();

Lodash: What's the opposite of `_uniq()`?

The _.uniq() in lodash removes duplicates from an array:
var tst = [
{ "topicId":1,"subTopicId":1,"topicName":"a","subTopicName1":"w" },
{ "topicId":2,"subTopicId":2,"topicName":"b","subTopicName2":"x" },
{ "topicId":3,"subTopicId":3,"topicName":"c","subTopicName3":"y" },
{ "topicId":1,"subTopicId":4,"topicName":"c","subTopicName4":"z" }]
var t = _.uniq(tst, 'topicName')
This returns:
[ {"topicId":1,"subTopicId":1,"topicName":"a","subTopicName1":"w" },
{ topicId: 2, subTopicId: 2, topicName: 'b', subTopicName2: 'x' },
{ topicId: 3, subTopicId: 3, topicName: 'c', subTopicName3: 'y' } ]
What's the opposite of this? It should only return a single object for each duplicate object:
[ { topicId: 3, subTopicId: 3, topicName: 'c', subTopicName3: 'y' } ]
I don't think there's a built in method, here's something that should do the job:
function dupesOnly(arr, field) {
var seen = {},
ret = [];
arr.forEach(function(item) {
var key = item[field],
val = seen[key];
if (!val) {
seen[key] = val = {
initial: item,
count: 0
}
}
if (val.count === 1) {
ret.push(val.initial);
}
++val.count;
});
return ret;
}

Creating sorted tree in DOJO 1.6?

I new to learn dojo and trying to learn by it using samples code.
Using dojo 1.6
With help of sample codes , I created a tree
now i want to apply sorting on root and also on child.
With the help of this sample code , i changed the code
Output is not sorted n but the root folder has changed their position and child is deleted.
Plz help me to resolve this.
My code :
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.tree.ForestStoreModel");
dojo.require("dijit.Tree");
var data = [ { id: 1, name: "answerTypeLabel", type:'scenario', children:[{_reference: 2}]},
{ id: 2, name: "acceptRequestLabel", type:'paragraph', data: "acceptRequestLabel"},
{ id: 3, name: "rejectRequestLabel", type:'scenario', children:[{_reference: 5},{_reference: 6}]},
{ id: 4, name: "MoreInformationLabel", type:'scenario', children:[{_reference: 7},{_reference: 8}]},
{ id: 5, name: "rejectRequestStatusLabel", type:'paragraph', data: "rejectRequestStatusLabel"},
{ id: 6, name: "rejectRequestNotCoveredLabel", type:'paragraph', data: "rejectRequestNotCoveredLabel" },
{ id: 7, name: "MoreInformationDocumentLabel", type:'paragraph', data: "MoreInformationDocumentLabel"},
{ id: 8, name: "MoreInformationDataLabel", type:'paragraph', data: "MoreInformationDataLabel"}
];
dojo.addOnLoad(function() {
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'id',
label: 'name',
items: data
}
});
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'id',
items: [],
label: 'name'
}
}) // blank itemsstore
})
var tree = new dijit.Tree({
model: model,
updateItems: function(items) {
var self = this;
console.log('pre', this.model.root.children);
dojo.forEach(items, function(newItem) {
console.log('add', newItem);
try {
self.model.store.newItem({
id: sortableStore.getValue(newItem, 'id'),
name: sortableStore.getValue(newItem, 'name'),
type: sortableStore.getValue(newItem, 'type'),
data: sortableStore.getValue(newItem, 'data'),
});
} catch (e) {
console.log(e);
}
});
console.log('post', this.model.root.children);
console.log("children: ", this.rootNode.getChildren());
},
});
tree.placeAt(dojo.body());
sortableStore.fetch({
query: {
type:'scenario'
},
sort: [{
attribute: "name"}],
onComplete: function(items) {
console.log(items, 'sorted');
tree.updateItems(items);
}
})
});
Output :
The 'Names' origins from you setting 'rootLabel'.
Btw, fiddles have revisions and is simply a paste-bin like feature :)
You need to use the tree model pasteItem to insert referenced items (the 'children' property of each 'newItem').
Otherwise, there's another approach, if you get rid of the '_reference' structure of your data. See: http://jsfiddle.net/GHFdA/1/

Find object in array of objects with same id

I have created classes
dojo.declare("UNIT",null,{
_id:'',
constructor:function(i){
this._id=i;
});
and
dojo.declare("ELEMENT", null, {
_id:'',
_unit_id:'',
constructor:function(u,i){
this._unit_id=u;
this._id=i;
});
I have array of Units and I want find one which have id like my element._unit_id. Hot to do this with Dojo ? I was looking in documentation examples but there is dojo.filter by I cannot pass argument . Can anybody help ?
You can use dojo.filter.E.g:
var units = [{
id: 1,
name: "aaaa"
},
{
id: 2,
name: "bbbb"
},
{
id: "2",
name: "cccc"
},
{
id: "3",
name: "dddd"
}];
var currentElementId = 2;
var filteredArr = dojo.filter(units, function(item) {
return item.id==currentElementId;
});
// do something with filtered array
}
Test page for you