How to come from equations to code with javascript [closed] - javascript

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;
}

Related

How do you get Math.max to give you the variables name [closed]

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 }

i want to create an empty array to fill it later [closed]

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);

How to write a JS code which will print Fibonacci series where End value is not greater than 33. (e.g. 0112358) [closed]

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 4 years ago.
Improve this question
Write a JS code which will print Fibonacci series where End value is not greater than 33. (e.g. 0112358)
var n=10;
function fib(n){
var a=[];
a[0]=0;
a[1]=1;
for(i=2;i<n;i++){
a[i]=a[i-1]+a[i-2];
}
return a;
}
console.log(fib(n));
Try the following:
function fib(n){
var a = 0;
var b = 1;
var c = 0;
document.write(a+" "+b);
for(var i = 2; i < n; i++){
c = a + b
if(c > 33)
break;
a = b;
b = c;
document.write(" "+c);
}
}
fib(10);
I get this solution by using array. but what is efficient way to this task.
function fib(n) {
var arr=[];
var a,b,c;
a=0;
b=1;
arr.push(a);
arr.push(b);
while((a+b)<n){
c=a+b;
a=b;
b=c;
arr.push(c);
}
return arr;
}
console.log(fib(33));

Spaces in a joined array [closed]

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 6 years ago.
Improve this question
I have been working on a ceaser cipher algorithm but I haven't been able to grasp the reason why the joined array returns spaces in a peculiar state.
function rot13(str) { // LBH QVQ VG!
var string = str.split('');
var codedStr = [];
var encoded = [];
for (var k=0; k < string.length; k++){
codedStr.push(string[k].charCodeAt());
}
for(var i = 0; i < codedStr.length; i++){
if(codedStr[i] > 77 ){
codedStr[i] -= 13;
}
else if( codedStr[i] == 32 || codedStr[i] == 63){
codedStr[i] = codedStr[i];
}
else{
codedStr[i] += 13;
}
encoded.push(codedStr[i]);
}
var decode = codedStr.map(String.fromCharCode);
var result = decode.join('');
return result;
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));
String.fromCharCode accepts multiple arguments, and map provides 3. You should use
codedStr.map(code => String.fromCharCode(code));

let vs var performance [closed]

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();

Categories

Resources