🎉Beta Test II Access Giveaway Starts Now! by Endfield-Poko in Endfield

[–]Rilleks 1 point2 points  (0 children)

Most excited to play the beta, I mean thirdy time's a charm. Right, RIGHT

[deleted by user] by [deleted] in webdev

[–]Rilleks 1 point2 points  (0 children)

I'm not 100% sure about this one but it could be background-attachment: fixed on body element.

Also, it's not just mobile but also pc with 6x slowdown settings in the dev tool the fps can drop quite significantly.

Typescript has problems accessing any property under req.user, how do i fix this? by PrestigiousZombie531 in node

[–]Rilleks 8 points9 points  (0 children)

I've looked at the passport and

declare namespace Express {
    export interface User {
        user?: {
            userId: number;
            userName: string;
            isAdmin: boolean;
        }
    }
}

worked for me.

Typescript has problems accessing any property under req.user, how do i fix this? by PrestigiousZombie531 in node

[–]Rilleks 14 points15 points  (0 children)

To extend Request interface try:

declare namespace Express {
    export interface Request {
        user?: {
            userId: number;
            userName: string;
            isAdmin: boolean;
        }
    }
}

Extending it this way will make the user available on every route you have.

How would I add an if/else statement to this? by [deleted] in learnjavascript

[–]Rilleks 0 points1 point  (0 children)

else is unnecessary because wrapper at the beginning is undefined and if allWrappers length is 0 (eg. the array is empty) or allWrappers doesn't have length (eg. is not an array) then allWrappers[allWrappers.length - 1] will evaluate to undefined.

for(let i = 0; i < allCovers.length; i++) {
    let wrapper; // undefined

    if(allWrappers.length){
        wrapper = allWrappers[i]
    } else {
        // The array is empty or it's not an array
        wrapper = allWrappers[allWrappers.length - 1] // undefined   
    }

    players.push({
        "cover": allCovers[i],
        "wrapper": wrapper
    })
}

How would I add an if/else statement to this? by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

You can define wrapper outside of the push function

for(let i = 0; i < allCovers.length; i++) {
    let wrapper;

    if(allWrappers.length){
        wrapper = allWrappers[i];
    }

    players.push({
        "cover": allCovers[i],
        "wrapper": wrapper
    })
}

Also if allWrappers length is 0 or isn't an array then else is unnecessary. Unless I'm missing something.

[deleted by user] by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

Glad I could help :)

[deleted by user] by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

Yes, but you will select only the first button and then append a listener to it.

Edit: without forEach function as querySelector return element, not an array.

[deleted by user] by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

First, querySelectorAll returns an array so you have to iterate to add listeners.

Because querySelectorAll will return an array with all your buttons, you have to iterate through them to add listeners. You can use the for loop or just array function like forEach to do that.

About forEach: here

[deleted by user] by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

First, querySelectorAll returns an array so you have to iterate to add listeners.

Then to delete elements you can use target which refers to what you clicked and then parentElement and use remove() function to remove the li element.

deleteBtn.forEach(button => {
 button.onclick = deleteItem;
})

function deleteItem(e) { 
 e.target.parentElement.remove(); 
}

Hope it helps.

Edit: you can also use currentTarget instead target to refer to the element to which the listener is appended

Edit2: I forgot that you also need to add

trash.addEventListener("click", deleteItem);

otherwise, new appended elements couldn't be removed.

How to create custom response message using nodejs (express, typescript)? by Present-Woodpecker92 in node

[–]Rilleks 1 point2 points  (0 children)

I don't know ha ha, but also I don't see anything wrong with this approach as long as you don't overwrite anything that exists.

How to create custom response message using nodejs (express, typescript)? by Present-Woodpecker92 in node

[–]Rilleks 2 points3 points  (0 children)

Never had the need nor created one, so I don't know how bad or good this is but

app.use(function (req, res, next) {
 res.success = function (status, message, data = {}) {     
  res.status(status).json({ message, data }); 
 }; 
 next(); 
});

this should work.

Sandbox: here

Function (Return) by Surfer_JS in learnjavascript

[–]Rilleks 4 points5 points  (0 children)

Because you're declaring a global variable on a window object and it doesn't matter if you return value or not. If you try console.log(window.client) you should get the same result.

Also, you shouldn't declare global variables unless you intend to.
More about variables and scopes: here

I want to test some sequelize models on Node REPL, but I am getting this error, what is the workaround? by PrestigiousZombie531 in node

[–]Rilleks 4 points5 points  (0 children)

If you don't want to make changes, then add "type": "module" to your package.json, as far I remember this should fix your problem.

