SCRIPT1003: Expected ':' in IE 11 only - javascript

My script works perfectly in every browser but ie 11 (of course ie...). Can't figure out what more I can do. JS Lint is passing my script... Says it's missing colon. Here's the entire function. Thanks for any insight at all. Error occurs on line that begins "setcurrentList(list) {" (second to last function).
Edit: updated code now receiving error on last function: getcurrentList()
JQ
generateAllLocationsData = (function() {
var counter = 0;
if (typeof allLocationsData == "undefined") {
allLocationsData = [];
for (var x = 0; x < officesData.children.length; x++) {
for (var y = 0; y < officesData.children[x].departments.length; y++) {
if (officesData.children[x].departments[y].jobs.length > 0) {
for (z = 0; z < officesData.children[x].departments[y].jobs.length; z++) {
counter++;
ids.push(officesData.children[x].departments[y].jobs[z].id);
g_deptHolder[officesData.children[x].departments[y].jobs[z].id] = officesData.children[x].departments[y].name;
}
}
}
}
jobsData = jobsData.sort(function(a, b) {
if (a.title > b.title) {
return 1;
}
if (a.title < b.title) {
return -1
}
return 0;
});
for (var x = 0; x < jobsData.length; x++) {
var dept = g_deptHolder[jobsData[x].id]
if (typeof officesData["All Departments"][dept] == "undefined") {
officesData["All Departments"][dept] = [];
}
officesData["All Departments"][dept].push(jobsData[x]);
}
var sortedObject = [];
Object.keys(officesData["All Departments"]).sort().forEach(function(key) {
sortedObject[key] = officesData["All Departments"][key];
})
officesData.children = officesData.children.sort(function(a, b) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1
}
return 0;
})
officesData["All Departments"] = sortedObject;
}
console.log("sorted", officesData);
return officesData;
});
return {
isLoading: function() {
return (!jobsDataLoading && !officesDataLoaidng) ? false : true;
},
getJobsData: function() {
if (this.isLoading() == false) {
return officesData;
} else {
return false;
}
},
getOfficesData: function() {
if (this.isLoading() == false) {
return officesData;
} else {
return false;
}
},
getAllLocationsData: function() {
return generateAllLocationsData();
},
setcurrentList: function(list) {
this.currentList = list.sort(function(a, b) {
if (a.title < b.title) {
return -1;
}
if (a.title > b.title) {
return 1;
}
return 0;
});
},
getcurrentList(): function(list) {
return this.currentList;
}
}
})()

