document.getElementById()
на находит элемента с данным id – ведь вы тут же пытаетесь изменить свойство checked
. checked
;try .. catch
function setCheck(arr, prefix) {
arr.forEach( item => {
const el = document.getElementById(prefix + item);
if (el && el.hasOwnProperty("checked")) el.checked = true;
});
}
setCheck (resKolichestvoSpalen, "kolichestvo-spalen-value-");
setCheck (resKolichestvoEtazhey, "kolichestvo-etazhey-value-");
const rowsNodeList = document.querySelectorAll("table tr"); // это еще не Array
const rowsArray = Array.prototype.slice.call(rowsNodeList);
const rowOne = document.querySelector("table tr");
const tdNodeList = rowOne.querySelectorAll("td"); // подумайте, как сюда же включить и все <th>
const tdArray = Array.prototype.slice.call(tdNodeList);
document.querySelector('tr')
всегда будет возвращать самую первую строку в самой первой таблице в документе. const SSH = require('simple-ssh');
const hosts = ['192.168.10.1', '192.168.20.1',];
async function processIPs(ipArr) {
for (ip of hosts) {
//console.log(ip);
const ssh = new SSH({
host: ip,
port: '22',
user: 'root',
pass: 'password'
});
const pr = new Promise((res, rej) => {
ssh
.exec('cat /proc/uptime', {
out: function(stdout) {
res(stdout);
}
})
.start();
});
const result = await pr;
console.log("ip: %s, result: %s", ip, result);
}
});
processIPs (hosts);
console.log("Done");
let user = {
name: 'Василий',
get name2() {
return this.name;
}
};
user.name2 // Василий
function monthly(a, b, n) {
const result = a * b / 12;
console.log("%f * %f / 12 = %f", a, b, result);
if (n) return monthly(a + result, b, n - 1);
else return result;
}
monthly(1e6, 0.15, 12);
1000000.000000 * 0.150000 / 12 = 12500.000000
1012500.000000 * 0.150000 / 12 = 12656.250000
1025156.250000 * 0.150000 / 12 = 12814.453125
1037970.703125 * 0.150000 / 12 = 12974.633789
1050945.336914 * 0.150000 / 12 = 13136.816711
1064082.153625 * 0.150000 / 12 = 13301.026920
1077383.180546 * 0.150000 / 12 = 13467.289757
1090850.470303 * 0.150000 / 12 = 13635.630879
1104486.101181 * 0.150000 / 12 = 13806.076265
1118292.177446 * 0.150000 / 12 = 13978.652218
1132270.829664 * 0.150000 / 12 = 14153.385371
1146424.215035 * 0.150000 / 12 = 14330.302688
1160754.517723 * 0.150000 / 12 = 14509.431472
eval(JSON.stringify(array).replace(/[^\d]+/g, '+') + '0') // 31
const a = [1, 2, [3, 4, [[5, 6], 7], 8]];
// Вжух! и получится строка:
"+1+2+3+4+5+6+7+8+"
timeout
Type: Number
Set a timeout (in milliseconds) for the request. A value of 0 means there will be no timeout. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.
str[i] = "x"
Так можно только читать.function reverse(str) {
var i, len = str.length, result = "";
for (i = len - 1; i >= 0; i--) result += str[i];
return result;
}
function getWithImages(result) {
return new Promise((res, rej) => {
const elPromises = [];
for (let key in result) {
if (!result.hasOwnProperty(key)) continue;
const element = result[key];
element.introtext = h2p(element.introtext);
element.fulltext = h2p(element.fulltext);
elPromises.push(
clientGI.search(element.title)
.then(images => {
element.images = images.map(image => [image.url, image.thumbnail]);
});
);
}
Promise.all(elPromises).then(function() {
// все готовы
res(result);
});
});
}
const gotImagesPromise = getWithImages(results[0]);
/**
* Разбивает строку на три части: цифры, две буквы, две буквы.
* Возвращает либо массив из трех элементов,
* либо undefined
*/
function getParts(str) {
const re = /^(\d+)(\S{2})(\S{2})$/;
const match = str.match(re);
if (match) return match.slice(1);
}
getParts("5дмсм") // ["5", "дм", "см"]
>> 1, 2, 3, 4, 5;
<– 5
undefined
.console.log()
. function camelCase(str) {
return str
.trim()
.split(' ')
.map((w, i, a) => {
if (i === 0) return w.toLowerCase();
return w.substr(0, 1).toUpperCase() + w.substr(1).toLowerCase();
})
.join('')
}
function test() {
const testIn = ['Good Day', 'good night', 'Good Evening', 'Sleep', 'Go'];
const testOut = ['goodDay', 'goodNight', 'goodEvening', 'sleep', 'Go'];
for (let i = 0; i < testIn.length; i++) {
const input = testIn[i];
const output = camelCase(input);
if (testOut[i] === output) console.log("Test passed: %s => %s", input, output);
else console.error("Test failed: %s => %s", input, output);
}
}
test();
function chooseBestSum(t, k, ls) {
/**
* make next combination of N on bits in a 32-bit integer
*/
function nextPerm(x) {
// via https://stackoverflow.com/questions/506807/creating-multiple-numbers-with-certain-number-of-bits-set
if (x === 0) return 0;
const smallest = (x & -x);
const ripple = x + smallest;
const new_smallest = (ripple & -ripple);
const ones = ((new_smallest/smallest) >> 1) - 1;
return ripple | ones;
}
let bestSum = null, bestN;
const len = ls.length;
if (len > 31) throw "Too many (over 31) options for this algorithm";
const maxmask = (1 << len) - 1;
if (len < k) return null; // not enough distances
ls.sort((a, b) => a - b); // todo: skip checking rest of combinations once solid selection of elements exceeds t
let mask = (1 << k) - 1; // initial mask value with k less significant bits on
let sum, pos, n;
while(true) {
for(sum = 0, n = 0, pos = 0; pos < 32; pos++) {
if (mask & (1 << pos)) {
sum += ls[pos];
if (++n === k) break;
}
}
if (sum > bestSum && sum <= t) {
bestSum = sum;
bestN = mask;
}
mask = nextPerm(mask);
if (mask > maxmask) break;
if (mask < 0) break;
}
return bestSum;
}
const data = [
[ '6', 'Shorter', '157' ],
[ '7', 'Fraser', '157' ],
[ '6', 'Chandter', '156' ]
// ...
];
const result = data.reduce((acc, cur) => {
acc.forEach((el, idx) => {el.push(cur[idx])});
return acc;
}, [[], [], []]);
// [["6","7","6"],["Shorter","Fraser","Chandter"],["157","157","156"]]