Alternate between subtracting and adding array items - javascript

I'm trying to create a formula for calculating PI using the Gregory-Leibniz series. I've created a for loop that's populating a new array with the individual values, but I need a way to then alternate between subtracting and adding each array item and then spit out the resulting number. Essentially, I need to get to something like this:
(4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15)
The following for loop is populating the new array correctly:
var arrLimit = 39;
var newArray = [];
for(i = 1; i <= arrLimit; i += 2) {
newArray.push(4/i);
}
At this point I get stuck. I need some way to take newArray and alternate between subtracting and adding the values, and then produce the final number.

You can sum your array with reduce, then judge *=-1 or *=1 based on the index of the array.
The code will be like below:
//Test Case 1
var arrLimit = 39;
var newArray = [];
for(i = 1; i <= arrLimit; i += 2) {
newArray.push(4/i);
}
console.log(newArray.reduce(function(pre, cur, currentIndex){
return pre+cur*(currentIndex%2 ? -1 : 1);
}, 0));
//Test Case 2
arrLimit = 11139;
newArray = [];
for(i = 1; i <= arrLimit; i += 2) {
newArray.push(4/i);
}
console.log(newArray.reduce(function(pre, cur, currentIndex){
return pre+cur*(currentIndex%2 ? -1 : 1);
}, 0));

Keep an extra variable, isNeg = true. On each iteration of the loop, just alternate it with isNeg = !isNeg;.
var arrLimit = 39;
var newArray = [];
var isNeg = false;
for(i = 1; i <= arrLimit; i += 2) {
isNeg = !isNeg;
var val = 4/i;
if (isNeg) {
val *= -1;
}
newArray.push(val);
}
You can also assign 1 to the variable, and multiple it by -1 each time to do the flip:
var arrLimit = 39;
var newArray = [];
var factor = 1;
for(i = 1; i <= arrLimit; i += 2) {
factor *= -1;
var val = factor * 4 / i;
newArray.push(val);
}
Personally I favor the second approach, but both work just fine.

I need some way to take newArray and alternate between subtracting and adding the values, and then produce the final number.
You can do that with Array.prototype.reduce
// Your code, unchanged
var arrLimit = 39;
var newArray = [];
for(i = 1; i <= arrLimit; i += 2) {
newArray.push(4/i);
}
// reduce the array to the final result
// (2*(-i%2)+1) alternates between `-1` and `1`.
var result = newArray.reduce( (prev,curr,i) => prev + (2*(-i%2)+1)*curr );
console.log( result );

How about doing away with the array in the first place and being able to "calculate" pi with as many terms as you like.
let pi = 0,
terms = 100;
for (var i = 0; i < terms; i++){
pi += 4/(2*i + 1) * ( i % 2 == 0 ? 1 : -1)
}
console.log(pi);

You could add an extra variable. If you want you could sum at the same time.
var arrLimit = 39;
var newArray = [];
var a=-1
var sum =0
for(i = 1; i <= arrLimit; i += 2) {
a*=-1;
newArray.push(a*4/i);
sum+=a*4/i
}

You can look at the length of newArray when you build it and multiply the value by -1 or +1
var arrLimit = 39;
var newArray = [];
for(i = 1; i <= arrLimit; i += 2) {
newArray.push(4 / i * (newArray.length % 2 ? 1 : -1));
}
console.log(newArray);

Related

I need to make an array of 15 random integers. I have a function but dont want numbers to repeat

