Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last month.
Improve this question
export function carBtnsMoveActive(boolean) {
const btnStart = document.querySelector('.btn-start')
const btnStop = document.querySelector('.btn-stop')
if (boolean) {
btnStart.disabled = false
btnStart.classList.remove('btn-off')
btnStop.disabled = true
btnStop.classList.add('btn-off')
} else {
btnStart.disabled = true
btnStart.classList.add('btn-off')
btnStop.disabled = false
btnStop.classList.remove('btn-off')
}
}
Hello, can someone help to improve and shrink down this 10 lines of code? I know this, IF can appearance better but i don't have knownledge how to do. I want to write readable code which has less lines. The code works.
I haven't tested this code, but this is my strategy for refactoring your code into something more concise.
function setEnabled(btn, isEnabled) {
btn.disabled = !isEnabled;
btn.classList[isEnabled ? 'remove' : 'add']('btn-off');
}
setEnabled(btnStart, !boolean)
setEnabled(btnStop, boolean)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have an array
const databaseName = ['Oracle','Mysql','MongoDb'];
the expected output is
const database = [
{
name:'Oracle',
isAvailable:true
},
{
name:'Mysql',
isAvailable:true
},
{
name:'MongoDb',
isAvailable:true
}
]
This has to be done using Javascript and es6.
i have tried using
const database = {...databaseName ,isAvailable:true}
const databaseName = ['Oracle','Mysql','MongoDb'];
console.log(
databaseName.map(name => {
return {
name,
isAvailable:true
}
})
)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a similar use case to this
import { BehaviorSubject, combineLatest } from 'rxjs'
import { map, debounceTime } from 'rxjs/operators'
const items$ = new BehaviorSubject([]);
const size$ = new BehaviorSubject(10);
const visibleItems$ = combineLatest([items$, size$])
.pipe(
debounceTime(0),
map(([items, size]) => items.slice(0, size))
);
And some times I have this scenario
const onData = bigArr => {
items$.next(bigArr);
}
Sometimes this
const changeSize = () => {
size$.next(20);
}
And sometimes this
const onData2 = bigArr => {
items$.next(bigArr);
size$.next(10);
}
I don't want to trigger the visibleItems$ observable flow multiple times, so a solution that I've found is use the debounceTime operator with 0 ms to perform the onData2 method without running the pipe operators twice, but it is a bit hard to reason this operator (at least from the first glance). How can I replace it, so my code is easier to understand?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm getting data from an API and I want to filter the array down based on the issue state. The following code works, but is there a nice way to write this?
let issuesToUse = this.state.issues;
if(this.state.issueState.status === 'closed'){
issuesToUse = this.state.issues.filter(issue => {
return issue.state === 'closed';
})
} else {
issuesToUse = this.state.issues.filter(issue => {
return issue.state === 'open';
})
}
issuesToUse = this.state.issues.filter(issue => issue.state === this.state.issueState.status)
You could try something like this example
let targetState = this.state.issueState.status;
let issuesToUse = this.state.issues.filter(
({ state }) => state === targetState
);
See
Destructuring assignment
If you have other states as well, you need to check the state in advance for filtering.
let issuesToUse = this.state.issues;
if (['closed', 'open'].includs(this.state.issueState.status)) {
issuesToUse = this.state.issues.filter(({ state }) => state === this.state.issueState.status)
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Hi i found this extension and would like to see the contents of the script itself to check what is actually does. I tried putting the script through all sort of javascript de/un-obfuscators and none worked.
Please tell me one that works or send me the de-obfuscated JS.
CODE HERE
The point of obfuscating something is so it cant be reversed. It has stripped all names and minified intentionally to make it as hard as possible.
You can reverse it partially but it won't be very readable.
For example if you obfuscate this
function hi(first, second) {
const third = first + second
return third
}
hi(1, 2);
You will get something like this.
function hi(_0x850806,_0x3f2655){const _0x2231a6=_0x850806+_0x3f2655;return _0x2231a6;}hi(0x1,0x2);
As this only uses primitive functionality it can easily be reversed to
function hi(_0x850806, _0x3f2655) {
const _0x2231a6 = _0x850806 + _0x3f2655;
return _0x2231a6;
}
hi(0x1, 0x2);
However, if you use some more complex functions (eg Array.includes). Like this
function hi(first, arr) {
if (arr.includes(first)) {
console.log('true')
} else {
console.log('false')
}
}
const res = hi(1, [1, 2, 3]);
This can be obfuscated to
const _0x41ab=['includes','log','true'];(function(_0x4972c4,_0x6d8a76){const _0x4e2cf3=function(_0x383eb3){while(--_0x383eb3){_0x4972c4['push'](_0x4972c4['shift']());}};_0x4e2cf3(++_0x6d8a76);}(_0x41ab,0x1d7));const _0x4ad9=function(_0x4972c4,_0x6d8a76){_0x4972c4=_0x4972c4-0x0;let _0x4e2cf3=_0x41ab[_0x4972c4];return _0x4e2cf3;};function hi(_0x5b3f5d,_0x577a76){if(_0x577a76[_0x4ad9('0x0')](_0x5b3f5d)){console['log'](_0x4ad9('0x2'));}else{console[_0x4ad9('0x1')]('false');}}const res=hi(0x1,[0x1,0x2,0x3]);
After tidying it up you get this.
const _0x41ab = ['includes', 'log', 'true'];
(function(_0x4972c4, _0x6d8a76) {
const _0x4e2cf3 = function(_0x383eb3) {
while (--_0x383eb3) {
_0x4972c4['push'](_0x4972c4['shift']());
}
};
_0x4e2cf3(++_0x6d8a76);
}(_0x41ab, 0x1d7));
const _0x4ad9 = function(_0x4972c4, _0x6d8a76) {
_0x4972c4 = _0x4972c4 - 0x0;
let _0x4e2cf3 = _0x41ab[_0x4972c4];
return _0x4e2cf3;
};
function hi(_0x5b3f5d, _0x577a76) {
if (_0x577a76[_0x4ad9('0x0')](_0x5b3f5d)) {
console['log'](_0x4ad9('0x2'));
} else {
console[_0x4ad9('0x1')]('false');
}
}
const res = hi(0x1, [0x1, 0x2, 0x3]);
As you can see, this snippet will give the same output as the non-obfuscated code, however, is much harder to read.
Now imagine applying this to a big project and you see why it is almost impossible.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is javascript serialized array:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4,"children":[{"id":5},{"id":6},{"id":7}]},{"id":8}]}]
How to save this (dynamic) to mysql with php like this:
***********************************
| id | subsite_id | orderby |
1 0 0
2 0 1
3 0 2
4 3 0
5 4 0
6 4 1
7 4 2
8 3 1
***********************************
Thanks for answer.
This might not be the best solution, but it's certainly a solution. Recently, I learned about RecursiveIterators and their cousin, RecursiveIteratorIterator. So, I took it upon myself to use them in everything I code (relevant XKCD: https://xkcd.com/208/).
I hacked this up quickly:
class ChildIDIterator implements RecursiveIterator{
private $_array;
private $_position = 0;
private $_parent;
public function __construct(array $array, $parent=0) {
$this->_array = $array;
$this->_parent = $parent;
}
function valid(){
return isset($this->_array[$this->_position]);
}
function current() {
return $this->_array[$this->_position]['id'];
}
function next() {
$this->_position++;
}
function rewind() {
$this->_position = 0;
}
function key() {
return $this->_position;
}
function hasChildren(){
return isset($this->_array[$this->_position]['children']);
}
function getChildren(){
return new self(
$this->_array[$this->_position]['children'],
$this->_array[$this->_position]['id']
);
}
function getParent(){
return $this->_parent;
}
}
This recursively iterates over your (decoded) array and returns the id values. To use it, you can do this:
$json = '[{"id":1},{"id":2},{"id":3,"children":[{"id":4,"children":[{"id":5},{"id":6},{"id":7}]},{"id":8}]}]';
$array = json_decode($json, TRUE);
$iterate = new RecursiveIteratorIterator(new ChildIDIterator($array), RecursiveIteratorIterator::SELF_FIRST);
foreach($iterate as $order=>$id){
echo "UPDATE sites SET subsite_id={$iterate->getParent()}, orderby={$order} WHERE id={$id};\n";
}
DEMO: https://eval.in/57189