add sum of previous to array - javascript - javascript

In a multi-dimensional array, how can I add a new dimension that is the sum of all of previous values of one of the other dimensions?
What I have:
var myarray = [[5,"a"],[10,"a"],[3,"a"],[2,"a"]];
What I want:
var newArray = [[5,"a",5],[10,"a",15],[3,"a",18],[2,"a",20]];
I am trying to turn this example (second answer, second code block):
var myarray = [5, 10, 3, 2];
var result = myarray.reduce(function(r, a) {
if (r.length > 0)
a += r[r.length - 1];
r.push(a);
return r;
}, []);
// [5, 15, 18, 20]
And apply it like this:
var myarray = [[5,"a"],[10,"a"],[3,"a"],[2,"a"]];
var result = myarray.reduce(function(r, a) {
if (r.length > 0)
a += r[0][r.length - 1];
r[2].push(a);
return r;
}, []);
// [[5,"a",5],[10,"a",15],[3,"a",18],[2,"a",20]];
But I can't find an effective way to separate out the first value in order to run reduce on it. I tried foreach, but that isn't returning an array:
var firstValue = myarray.forEach(function(value, index) {
console.log(value[0]);
})
So, I'm thinking take that foreach output and turn it into an array somehow to operate on, then push it back in. But it's seeming very convoluted. Can anyone point me in the right direction?
Thanks.

For reducing the array and only pulling the first element from each array, you actually need to build an array with the foreach loop.
Create an array outside the loop and use array.push(value) to build the other array.
var firstValues = [];
myarray.forEach(function(value, index) {
firstValues.push(value);
});
My preferred solution:
Since you're already looping through the array, you can instead do a lot of the logic within just one loop.
var currentSum = 0;
myarray.forEach(function(value, index) {
currentSum += value[0]; //add to the running sum
value.push(currentSum); //append the running sum to the end of this inner array
});
Here's a demo via jsfiddle

a simple for loop should do it
var sum = 0;
for (var i = 0; i < myarray.length; i++) {
sum = sum + (myarray[i][0]);
myarray[i].push(sum);
}
console.log(myarray);
Fiddle link

Related

javascript weird console logs

I've got a assignment to make function that gives you a sorted array with 6 random numbers from 1 to 45. None of the array values should equal to one another.
I thought about a solution that would work in Java but the JavaScript console logs I get are pretty confusing. Could anyone help me out?
"use strict";
var numbers = [];
for(var x = 1; x <46;x++){
numbers.push(x);
}
function LottoTipp(){
var result = [];
for(var i = 0; i <6; i++){
var randomNum = Math.round(Math.random()* 45);
var pushed = numbers[randomNum];
result.push(pushed);
numbers.splice(randomNum)
}
return console.log(result) + console.log(numbers);
}
LottoTipp();
the console logs
[ 34, 7, undefined, undefined, undefined, undefined ]
[ 1, 2, 3, 4, 5, 6 ]
There were three problems:
If you want to remove one item of an array you have to splice it by the items index and give a deletecount.
In your case: numbers.splice(randomNum, 1);
You have to use Math.floor instead of Math.round, because Math.floor always down to the nearest integerer, while Math.round searches for the nearest integer wich could be higher than numbers.length.
After removing one item the length of the array has been changed. So you have to multiply by numbers.lenght instead of 45.
In your case: var randomNum = Math.floor(Math.random()* numbers.length);
"use strict";
var numbers = [];
for(var x = 1; x < 46; x++){
numbers.push(x);
}
function LottoTipp(){
var result = [];
for(var i = 0; i < 6; i++){
var randomNum = Math.floor(Math.random()* numbers.length);
var pushed = numbers[randomNum];
result.push(pushed);
numbers.splice(randomNum, 1);
}
return console.log(result) + console.log(numbers);
}
LottoTipp();
If you only want an array with random unique numbers I would suggest doing it like this:
<script>
var array = [];
for(i = 0; i < 6; i++) {
var number = Math.round(Math.random() *45);
if(array.indexOf(number) == -1) { //if number is not already inside the array
array.push(number);
} else { //if number is inside the array, put back the counter by one
i--;
}
}
console.log(array);
</script>
There is no issue with the console statement the issue is that you are modifying the numbers array in your for loop. As you are picking the random number between 1-45 in this statement:-
var randomNum = Math.round(Math.random()* 45);
and you expect that value would be present in the numbers array at that random index. However you are using array.splice() and providing only first parameter to the function. The first parameter is the start index from which you want to start deleting elements, find syntax here. This results in deleting all the next values in the array.Therefore if you pick a random number number say.. 40, value at numbers[40] is undefined as you have deleted contents of the array.
if you want to generate unique set of numbers follow this post.
hope it helps!
Just add the number in the result if it is unique otherwise take out a new number and then sort it. Here is an implementation:
let result = []
while(result.length < 6) {
let num = Math.round(Math.random() * 45);
if(!result.includes(num)) {
result.push(num);
}
}
result.sort((a,b) => {
return parseInt(a) - parseInt(b);
});
console.log(result);