I'm working on a project for school. I need to generate an array of 15 random integers between 1 & 50. I have a function, but I would not like for the numbers to repeat. (for example, if the number 3 is located at index 0, I would not like for it to show up again in the array.) If I could get some help on not getting repeat numbers, that would be great.
Thank you for any help!
var arr;
function genArray() {
//generates random array
arr = [];
for (var i = 0; i < 15; i++) {
var min = 1;
var max = 50;
var arrayValue = Math.floor(Math.random() * (max - min + 1)) + min;
arr.push(arrayValue);
}
arr.sort(function(a, b) {
return a - b
});
console.log(arr);
}
In the loop generate a new random number while the number is in the array. In other words only continue when the new number is not in the array already.
var arr;
function genArray() {
//generates random array
arr = [];
for (var i = 0; i < 15; i++) {
var min = 1;
var max = 50;
do
{
var arrayValue = Math.floor(Math.random() * (max - min + 1)) + min;
}while(arr.includes(arrayValue))
arr.push(arrayValue);
}
arr.sort(function(a, b) {
return a - b
});
console.log(arr);
}
genArray();
You can make a function in which check the number if its already in array than regenrate the number else push the number in array
var arr;
function genArray() {
//generates random array
arr = [];
for (var i = 0; i < 15; i++) {
var min = 1;
var max = 50;
var arrayValue = Math.floor(Math.random() * max) + min;
if(checkno(arrayValue)==true)
arr.push(arrayValue);
}
arr.sort(function(a, b) {
return a - b
});
console.log(arr);
}
function checkno(var no)
{
for(var i=0;i<arr.length;i++)
{
if(arr[i]==no)
return false;
else
return true;
}
}
An alternate solution involves the Set object, sets only have unique elements, multiple elements of the same value are ignored.
Example of the set object implemented for this use:
var temp = new Set();
while (temp.size < 15) {
var min = 1;
var max = 50;
temp.add(Math.floor(Math.random()*(max-min+1))+min);
}
This approach uses Arrow functions, forEach and includes functions.
let LENGTH = 15;
let numbers = new Array(LENGTH).fill();
let findRandomNumber = (i) => {
let rn;
while (numbers.includes((rn = Math.floor(Math.random() * 50) + 1))) {}
numbers[i] = rn;
};
numbers.forEach((_, i) => findRandomNumber(i));
console.log(numbers.sort((a, b) => a - b));
.as-console-wrapper {
max-height: 100% !important
}
You do not need to check the resulting array and regenerate the number. It is not efficient.
Please take a look at the following snippet:
function get_N_rand(N = 15, min = 1, max = 50) { // set default values
var N_rand = [], range = [];
for (var i = min; i <= max;) range.push(i++); // make array [min..max]
while (N_rand.length < N) { // cut element from [min..max] and put it into result
var rand_idx = ~~(Math.random() * range.length);
N_rand.push(range.splice(rand_idx, 1)[0]);
}
return N_rand;
}
console.log(JSON.stringify( get_N_rand() )); // run with defaults
console.log(JSON.stringify( get_N_rand(6, 10, 80) )); // run with arbitraries

Counting duplicate random numbers from a for loop

I am trying to create a score predictor based on a teams goal difference (football). I am new to JavaScript, and I have managed to get this far.
I want it to be like spinning a ten-sided dice 20 times + the team's goal difference. I have got this bit sorted I think. With my code now I have a list of random numbers logged in the console which is what I wanted. Now I would like to choose a number (e.g., 2) and see how many times this occurs in the list. I'd like to save this in a new variable called homeFinalScore (So if '2' occurs three times in the list of random numbers, the homeFinalScore variable should be 3). I've tried several things but have been unable to sort it yet!
Any help would be extremely helpful. Thank you in advance!
var homeFinalScore = 0;
function calculateScore(){
var homeTeam = document.getElementById("HomeTeam").value;
var awayTeam = document.getElementById("AwayTeam").value;
var homeGd = parseInt(document.getElementById("HomeGD").value);
var awayGd = parseInt(document.getElementById("AwayGD").value);
var homeGd = 20 + homeGd;
var awayGd = 15 + awayGd;
for (i = 0; i < homeGd; i++) {
var randNum = Math.floor(Math.random() * 11);
console.log(randNum);
}
}
You can create an array, use Array.prototype.push() to push randNum to the array, then use Array.prototype.filter(), .length to determine how many occurrences of a value are present within array.
var homeGd = 20 + 2;
var awayGd = 15 + 2;
var arr = [];
function countOccurrences(n, arr) {
return arr.filter(function(value) {
return value === n
}).length;
}
for (i = 0; i < homeGd; i++) {
var randNum = Math.floor(Math.random() * 11);
arr.push(randNum);
}
console.log(arr);
console.log(countOccurrences(2, arr));
Alternatively, you can increment a variable when randNum is equal to a value.
var homeGd = 20 + 2;
var awayGd = 15 + 2;
var n = 0;
var num = 2;
for (i = 0; i < homeGd; i++) {
var randNum = Math.floor(Math.random() * 11);
console.log(randNum);
if (randNum === num) {
++n
}
}
console.log("occurrences of 2:", n);
const homeGd = 10;
const randomNumbers = []; // array of random numbers
for (i = 0; i < homeGd; i++) {
randomNumbers.push(Math.floor(Math.random() * 11));
}
const countBy = randomNumbers.reduce((acc, current) => {
acc[current] = (acc[current] || 0) + 1;
return acc;
}, {});
console.log(countBy);

