Multiple selection in single Dropdown - javascript

I found this code on reddit that works quite well. Anyway, when I try to delete the information that I completed with the dropdown, or modify something, an undefined value shows up and I can not delete it. How could I do that?
Code:
function onEdit(e){
if(e.range.columnStart != 6 || e.oldValue == undefined) return;
e.range.setValue(e.oldValue+" | "+e.value);
}
Besides, when more than 1 option is added on my cell, an 'invalid' message shows up. How could I delete it? It's a default config from the Data Validation.

So far your code snippet works on mine properly. (I can't seem to replicate your issue)
But for you, it isn't (that I am not sure). Try to include the condition where e.value is null (that happens when you delete the cell value). Your code should look like this.
Code:
function onEdit(e){
if(e.range.columnStart != 6 || e.oldValue == null || e.value == null) return;
e.range.setValue(e.oldValue+" | "+e.value);
}
Note:
Try to log the values before comparing them in a condition. It's true that in a == sense, null and undefined are the same as both of them return FALSE, but they have different types. If you have used ===, then they are not equal.
If the cell is deleted (as you can see on the image below), both oldValue and value becomes null.
Cloud logs (blank -> yes -> no -> delete cell):

Related

How to verify not null and skip row if it is?

I am trying to compare 2 dates to be able to display the data in a table.
I am doing the following:
${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()}
The problem is, CreateDate has a null value on some rows so the above is not working.
I have also tried this:
<div ng-if="${row.CreateDate!==null} && ${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()}"
I need to check first if the row is null or not, but even when it is null, it is continuing after the && and when it hits the dateToCompare.getTime, it is throwing a syntax error because I am calling getTime on a null value.
Is it possible to tell ng-if to not run it if null and to skip that row?
What is the best way to be able to fix this problem and be able to run the getTime() on it?
Thank you
I had to create a function:
convToTime(date: Date) {
if (date !== null)
{
this.tDate = new Date(date.toString().substring(0,10));
return this.tDate.getTime();
}
}
and called it this way:
${this.convToTime(row.CreateDate)}
All working properly now and as needed.

Javascript internally changing array values, any fix?

I'm making a chess game. And I'm storing some tiles of the chessboard for some usage in my program in an array. The problem is, when I update the values in the array, and log them onto the console, I can see the values of the array, which are correct. But, upon clicking and checking the actual values in the console, the array shows different values.
I'm using google chrome's console.
I've already seen another stack overflow saying that this is a known "bug", but the developers won't fix it because it's not actually a bug.
Here's what I see in the console:
(2) ["a6", "a5"]
0: "`6"
1: "b6"
length: 2
__proto__: Array(0)
As you see, the values in the array I see are different than the values that are stored in the array. And this causes many other problems in my program which rely on the contents of this array.
What I want to know is if this is common, and also if there is any fix. Or maybe some scenarios where cases like this occur.
Also, I'm still unsure, for the array shown above ^, are the actual values of the array the values I see ("a6" and "a5"), or the wrong values shown internally ("`6" and "b6").
Please help, if this doesn't work, it will be extremely difficult to finish the project I'm working on.
Here is the code I use to update the array:
let moves = [];
if (tile.slice(1, 2) == 7 && b1.board[dd].piece == null && b1.board[d].piece == null) {
moves.push(d);
moves.push(dd);
}
if (tile.slice(1, 2) == 7 && b1.board[d].piece == null && b1.board[dd].piece != null) {
moves.push(d);
}
if (parseInt(tile.slice(1, 2)) > 1 && tile.slice(1, 2) != 7 && b1.board[d].piece == null) {
moves.push(d);
}
if (tile.charCodeAt(0) > 97 && parseInt(tile.slice(1, 2)) > 1 && b1.board[ld].piece != null && b1.board[ld].piece.slice(0, 1) == "w") {
moves.push(ld);
}
if (tile.charCodeAt(0) < 104 && parseInt(tile.slice(1, 2)) > 1 && b1.board[rd].piece != null && b1.board[rd].piece.slice(0, 1) == "w") {
moves.push(rd);
}
console.log(moves);
just to provide some context, I make an array called moves. I push values into the array based on some conditions. Then I log the array onto the console, which is what I showed above.
The if statements are working because the values I see in the array are correct. But as you know, upon clicking and checking the internal values in the console, they are wrong.
The result I am expecting from the console is:
(2) ["a6", "a5"]
0: "a6"
1: "a5"
length: 2
__proto__: Array(0)
How do I fix this problem?
Not sure this is the cause, but when console.log, Chrome and other browser don't output the serialized values of the arrays/objects but just stores the reference on them. Later when you inspect these arrays/objects Chrome will show the current values in those arrays, not the values that were present at the time of logging.
Try logging with console.log(JSON.stringify(thingToLog)). Or even better with console.log(JSON.stringify(thingToLog, undefined, ' ')).

cannot read shorthand javascript/query

i've spent ages trying to understand bootstrap's navigation bar, mainly by spending 4-5 days reading stackoverflow posts
& finally i think i've found an answer that helps!!!
trouble is, i can't understand the accompanying javascript/jquery code. i'm guessing its a shorthand version of js or something but just what it means i cannot decipher
basically, its the javascript code that appears on this jsfiddle page
$('.navbar').on('show', function () {
var actives = $(this).find('.collapse.in'),
hasData;
if (actives && actives.length) {
hasData = actives.data('collapse')
if (hasData && hasData.transitioning) return
actives.collapse('hide')
hasData || actives.data('collapse', null)
}
});
so, if anyone can explain to me what the code is doing on a line by line basis it'd be really cool
the first line i understand. its the weird-ass syntax in the next 6 lines that have me mystified
var actives = $(this).find('.collapse.in'),
hasData;
This creates two variables. One with elements picked from current scope that match the selector .collapse.in, and one empty variable.
if (actives && actives.length)
If actives exists and contains more than zero elements, do the following...
hasData = actives.data('collapse')
Retrieve arbitrary data stored under the key collapse. See https://api.jquery.com/jquery.data/ for more info.
if (hasData && hasData.transitioning) return
If hasData exists and hasData.transitioning is truthy, stop function execution.
actives.collapse('hide')
Call the collapse function on actives. This is not a native jQuery function, so you'll have to look up whatever plugin it comes from to make sense of the argument being passed in.
hasData || actives.data('collapse', null)
If hasData is truthy, skip this line. Otherwise, set the arbitrary data in actives variable to null.

Javascript lazy typecasting safety check

I am peer reviewing code written in vanilla javascript, and one of the authors occasionally performs a lazy-typecast if statement because he is evaluating values from a REST service that has very sloppy data. Occasionally a value representing money in USD comes back as a string, and sometimes as a number, probably depending on what validation was in place at the time the user entered the data over the years.
So sometimes we get a value of 150 and sometimes we get "150" (without the quotes, if you know what I mean).
The data is just displayed, and there are no math operations done against it: it simply needs to be compared to another number so some display styling can be done.
Instead of writing some function that checks the type of data and forces it to be a uniform type, the developer does something like this:
var baseVal = ReturnsAnIntegerValue(); // returns a number
var retrievedVal = CallToAwfulDataService(); // could return number or string
if (retrievedVal && retrievedVal == baseVal) {
// do stuff
}
else { ...
At first the == made me see a red flag, but when I asked him about it, he said that the initial check for if (retrievedVal && ... would prevent unexpected outcomes from situations where baseVal and retrievedVal are each some combination of null, 0, empty string, or undefined, like this:
if (null && null == undefined) // false | if (null == undefined) // true
if (0 && 0 == "") // false | if (0 == "") // true
if (0 && 0 == "0") // false | if (0 == "0") // true
if (0 && 0 == "") // false | if (0 == "") // true
You get the idea. I feel like this should be bad code because I was taught that the == is bad form, but the code is readable and concise, without the need for an extra function that type-converts just for show. I can't think of a scenario where we are likely to be hurt by it.
Is this bad code? Specifically: what is wrong with it? Is there a better practice for handling this?
edit
I forgot to mention, I have looked at a bunch of SO questions that mostly seem to back up the == is code smell school of thought, but I can't see what is technically risky about this scenario.
You should probably have some "normalize" function, and test cases with data samples from the backend.
Good practices generally assume that "all is fine" with the environment, which isn't your case.
Which ever way you go, you're doomed as far as "good practices" are concerned.

Fetching content from a form, should I check for undefined or null or?

If I do this:
var text = $("#some-input-box").val();
// check for null/empty string
if(text == null || text = undefined || text.length == 0 ) {
}
I want check for all cases if the value returned is null or empty string etc.
What should I be checking for here?
Is it a good idea to make a generic function that returns true/false if the text is empty or null or undefined ?
you can try out validation plugins like
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Or
http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
You should check for any invalid data, to make sure the user doesn't put something in that breaks some other functionality.
Maybe the validation plugin?
http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Categories