Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions editor/static/js/question/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,20 +771,29 @@ $(document).ready(function() {

// Finally, mark duplicate names
names.sort(Numbas.util.sortBy('name'));
var groupArray = [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to include the type of a variable in its name. This could just be called groups.

function handle_group(group) {
group.forEach(function(n) {
n.v.duplicateNameError(group.length > 1 ? n.name : null);
group.forEach(function (n) {
n.v.duplicateNameError(null);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it won't noticeably affect performance, you can avoid having two nested forEach calls by setting n.v.duplicateNameError(null) when you loop over each name, instead of here where you're looping over names inside groups.

});
groupArray.push(group);
}

var start = 0;
names.forEach(function(n,i) {
if(n.name!=names[start].name) {
handle_group(names.slice(start,i));
names.forEach(function (n, i) {
if (n.name != names[start].name) {
handle_group(names.slice(start, i));
start = i;
}
});
handle_group(names.slice(start));
// Triggers duplicateNameError observable for groups with duplicates
groupArray.forEach(function (group) {
var groupVariable = group[0];
if (group.length > 1 && groupVariable.name != "") {
groupVariable.v.duplicateNameError(groupVariable.name);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're only setting duplicateNameError on the first Variable in the group. You should set it for each variable in the group.

Again, groupVariable unnecessarily includes the type of the variable in its name. Just group will do.

}
})
},this).extend({rateLimit: 2000});

/** Create an instance of this question as a Numbas.Question object.
Expand Down Expand Up @@ -1963,10 +1972,10 @@ $(document).ready(function() {

var nameError = ko.pureComputed(function() {
var duplicateNameError = v.duplicateNameError();
var name_errors = names().map(function(nd) {
var name_errors = names().map(function (nd) {
var name = nd.name;
if(name.toLowerCase() == duplicateNameError) {
return 'There\'s another variable or constant with the name '+name+'.';
if (name.toLowerCase() == duplicateNameError) {
return 'There\'s another variable or constant with the name ' + name + '.';
}
if(name=='') {
return '';
Expand Down