javascript function that takes array as input, increases each array element by 1 and returns new array with increased values not working

I'm totally new to javascript. I'm learning how to write functions for the first time and I'm stuck with this one. Please can you help me figure out why my for loop is only looping on the first element of the array and not the others.
Thank you for your help.
See code:
I'm trying to write a function that takes as input an array of numbers and returns a new array that contains each number from the input array, increased by one.
function incrementEach (myArray) {
var newArray = [];
for (var i = 0; i < myArray.length; i++) {
newArray.push(myArray + 1);
return newArray;
};
};
var nuArray = incrementEach ([23, 34, 56, 67]);
log (nuArray);
The result I get after running this code is: '[24]'
Only the first element in the array is increased and printed. Something must be wrong with my loop but I can't figure it out. Please help me!
Your code has a couple of problems:
In your newArray.push call, you're pushing the entire array + 1 instead of the individual item + 1.
You're returning within your loop, which means it is not a loop at all and only gets run once.
Here is your code with fixes in place:
function incrementEach (myArray) {
var newArray = [];
for (var i = 0; i < myArray.length; i++) {
newArray.push(myArray[i] + 1);
};
return newArray;
};
var nuArray = incrementEach ([23, 34, 56, 67]);
log(nuArray);
You could also use the map function to do this with much less code:
var nuArray = [23, 34, 56, 67].map(function(item) {
return item + 1;
});
You need to grab the element from the array:
newArray.push(myArray[i] + 1);
You probably want
function incrementEach (myArray) {
var newArray = [];
for (var i = 0; i < myArray.length; ++i) {
newArray.push(myArray[i] + 1); // i-th element
}
return newArray; // Return at the end
}
However, it would be simpler like this:
[23, 34, 56, 67].map(n => n+1);
var arr
function incrementEach (myArray) {
for (var i = 0; i < myArray.length; i++) {
myArray[i] += 1
};
return myArray
};
arr = incrementEach([23, 34, 56, 67]);
log(arr);
Best use case for .map(). Which creates a new array with the results of calling a provided function on every element in this array.
var increased = [1, 2, 3].map(function(item, index) {
return item + 1;
});
A function could look like:
function increaseValues(arr, amount) {
return arr.map(function(item) {
return item + amount;
});
}
// increase array values by 5
increaseValues([1,2,4], 5);
You are accessing the array itself not the item being looped.
use newArray.push(myArray[i] + 1); instead of newArray.push(myArray + 1); inside the loop
Also, the return statement should be outside the for loop
DEMO

Get all elements of array with same (highest) occurrence