How can I get the sum of all odd fibonacci vales in javaScript?

I am working through this Free Code Camp exercise.
Return the sum of all odd Fibonacci numbers up to and including the
passed number if it is a Fibonacci number. The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and
8, and each subsequent number is the sum of the previous two numbers.
And here is what I have so far...
Any suggestions?
function sumFibs(num) {
var arr, isFibVal, isEvenVal, sum, i = 0, fibonacci = function (num){
var a, b, result, fibArr = [1];
a=0;
b=1;
result=b;
for(var j = 0; j < num; j++){
result = a + b;
a = b;
b = result;
fibArr.push(result);
}
return fibArr;
},
isFib = function (val){
var prev = 0;
var curr = 1;
while(prev<=val){
if(prev == val){
return true;
} else {
return false;
}
curr = prev + curr;
prev = curr - prev;
}
},
isEven = function(someNumber){
return (someNumber % 2 === 0) ? true : false;
};
function sumArray(array) {
for (
var
index = 0, // The iterator
length = array.length, // Cache the array length
sum = 0; // The total amount
index < length; // The "for"-loop condition
sum += array[index++] // Add number on each iteration
);
return sum;
}
arr = fibonacci(num);
isFibVal = isFib(num);
isEvenVal = isEven(num);
if (isFibVal && !isEvenVal){
sum += sumArray(arr);
}
return sum;
}
All I get back is undefined which seems to be weird because i thought this part of my code was pretty cool—using the function values to check vs. in the if statement.
arr = fibonacci(num);
isFibVal = isFib(num);
isEvenVal = isEven(num);
if (isFibVal && !isEvenVal){
sum += sumArray(arr);
}
I won't give you the answer outright since you're going through FCC, but I'll provide you with some hints as to where to look:
See this segment:
for(var j = 0; j < num; j++){
result = a + b;
a = b;
b = result;
fibArr.push(result);
}
And this one:
function sumArray(array) {
for (
var
index = 0, // The iterator
length = array.length, // Cache the array length
sum = 0; // The total amount
index < length; // The "for"-loop condition
sum += array[index++] // Add number on each iteration
);
return sum;
}
Also, you probably don't need this segment at all:
isFibVal = isFib(num);
isEvenVal = isEven(num);
if (isFibVal && !isEvenVal){
sum += sumArray(arr);
Good luck. As someone who has finished a good chunk of the curriculum, I can say that Free Code Camp is the real deal.
You're pretty close and the other answer is good for pushing you in the right direction, I'll post a different way that does this using native JS functions:
Example of the code below in JSBin
function fibs(n) {
var f = [0, 1];
var extraNumber = 0;
for (var i = 0; i < n; i++) {
f.push(f[f.length - 1] + f[f.length - 2]);
}
// lets check if the passed in number is a fib:
if (f.indexOf(n) > -1) {
extraNumber = n;
}
console.log(f); // just to check we can cut all the logs later...
var filtered = f.filter(function(num) {
// filter out the even numbers
return num % 2 === 1;
});
console.log(filtered);
var sum = filtered.reduce(function(a, b) {
// add up whats left
return a + b;
});
console.log(sum);
return sum + extraNumber;
}
heres my solution, and i find it to be pretty readable:
function sumOddFibs(num) {
// initialize with 2 because
// fib sequence starts with 1 and 1
var sum = 2;
var prev = 1;
var curr = 1;
var next = 2;
while (next <= num) {
prev = curr;
curr = next;
next = prev + curr;
if (curr % 2 !== 0) {
sum += curr;
}
}
return sum;
}
You could start by defining variables for the previous number, current number, and total Fibonacci
To check for odd numbers, you could use an if statement and use %:
if (currNum % 2 !== 0){ }
If current number is odd, then you add it to the total
fibTotal += currNumber;
To determine the next Fibonacci number you, you will need to add the previous and current number:
var nextNumber = prevNumber + currNumber;
You will need to update the previous number to the current one
prevNumber = currNumber;
Set the current number to the next Fibonacci number in the sequence
currNumber = nextNumber;
Hope this helps.

Array equals itself plus number = NaN

I must be doing something stupid. The array newArea needs to add up data from all regions, i.e. be global. Regions are represented by variable p. But when I try to get newArea array to add to itself, e.g. newArea[p] += otherArray, it outputs NaNs. Even newArea[p] += 1 outputs NaNs.
Can anyone spot what I'm doing wrong? It's driving me mad and I'm working to a deadline.
mm=0
var maxVolume = 0;
var tempCAGR = 0;
var maxCAGR = 0;
var newArray = [];
var newRegions = [];
var newConsValues = [];
var newArea = [];
for (var p=0; p<arrayRef[mm].length; p++) {//9 regions
newArray[p] = [];
for (var q=0; q<arrayRef[mm][p].length; q++) {//4 scenarios
newArea[q] = [];
if (q==0) {
newRegions.push(arrayRef[mm][p][q][0]);
newConsValues.push(arrayRef[mm][p][q][1]);
}
for (var r=0; r<dates.length; r++) {//time
//console.log('p: '+p+', q: '+q+', r: '+r);
if (p==0) {
newArea[q][r] = 1;
} else {
newArea[q][r] += 1;
}
}
arrayRef[mm][p][q].shift();
tempCAGR = Math.pow(( arrayRef[mm][p][q][len] / arrayRef[mm][p][q][1] ),(1/len))-1;
//console.log(newRegions[p]+', num: '+arrayRef[mm][p][q][len-1]+', denom: '+arrayRef[mm][p][q][0]+', len: '+len+', cagr: '+tempCAGR);
newArray[p][q] = tempCAGR;
maxCAGR = Math.max(maxCAGR,tempCAGR);
}
}
console.log(newArea);
You are cleaning the array in newArea everytime you loop through it:
...loop q ...
newArea[q] = []; // <-- resets the array at q pos
... loop r ...
if (p==0) {
newArea[q][r] = 1;
} else {
newArea[q][r] += 1;
}
So when p === 0 it will fill an array at q pos of your newArea array. However, next iteration of p will clear them out, so there's nothing there to sum.
You probably want to keep the old array or create a new one if there isn't one.
newArea[q] = newArea[q] || [];
It looks like you do not have the variable initialised. With adding something to undefined, you get NaN.
You can change the art of incrementing with a default value:
if (p == 0) {
newArea[q][r] = 1;
} else {
newArea[q][r] = (newArea[q][r] || 0) + 1;
}

How to reverse the order in a FOR loop

I've a simple FOR statement like this:
var num = 10,
reverse = false;
for(i=0;i<num;i++){
console.log(i);
}
when reverse is false I want it to return something like [0,1,2,3,4,5,6,7,8,9]
but, when reverse is true, it should return [9,8,7,6,5,4,3,2,1,0]
Which is the most efficient way to get this result, without checking every time if reverse is true or false inside the loop?
I don't want to do this:
var num = 10,
reverse = false;
for(i=0;i<num;i++){
if(reverse) console.log(num-i)
else console.log(i)
}
I would like to check reverse only one time outside the loop.
var num = 10,
reverse = false;
if(!reverse) for( var i=0;i<num;i++) log(i);
else while(num-- ) log(num);
// to avoid duplication if the code gets long
function log( num ) { console.log( num ); }
EDIT:
As noted in the comments below, if i is not declared elsewhere and you do not intend for it to be global, then declare it with the other variables you declared.
And if you don't want to modify the value of num, then assign it to i first.
var num = 10,
reverse = false,
i;
if(!reverse) for(var i=0;i<num;i++) log(i); // Count up
else {var i=num; while(i--) log(i);} // Count down
function log( num ) { console.log( num ); }
Try use 2 loops:
if (reverse) {
for(i=num-1;i>=0;i--){
console.log(i)
}
}
else {
for(i=0;i<num;i++){
console.log(i)
}
}
var num = 10,
reverse = false;
for (var i = 0, n = reverse?num-1:0, d = reverse?-1:1; i < num; i++, n+=d) {
console.log(n);
}
This is equivalent to the following, which is more readable, but less compact:
var num = 10,
reverse = false;
var start = reverse ? num-1 : 0,
end = reverse ? -1 : num,
step = reverse ? -1 : 1;
for (var i = start; i != end; i += step) {
console.log(i);
}
Edit:
Actually, these two solutions are not identical, because the first one has an additional increment operation. Still, it is negligible from performance point of view. If you really want to get a compact solution that has the best performance, you can do the following (not for the faint of heart):
var num = 10,
reverse = false;
for (var r=reverse, i=r?num-1:0, n=r?-1:num, d=r?-1:1; i!=n; i+=d) {
console.log(i);
}
This has the advantage of having a single control structure, a single test in each loop, and a single iterator addition. It is not as fast as having an iterator increment/decrement, but only marginally so.
var start; var end; var inc;
if (reverse) {
start = num-1; end = 0; inc = -1;
}
else {
start = 0; end = num-1; inc = 1;
}
for(i=start;i!=end;i+=inc){
console.log(i)
}
I just came across the need for this the other day. Here's how I did it:
var num = 10,
i = 0,
direction = 1,
reverse = false;
if(reverse)
i = num + (direction = num = -1);
for(; i !== num; i += direction) {
console.log(i);
}
No need for separate loops, and no need to do math to calculate the proper i in the loop.
So if reverse is true...
i (which represents our first item) becomes num - 1, so we're now starting on what would have been the last item
num (which represents out of bounds) becomes -1, so we're now stopping on what would have been the first item
direction is -1, which means it will decrement when we do i += direction
So by swapping our starting point with our ending point and changing the alteration of i from 1 to -1, we'll be going up or down based on those modifications.
I think this meets your requirements:
var num = 10;
var reverse = false;
var diff = 0;
if (reverse) {
diff = num - 1;
}
for (i = 0; i < num; i++) {
console.log(Math.abs(diff - i));
}
Here's how I've always done reverse loops:
for (i = num; --i >= 0; ) ...
And what's your problem with:
if (reverse)
{
for(i=num-1; i>=0;i--){
console.log(i);
}
}
else
{
for(i=0;i<num;i++){
console.log(i)
}
}
}
Roy's is similar to mine, but here's what I was thinking. I'll give you what I wrote in C# and how I think it translates to Javascript.
C#
int num = 10;
bool reverse = true;
for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
{
Console.Write((reverse ? i - 1 : i).ToString());
}
Console.ReadKey();
Javascript
var num = 10,
reverse = true;
for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
{
console.log(reverse ? i - 1 : i);
}
And here's another way
Javascript
var num = 10,
reverse = false;
for (int i = 0; i < num; i++)
{
console.log((reverse ? abs(-num + (i + 1)) : i));
}
It seems to work:
var num = 10;
var z = 1;
var k = -10;
if (reverse ){
k = -1;
z = -1;
}
for(int i = 0; i < 10; i++){
console.log(num+i*z+k);
}
Surely in a language like Javascript there must be a way to define a local function and use that in the loop?
function SubtractFrom(val, subtractor) {
return val - subtractor;
}
function PassThrough(val) {
return val;
}
var num = 10;
var processor = reverse ? SubtractFrom(num-1) : PassThrough;
for (i = 0; i < num; i++) {
console.log(processor(i));
}
Not knowing Javascript though, I don't know what actual form the function definitions would take.
//reverse a number
let c = [2,3,5,67,4]
let sum = '';
let d = c.toString().split('')
console.log(d)
for(let i = d.length-1; i >= 0; i--) {
sum += d[i]
}
console.log(sum)

Categories