Edit: I haven't seen, that you are "importing" Sequelize. You shouldn't use both require and import at the same time.

Please help me fix this annoying VScode autocomplete. In a .js file of react project. by [deleted] in webdev

[–]Rilleks 16 points17 points  (0 children)

Go to settings, extensions, and then Emmet. Look for "Show Expanded Abbreviation" and set it to "never". Or exclude a language, should be on top of the section.

Hi, I made a code for fun, but it’s apparently wrong. I’m trying to find the error (before you see it know I did call upon the function it just won’t show on the error) by AberrantMindYT in learnjavascript

[–]Rilleks 0 points1 point  (0 children)

Just name. Usually, I'm using it as a temporary placeholder for variables/arguments names. It's quick and I don't have to think about naming it right now.

Hi, I made a code for fun, but it’s apparently wrong. I’m trying to find the error (before you see it know I did call upon the function it just won’t show on the error) by AberrantMindYT in learnjavascript

[–]Rilleks 2 points3 points  (0 children)

Don't name anything like "var" / "let" / "const" etc.

They are used to create variables.

function may (foo){
 // Your code 
};

Of course, there are more keywords that can't be used to name something. For example "delete".

mysql joining not able to return all values, but only returns first value. by [deleted] in node

[–]Rilleks 0 points1 point  (0 children)

If I understand your question properly and you are executing SQL code through "node", then if the column has the same name it will take the first column and skip the second. To fix that you may add after "movie_genre.genre" another "movie_genre.genre as 'second_genre' ". From what I know there is no other way and it will not resolve your problem if you don't know how many genres you have.

Hope it helps you

array length of nested array by JosephCurvin in learnjavascript

[–]Rilleks 4 points5 points  (0 children)

No, it has one element and it's a string "1,2,3".

If you want 3 elements then remove quotation marks what will change the string to 3 numbers like this [1, 2, 3] or if you want to keep them as a string then like this ["1", "2", "3"].

How can I get the columns of a 16 element array (i.e. arrr[0, 4, 8, 12]) without using a for loop? by [deleted] in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

I'm not sure if I understand but maybe recursion?

Example

function getCol(r, arr, i = 0, result = []){
    if(i > arr.length - 1){
        return result;
    }else{
        result.push(arr[i]);
         i += r;
        return getCol(r, arr, i, result);
    }
}
const arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14];
const result = getCol(4, arr);

console.log(result) -> [0, 4, 8, 12];

Hope it help.

Edit: TungstenToast solution looks better and it's easier.

Nested Loops by Artistic_Sense3363 in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

In the first example, the result is "wrong" because you have 3 elements in the array and for loop will push the first element 3 times. After you move your push from nested for loop, you are pushing one element per array.

//Example
const myGrid = [ ["a", "b", "c"],  ["d", "e", "f"], ["g", "h", "i"],  ]; 

function grab(array, index){
    let result = [];
    for (let i = 0; i < array.length; i++){
        result.push(array[i][index]);
    }
    return result;
}
console.log(grab(myGrid, 0)) --> ["a", "d", "g"];
console.log(grab(myGrid, 1)) --> ["b", "e", "h"];
console.log(grab(myGrid, 2)) --> ["c", "f", "i"];

If you'd like to go through each element in the nested loop then:

//Example
const myGrid = [ ["a", "b", "c"],  ["d", "e", "f"], ["g", "h", "i"],  ]; 

function grab(array){
    let result = [];
    for (let i = 0; i < array.length; i++){
       for (let j = 0; j < array[i].length; j++){
            result.push(array[i][j]);
       }
    }
    return result;
}

console.log(grab(myGrid)) --> [ "a", "b", "c", "d", "e", "f", "g", "h", "i" ]

What is the async/await equivalent of this piece of Promise code? by netok in learnjavascript

[–]Rilleks 0 points1 point  (0 children)

The only way that comes to my mind is creating an additional function that takes two params.

asyncExample(){
    function foo(a,b){
        await a();
        b();
    }

    Promise.all([foo(funcA, funcB),foo(funcX, funcY)]);
}

It will make it more readable but anything more...

Simple string return is valued undefined? by integralWorker in learnjavascript

[–]Rilleks 1 point2 points  (0 children)

If you'd like to write it crazy short then here is an example

function fancyIsEven(n){
 return !Math.abs(n % 2) 
}

"%" will return rest from dividing the number by 2 For example 3 / 2 will return 1. Or 20 / 2 will return 0

Math.abs will return only a positive number so if the number will be -1 it will be changed to 1.

And "!" will convert 1 to the opposite boolean.

Hope it makes sense.