I have an array like [1,4,3,1,6,5,1,4,4]
Here Highest element frequency is 3 ,I need to select all elements from array that have 3 frequency like [1,4] in above example.
I have tried with this
var count = {},array=[1,4,3,1,6,5,1,4,4],
value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (value in count) {
count[value]++;
} else {
count[value] = 1;
}
}
console.log(count);
this will output array element with their frequency , now i need all elements that have highest frequency.
I'd approach this problem as follows.
First, write down how you think the problem can be solved IN ENGLISH, or something close to English (or your native language of course!). Write down each step. Start off with a high-level version, such as:
Count the frequency of each element in the input.
Find the highest frequency.
and so on. At this point, it's important that you don't get bogged down in implementation details. Your solution should be applicable to almost any programming language.
Next flesh out each step by adding substeps. For instance, you might write:
Find the highest frequency.
a. Assume the highest frequency is zero.
b. Examine each frequency. If it is higher than the current highest frqeuency, make it the current highest frequency.
Test your algorithm by executing it manually in your head.
Next, convert what you have written about into what is sometimes called pseudo-code. It is at this point that our algorithm starts to look a little bit like a computer program, but is still easily human-readable. We may now use variables to represent things. For instance, we could write "max_freq ← cur_freq". We can refer to arrays, and write loops.
Finally, convert your pseudo-code into JS. If all goes well, it should work the first time around!
In recent years, a lot of people are jumping right into JavaScript, without any exposure to how to think about algorithms, even simple ones. They imagine that somehow they need to be able to, or will magically get to the point where they can, conjure up JS out of thin air, like someone speaking in tongues. In fact, the best programmers do not instantly start writing array.reduce when confronted with a problem; they always go through the process--even if only in their heads--of thinking about the approach to the problem, and this is an approach well worth learning from.
If you do not acquire this skill, you will spend the rest of your career posting to SO each time you can't bend your mind around a problem.
A proposal with Array.prototype.reduce() for a temporary object count, Object.keys() for getting the keys of the temporary object, a Array.prototype.sort() method for ordering the count results and Array.prototype.filter() for getting only the top values with the most count.
Edit: Kudos #Xotic750, now the original values are returned.
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4],
result = function () {
var temp = array.reduce(function (r, a, i) {
r[a] = r[a] || { count: 0, value: a };
r[a].count++;
return r;
}, {});
return Object.keys(temp).sort(function (a, b) {
return temp[b].count - temp[a].count;
}).filter(function (a, _, aa) {
return temp[aa[0]].count === temp[a].count;
}).map(function (a) {
return temp[a].value;
});
}();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Bonus with a different attempt
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4],
result = array.reduce(function (r, a) {
r.some(function (b, i) {
var p = b.indexOf(a);
if (~p) {
b.splice(p, 1);
r[i + 1] = r[i + 1] || [];
r[i + 1].push(a);
return true;
}
}) || (
r[1] = r[1] || [],
r[1].push(a)
);
return r;
}, []).pop();
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
you can try this
var input = [1,4,3,1,6,5,1,4,4];
var output = {};
for ( var counter = 0; counter < input.length; counter++ )
{
if ( !output[ input[ counter ] ] )
{
output[ input[ counter ] ] = 0;
}
output[ input[ counter ] ]++;
}
var outputArr = [];
for (var key in output)
{
outputArr.push([key, output[key]])
}
outputArr = outputArr.sort(function(a, b) {return b[1] - a[1]})
now initial values of outputArr are the ones with highest frequency
Here is the fiddle
Check this updated fiddle (this will give the output you want)
var input = [1,4,3,1,6,5,1,4,4];
var output = {}; // this object holds the frequency of each value
for ( var counter = 0; counter < input.length; counter++ )
{
if ( !output[ input[ counter ] ] )
{
output[ input[ counter ] ] = 0; //initialized to 0 if value doesn't exists
}
output[ input[ counter ] ]++; //increment the value with each occurence
}
var outputArr = [];
var maxValue = 0;
for (var key in output)
{
if ( output[key] > maxValue )
{
maxValue = output[key]; //find out the max value
}
outputArr.push([key, output[key]])
}
var finalArr = []; //this array holds only those keys whose value is same as the highest value
for ( var counter = 0; counter < outputArr.length; counter++ )
{
if ( outputArr[ counter ][ 1 ] == maxValue )
{
finalArr.push( outputArr[ counter ][ 0 ] )
}
}
console.log( finalArr );
I would do something like this. It's not tested, but it's commented for helping you to understand my approach.
// Declare your array
var initial_array = [1,4,3,1,6,5,1,4,4];
// Declare an auxiliar counter
var counter = {};
// Loop over the array
initial_array.forEach(function(item){
// If the elements is already in counter, we increment the repetition counter.
if counter.hasOwnProperty(item){
counter[item] += 1;
// If the element is not in counter, we set the repetitions to one
}else{
counter[item] = 1;
}
});
// counter = {1 : 3, 4 : 3, 3 : 1, 6 : 1, 5 : 1}
// We move the object keys to an array (sorting it's more easy this way)
var sortable = [];
for (var element in counter)
sortable.push([element, counter[element]]);
// sortable = [ [1,3], [4,3], [3,1], [6,1], [5,1] ]
// Sort the list
sortable.sort(function(a, b) {return a[1] - b[1]})
// sortable = [ [1,3], [4,3], [3,1], [6,1], [5,1] ] sorted, in this case both are equals
// The elements in the firsts positions are the elements that you are looking for
// This auxiliar variable will help you to decide the biggest frequency (not the elements with it)
higgest = 0;
// Here you will append the results
results = [];
// You loop over the sorted list starting for the elements with more frequency
sortable.forEach(function(item){
// this condition works because we have sorted the list previously.
if(item[1] >= higgest){
higgest = item[1];
results.push(item[0]);
}
});
I'm very much with what #torazaburo had to say.
I'm also becoming a fan of ES6 as it creeps more and more into my daily browser. So, here is a solution using ES6 that is working in my browser now.
The shims are loaded to fix browser browser bugs and deficiencies, which is recommended in all environments.
'use strict';
// Your array of values.
const array = [1, 4, 3, 1, 6, 5, 1, 4, 4];
// An ES6 Map, for counting the frequencies of your values.
// Capable of distinguishing all unique values except `+0` and `-0`
// i.e. SameValueZero (see ES6 specification for explanation)
const frequencies = new Map();
// Loop through all the `values` of `array`
for (let item of array) {
// If item exists in frequencies increment the count or set the count to `1`
frequencies.set(item, frequencies.has(item) ? frequencies.get(item) + 1 : 1);
}
// Array to group the frequencies into list of `values`
const groups = [];
// Loop through the frequencies
for (let item of frequencies) {
// The `key` of the `entries` iterator is the value
const value = item[0];
// The `value` of the `entries` iterator is the frequency
const frequency = item[1];
// If the group exists then append the `value`,
// otherwise add a new group containing `value`
if (groups[frequency]) {
groups[frequency].push(value);
} else {
groups[frequency] = [value];
}
}
// The most frequent values are the last item of `groups`
const mostFrequent = groups.pop();
document.getElementById('out').textContent = JSON.stringify(mostFrequent);
console.log(mostFrequent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.4.1/es5-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.34.0/es6-shim.js"></script>
<pre id="out"></pre>
you can do like this to find count occurrence each number
var array = [1, 4, 3, 1, 6, 5, 1, 4, 4];
var frequency = array.reduce(function(sum, num) {
if (sum[num]) {
sum[num] = sum[num] + 1;
} else {
sum[num] = 1;
}
return sum;
}, {});
console.log(frequency)
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

Arrange array like gaussian function (max values in middle, min values in edges)

How can I arrange an array like a gaussian function, meaning max values in the middle, min values in edges?
e.g.
var Array = [5,2,7,4,1]
will output the following array:
[1,4,7,5,2]
I didn't used underscore functions but you can use equivalent function from underscore/lodash to shorten code.
Steps:
Sort the array in descending order
Iterate over array and add the elements from sorted array at the start and end alternately
var arr = [5, 2, 7, 4, 1];
var sortedArr = arr.sort(function(a, b) {
return b - a;
});
var gaussianArr = [];
sortedArr.forEach(function(e, i) {
if (i % 2) {
gaussianArr.push(e);
} else {
gaussianArr.unshift(e);
}
});
console.log(gaussianArr);
document.write(gaussianArr);
Want underscore solution?
Here you go. fiddle. You won't see much difference between Vanilla JS solution and underscore solution(as the logic is same, only different syntax).
Here is the logic.
function gSort(arr) {
var _a = arr.slice()
_a.sort(function(a,b){return a-b});
_a.reverse();
var _isstart = false;
var _out = [];
for (var i = 0; i < _a.length; i++) {
if (i%2) {
_out.push(_a[i])
}else{
_out.splice(0,0,_a[i]); //You can use _out.unshift(_a[i]); also
}
}
return _out;
}
var array = [5,2,7,4,1]
console.log(gSort(array));

Fill empty array

I need to iterate from 0 to 30, but I want to do this with help of forEach:
new Array(30).forEach(console.log.bind(console);
Of course this does not work, therefor I do:
new Array(30).join(',').split(',').forEach(console.log.bind(console));
Is there other ways to fill empty arrays?
Actually, there's a simple way to create a [0..N) (i.e., not including N) range:
var range0toN = Object.keys(Array.apply(0,Array(N)));
Apparently Object.keys part can be dropped if you only want to get a proper array of N elements.
Still, like others said, in this particular case it's probably better to use for loop instead.
if you want all of item have same value, do this
var arrLength = 4
var arrVal = 0
var newArr = [...new Array(arrLength)].map(x => arrVal);
// result will be [0, 0, 0, 0]
You could try using a for loop. new Array is not a best practise
var index, // we use that in the for loop
counter, // number of elements in array
myArray; // the array you want to fill
counter = 30;
myArray = [];
for (index = 0; index < counter; index += 1) {
myArray[index] = [];
/*
// alternative:
myArray.push([]);
// one-liner
for (index = 0; index < counter; index += 1) myArray.push([]);
*/
}
If you simply want to iterate, then use for loop like this
for (var i = 0; i < 30; i += 1) {
...
...
}
Actually, if you are looking for a way to create a range of numbers, then you can do
console.log(Array.apply(null, {length: 30}).map(Number.call, Number));
It will create numbers from 0 to 29. Source : Creating range in JavaScript - strange syntax
If you insist foreach
var data = [1, 2, 3];
data.forEach(function(x) {
console.log(x);
});

Categories

Resources