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();
Related
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 1 year ago.
Improve this question
i've written
var i = 0;
var x = 0;
var y = 0;
var z = 0;
each variable is incremented by the user, however when i write
function blue() {
console.log(Math.max(i, x, y, z))
}
then I simply get back the highest number, not the name of the highest variable. The goal for this code eventually is to tell the user what number they are, and link them to the corresponding number's information. I've tried converting to an array however I'm a beginner with javascript and unsure where to go from here, would greatly appreciate any advice :)
repository for code in question is: https://github.com/LukeMcHenry311/showingproblem
inside of quiz folder
test.html
test.js
var i = 1;
var x = 3;
var y = 2;
var z = 0;
console.log(Object.entries({i,x,y,z}).sort(([, a], [, b]) => b - a)[0]);
You can use Array.prototype.reduce() with Object.entries():
var i = 1;
var x = 3;
var y = 2;
var z = 0;
max = Object.entries({ i, x, y, z }).reduce(function(carry, [key, value]) {
if (value > carry.value) {
carry.key = key
carry.value = value
}
return carry
}, { 'key': null, 'value': null });
console.log(max); // { key: 'x', value: 3 }
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
I am new to javascript and trying to loop through an array to set each element equal to a random number. I am wondering why this works:
function randomize() {
for (let i = 1; i < 16; i++) {
let random = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = random + "%";
}
}
but when I am trying to use an array instead of a variable, doesn't:
function randomize() {
const sortArray = [];
for (let i = 1; i < 16; i++) {
let sortArray[i] = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}
}
This is a syntax error. You should remove let in the array[i] declaration
function randomize() {
const sortArray = [];
for (let i = 1; i < 16; i++) {
sortArray[i] = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}
}
You are declaring sortArray again.
You have already declared sortArray as
const sortArray=[]; //sort array is already declared here
for (let i = 1; i < 16; i++) {
sortArray[i] = Math.floor(Math.random() * 30); //No need to declare again
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}}
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 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 5 years ago.
Improve this question
I will implement four equations first in javascript and later in go. My problem is i'm not a Mathematician, i don't know how to read the equations.
First one is the FFT window Blackman.
Second is an FFT algorithm:
Third is Smoothing over time:
Fourth is to decibel:
I don't can use a fft library for this job, it's required to implement this four equations. When the work is done i will run this four equations in this sequence on the same signal.
Can anyone help and explain how i come from the equations to working code?
I don't know where i should start.
Thank you for each answer
This is in javascript. Make the appropriate changes for Go as well. I've not tested everything, just translated the math equations into code. If there are errors please correct the answer.
var pi = 3.14;
var blackmann = function(N){
var a = 0.16;
var a0 = (1-a)/2, a1 = 1/2, a2 = a/2;
var w = [];
for (var i=0; i<N, i++){
w[i] = a0 - a1*Math.cos(2*pi*i/N) - a2*Math.cos(4*pi*i/N);
}
return w;
}
var fft = function(x, K){
var X1 = [], X2 = [];
var N = x.length;
// X1 for the real spectrum, X2 for the imaginary part.
// For magnitude spectrum take |X1^2 + X2^2|
// For a K point fft
for (var k=0; k<K; k++){
for (var n=0; n<N; n++){
X1[k] = Math.cos(2*pi*n/N);
X2[k] = Math.sin(-2*pi*n/N);
}
}
return {"real":X1, "img":X2}
}
var smooting = function(x){
var s = [], t = 0.16
s[0] = x[0];
for (var i=1; i<x.length; i++){
s = t*s[i-1] + (1-t)*x[i]
}
return s
}
var decibel = function(X){
var Y = [];
for (var i =0;i<X.length(); i++){
Y[i] = Math.log10(Math.abs(X[i]));
}
return Y;
}
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 <