Your syntax
setcurrentList(list) {
inside an object, is only valid in ES2015, and is what is called a method definition, a shorthand way to declare functions inside object literals
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions
Method definitions are not valid in IE11, it should be
setcurrentList: function(list) {
if you have to support older browsers (or any version of IE)

Related

Problem with generic sort method with Vue.JS

I did a generic method to sort both text and dates from my columns, however it does not filter the year, it is ignoring it considering only the month and day. The following method:
orderBy(header) {
let data = [...this.data];
if (this.orderByProperty === header.property) {
if (this.orderByType === "asc") {
this.orderByType = "desc";
} else {
this.orderByType = "asc";
}
data = data.reverse();
this.$emit("orderBy", data);
return;
}
this.orderByProperty = header.property;
this.orderByType = header.orderBy;
switch (header.property) {
case "active":
data.sort((a, b) => {
let comparison = 0;
if (a[header.property] < b[header.property]) {
comparison = 1;
} else if (a[header.property] > b[header.property]) {
comparison = -1;
}
//it has to return -1, 0 or 1
return comparison;
});
break;
case "name":
data.sort((a, b) => {
return a.name < b.name ? -1 : 1;
});
break;
default:
data.sort((a, b) => {
let valueA;
let valueB;
if (Array.isArray(a[header.property])) {
valueA = a[header.property].sort()[0];
valueB = b[header.property].sort()[0];
} else {
valueA = a[header.property] || "";
valueB = b[header.property] || "";
}
let comparison = 0;
if (isNaN(a[header.property])) {
if (a[header.property] == undefined) {
comparison = 1;
} else if (
a[header.property].length == 10 &&
this.moment(a[header.property], "DD/MM/YYYY").format(
"MM/DD/YYYY"
) >
this.moment(b[header.property], "DD/MM/YYYY").format(
"MM/DD/YYYY"
)
) {
comparison = 1;
} else if (
valueA.toLowerCase() > valueB.toLowerCase() &&
a[header.property].length != 10
) {
comparison = 1;
} else {
comparison = -1;
}
} else if (isDateString(valueA)) {
if (new Date(valueA) < new Date(valueB)) {
comparison = 1;
} else {
comparison = -1;
}
} else {
if (parseInt(a[header.property]) > parseInt(b[header.property])) {
comparison = 1;
} else {
comparison = -1;
}
}
//it has to return -1, 0 or 1
return comparison
})
break
}
this.$emit("orderBy", data)
return
}
I tried to change the ordering but it is ordering only by text.
The header sorts both by date and by text.

No response from recursive function

I want to create a function that is able to determine if a number is same or palindrome. if a given number is palindrome or same then return 2 otherwise if it is not palindrome or same then i need check it twice by increment the given number by 1. after that if it palindrome or same then return 1. if no palindrome or same number found then return 0. i write the function which is giving me the exact result when i give the number as 11211 but the function don't show any response if i enter 1122 or other random value. please help me to find where the error of my function.
function sameOrPalindrome(num) {
var c = 0;
var al = 0;
var normalArray = num.toString().split("");
var revArray = num.toString().split("").reverse();
for (var i = 0; i < normalArray.length; i++) {
if (normalArray[i] != revArray[i]) {
c++;
}
}
if (c == 0) {
return 2;
} else {
num++;
al = sameOrPalindrome(num);
if (al == 2) {
return 1;
} else {
num++;
al = sameOrPalindrome(num);
if (al == 2) {
return 1;
}
}
}
return 0;
}
console.log("1233",sameOrPalindrome(1233))
here is my solution to this problem:
function reversedNum(num) {
return (
parseFloat(
num
.toString()
.split('')
.reverse()
.join('')
) * Math.sign(num)
)
}
function sameOrPalindrome(num) {
if (num === reversedNum(num)) {
return 2;
} else {
num++;
if (num === reversedNum(num)) {
return 1;
} else {
num++;
if (num === reversedNum(num)) {
return 1;
}
}
}
return 0;
}
console.log("1233",sameOrPalindrome(1233))
Perhaps not using recurse - I think your function loops
const allEqual = arr => arr.every( v => v === arr[0] )
const sameOrPalin = num => {
const str = String(num);
let arr = str.split("")
if (allEqual(arr)) return 2
arr.reverse();
if (arr.join("") === str) return 1;
return 0
};
console.log("1111",sameOrPalin(1111));
console.log("2111",sameOrPalin(2111));
console.log("2112",sameOrPalin(2112));
console.log("1234",sameOrPalin(1234));
for (let i = 2111; i<=2113; i++) console.log(i,sameOrPalin(i));
Question: I assumed if palindrome test is true at first time then return 2. if not try incrementing by one and test the palindrome again . if true return 1 else try incrementing for last time and check the palindrome if true return 1 else 0.
Store string into array first and do arr.reverse().join("") to compare
let arr=num.toString().split("");
if(num.toString() == arr.reverse().join(""))
function sameOrPalindrome(num, times) {
let arr = num.toString().split("");
if (num.toString() == arr.reverse().join("")) {
if (times == 3) return 2
else return 1;
} else if (times > 0) {
num++; times--;
return sameOrPalindrome(num, times);
} else return 0
}
console.log(sameOrPalindrome(123321, 3));
console.log(sameOrPalindrome(223321, 3));
console.log(sameOrPalindrome(323321, 3));
Your function needs to know if it should not call itself any more, e.g. when it's doing the second and third checks:
function sameOrPalindrome(num,stop) { // <-- added "stop"
var c = 0;
var al = 0;
var normalArray = num.toString().split("");
var revArray = num.toString().split("").reverse();
for (var i = 0; i < normalArray.length; i++) {
if (normalArray[i] != revArray[i]) {
c++;
}
}
if (c == 0) {
return 2;
} else if(!stop) { // <-- check of "stop"
num++;
al = sameOrPalindrome(num,true); // <-- passing true here
if (al == 2) {
return 1;
} else {
num++;
al = sameOrPalindrome(num,true); // <-- and also here
if (al == 2) {
return 1;
}
}
}
return 0;
}
for(let i=8225;i<8230;i++)
console.log(i,sameOrPalindrome(i));
function check_palindrom(num){
var c1 = 0;
var normalArray = num.toString().split("");
var revArray = num.toString().split("").reverse();
for (var i = 0; i < normalArray.length; i++) {
if (normalArray[i] == revArray[i]) {
c1++;
}
}
if(c1==0){
return 2;
}else{
return 1;
}
}//check_palindrom
function my_fun_check_palindrome(mynum){
//console.log(mynum);
var num = mynum;
var c2 = 0;
var al = 0;
var normalArray = mynum.toString().split("");
var revArray = mynum.toString().split("").reverse();
for (var j = 0; j < normalArray.length; j++) {
if (normalArray[j] == revArray[j]) {
c2++;
}
}
if(c2==0){
console.log('Number is palindrome. Return Value :'+ 2);
}
if(1){
console.log('checking again with incremeting value my one');
num = parseInt(num)+1;
al = check_palindrom(num);
if(al==2){
console.log('Number is palindrome. Return Value :'+ 1);
}else{
console.log('Number is not palindrome. Return Value :'+ 0);
}
}
}//my_fun_check_palindrome
console.log(my_fun_check_palindrome(1122));
console.log(my_fun_check_palindrome(11221));
We should always strive to make function more effiecient... you dont need to run full loop. plus actual checking of palindrome can me modularized
function isSameOrPalindrome(num) {
var normalArray = num.toString().split("");
var revArray = num.toString().split("").reverse(),
i;
for (i = 0; i < normalArray.length / 2; i++) {
if (normalArray[i] !== revArray[i]) {
break;
}
}
if (i >= normalArray.length/2) {
return "Palindrome";
} else {
return "Not Palindrome";
}
}
function doCheck(num) {
var isPalindrome = isSameOrPalindrome(num);
console.log(isPalindrome);
if(isPalindrome === "Palindrome") {
return 2;
} else {
num++;
isPalindrome = isSameOrPalindrome(num);
if(isPalindrome === "Palindrome") {
return 1;
} else {
return 0
}
}
}
console.log("100",doCheck(100));

JavaScript Recursive function return undefined instead of an array

I have the next function:
function solveSudoku(prev_tab, fila, columna) {
let tab = _.cloneDeep(prev_tab);
let sig_fila = fila;
let sig_col = columna;
if (fila === 8 && columna === 8) {
//console.log(tab);
return tab;
}
if (columna === 8) {
sig_col = 0;
sig_fila = sig_fila + 1
} else {
sig_col = sig_col + 1;
}
if ((tab[fila][columna]) !== '') {
solveSudoku(tab, sig_fila, sig_col)
} else {
for (let num = 1; num <= 9; num++) {
if (numeroValido(tab, num, fila, columna)) {
tab[fila][columna] = num;
//tab.toString();
solveSudoku(tab, sig_fila, sig_col)
}
}
}
}
it returns undefined instead of a 2D array, i already try to add return in every recursive call =>
return solveSudoku( tab, sig_fila, sig_col )
but now that doesn't work either
I'm not really familiar with algorithms for solving sudoku, so I don't know if the algorithm below is correct.
But you need to ensure that the result of the recursion is returned. In my update below, I return the first recursive call. In the loop, I only return it if the recursion successfully found a solution, otherwise the loop continues trying other numbers in the column.
function solveSudoku(prev_tab, fila, columna) {
let tab = _.cloneDeep(prev_tab);
let sig_fila = fila;
let sig_col = columna;
if (fila === 8 && columna === 8) {
//console.log(tab);
return tab;
}
if (columna === 8) {
sig_col = 0;
sig_fila = sig_fila + 1
} else {
sig_col = sig_col + 1;
}
if ((tab[fila][columna]) !== '') {
return solveSudoku(tab, sig_fila, sig_col)
} else {
for (let num = 1; num <= 9; num++) {
if (numeroValido(tab, num, fila, columna)) {
tab[fila][columna] = num;
//tab.toString();
let result = solveSudoku(tab, sig_fila, sig_col);
if (result) { // continue searching if the recursion failed
return result;
}
}
}
}
}

What the meaning of return [''] in JavaScript?

Please see this leetcode solution. In the function it returns [''] which actually return an array of answer. Could someone tell me what's going on there?
[The problem is solved. Actually it will return in the middle of the code.]
https://leetcode.com/problems/remove-invalid-parentheses/discuss/154272/JavaScript-BFS-solution
function removeInvalidParentheses(s) {
let queue = new Set([s]);
while (queue.size) {
const next = new Set();
for (let v of queue) {
if (isValid(v)) {
return [...queue].filter(isValid);
}
for (let i = 0; i < v.length; i++) {
next.add(v.slice(0, i) + v.slice(i+1));
}
}
queue = next;
}
return [''];
}
function isValid(str) {
let bal = 0;
for (let ch of str) {
if (ch === '(') {
bal++;
} else if (ch === ')') {
bal--;
}
if (bal < 0) {
return false;
}
}
return bal === 0;
}
The function returns an array with a single empty string if the prior code (line 7) does not return a result. It is simply a default value so that calling code sees some result from the method.
function removeInvalidParentheses(s) {
let queue = new Set([s]);
while (queue.size) {
const next = new Set();
for (let v of queue) {
if (isValid(v)) {
return [...queue].filter(isValid);
}
for (let i = 0; i < v.length; i++) {
next.add(v.slice(0, i) + v.slice(i+1));
}
}
queue = next;
}
return [''];
}
function isValid(str) {
let bal = 0;
for (let ch of str) {
if (ch === '(') {
bal++;
} else if (ch === ')') {
bal--;
}
if (bal < 0) {
return false;
}
}
return bal === 0;
}

called function refuses to return true

Even when sum === largest in subset(), I don't get a "true" in my console. The true is sent back only to the calling function? Then how do I get the "return true" to behave as a newbie would expect?
function ArrayAdditionI(arr)
{
arr.sort();
var largest = arr.pop()
subset([], arr, largest);
}
function subset(soFar, rest, largest)
{
var sum = 0;
if (rest.length === 0)
{
for(var i=0; i<soFar.length; i++)
{
sum+= soFar[i];
}
if (sum === largest)
{
return true;
}
}
else
{
var soFar2 = soFar.slice(0);
soFar2.push(rest[0]);
subset(soFar,rest.slice(1),largest);
subset(soFar2, rest.slice(1),largest);
}
}
ArrayAdditionI([85,3,88,2])
function ArrayAdditionI(arr)
{
arr.sort();
var largest = arr.pop()
var ret = subset([], arr, largest);
// do something with ret
}
function subset(soFar, rest, largest)
{
var sum = 0;
if (rest.length === 0)
{
for(var i=0; i<soFar.length; i++)
{
sum+= soFar[i];
}
if (sum === largest)
{
return true;
}
}
else
{
var soFar2 = soFar.slice(0);
soFar2.push(rest[0]);
subset(soFar,rest.slice(1),largest);
subset(soFar2, rest.slice(1),largest);
}
return false;
}
ArrayAdditionI([85,3,88,2])

Categories

Resources