Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am finding css Rules and then changing it's style and i am done with it
but i don't know why i am getting this error
TypeError: sh[i] is undefined
if ("undefined"!==sh[i].cssRules)
my code is here
window.onload = function() {
var sh = document.styleSheets;
for (var i = 1; i <= sh.length; i++) {
if (sh[i].cssRules)
rule = sh[i].cssRules;
else if (sh[i].rules)
rule = sh[i].rules;
for (var j = 0; j < rule.length; j++) {
var sel = rule[j].selectorText;
if (sel == ".test") {
var R = rule[j].style;
R.color = "red";
}
}
}
var C = document.createElement("div");
C.className = "test";
C.innerHTML = "test";
var E = document.getElementById("div1");
E.appendChild(C);
}
i am finding all css that has been load to page and then find rule that i want to change.
if you have any question please ask me
Solved :
problem was at this line
for (var i = 1; i <= sh.length; i++) {
repalced <= withi <
Related
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 4 years ago.
Improve this question
I have one string as "testaabbaccc" in this string we contain palindrome as "abba" and it's length is 4 but how can we identify this with a JavaScript code.
var string ="testaabbaccc"
Need Output as abba is palindrome and length is 4
You can use this article and modify it to your needs.
Working demo
function isPalindrome(s) {
var rev = s.split("").reverse().join("");
return s == rev;
}
function longestPalind(s) {
var maxp_length = 0,
maxp = '';
for (var i = 0; i < s.length; i++) {
var subs = s.substr(i, s.length);
for (var j = subs.length; j >= 0; j--) {
var sub_subs = subs.substr(0, j);
if (sub_subs.length <= 1)
continue;
if (isPalindrome(sub_subs)) {
if (sub_subs.length > maxp_length) {
maxp_length = sub_subs.length;
maxp = sub_subs;
}
}
}
}
return maxp;
}
console.log(longestPalind("testaabbaccc"));
console.log(longestPalind("testaabbaccc").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
hi guys i tryed to create an empty array to fill it later. I post my code so you can help me. XD
"use strict"
var array = new array(6);
for(var i = 0; i <= 5; i++){
do {
var number = prompt("Put the element" + (i+1), 0);
}while(isNaN(number));
arr.push(number);
};
write.data(array);
It appears you may have copy and pasted some code without understanding what it is doing.
You are trying to push to an array, but you've declared your array as array, but trying to push to arr. Which is why it's not working.
var arr = new Array();
for(var i = 0; i <= 5; i++){
arr.push(i + 1);
}
console.log(arr);
alert(arr);
EDIT * You actually don't even need to declare the new Array(6), you can just use new Array() to push. However, if you would like to declare the size, you can do this instead.
var arr = new Array(6);
for(var i = 0; i <= 5; i++){
arr[i] = i + 1;
}
console.log(arr);
alert(arr);
You can also use this:
var arr = []; // create an empty object
for(var i = 0; i <= 5; i++){
arr.push(i + 1); // fill the object
}
console.log(arr);
alert(arr); // shows 1,2,3,4,5,6
This does what you are trying to achieve.
You can do it like
var a = new Array(6);
for(var i = 0;i < 6;i++){
a.push(i+1);
}
console.log(a);
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 6 years ago.
Improve this question
I've been reading about ES6 Let keyword vs existing var keyword.
I've got few questions. I understand that "scoping" is the only difference between let and var but what does it mean for the big picture?
function allyIlliterate() {
//tuce is *not* visible out here
for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
};
//tuce is *not* visible out here
};
function byE40() {
//nish *is* visible out here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
//nish *is* visible out here
};
Now my questions:
Does let posses any memory(/performance) advantage over var?
Other than browser support, what are the reasons why i should be using let over var?
Is it safe to start using let now over var in my code workflow?
Thanks,
R
let is much slower than var in node.js. Version v6.3.0 anyway. Sometimes this is dramatic. The code below is about three times slower if you replace var with let:
function collatz() {
var maxsteps = 0;
var maxval = 0;
var x = 1;
var n;
var steps;
while (x < 1000000) {
steps = 0;
n = x;
while (n > 1) {
if (n & 1)
n = 3*n + 1;
else
n = n / 2;
steps += 1;
}
if (steps > maxsteps) {
maxsteps = steps;
maxval = x;
}
x += 1;
}
console.log(maxval + ' - ' + maxsteps + ' steps');
}
collatz();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to encode Tags with .innerHTML
array encode html entities
Does not work well
var find = ['<','>','"'];
var replace = ['<','>','"'];
window.onload = function() {
var code = document.getElementsByTagName("code");
cl = code.length;
for (var e = 0; e < cl; e++)
code[e].innerHTML = code[e].innerHTML.replace(find,replace);
};
You're safer creating a text node. Any needed escapes will be done for you:
window.onload = function() {
var code = document.getElementsByTagName("code");
cl = code.length;
for (var e = 0; e < cl; e++) {
var tx = code[e].innerHTML;
code[e].innerHTML = "";
var n = document.createTextNode(tx);
code[e].appendChild(n);
}
};
<p><code>Escaped: <b><i>foo</i></b></code></p>
<p>Not: <b><i>foo</i></b></p>
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 8 years ago.
Improve this question
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
alert(test.substring(i,1));
}
I expected each alert to return each letter of the alphabet individually.
Instead, the first 5 alerts displayed as follows. Why?
a
b
bc
bcd
bcde
var test = "abcdefghijklmnopqrstuvwxyz";
for(i = 0; i < test.length; i++) {
console.log(test.substring(i,i+1));
}
actually, it's
substring(start, end)
not
substring(start, length)
unlike substr, which is indeed, substr(start, length)
If "start" is greater than "end", this method (substring) will swap the two arguments, meaning str.substring(1,4) == str.substring(4,1).
Use:
for(i = 0; i < test.length; i++) {
alert(test[i]);
}