javascript: combine array of arrays into an array of single multidimensional array - javascript

I have N number of arrays in an array, for now N = 3
[[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8]...]
I would like for it to return
[[1,3,6], [2,4,7], [3,5,8], ...., ["a","c", ""], ["b", "",""]]
There are lot of solutions of combining two arrays but I want to handle any number of arrays. I would like for it to return above. I am looking to do this without underscore or jquery.
Values=[];
status = [[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8]];
status_name = status[0]; //take the longest one always
for (i = 0; i < status_name.length; ++i)
{
Values[i] =Array(status_name[i], status[i+1][i]);
}

why not nest loops ?
sth. like this warning pseudocode
Values=[];
status = [[1,2,3, "a","b"] , [3,4,5,"c"], [6,7,8];
status_name = status[0];
for (i = 0; i < status.length; ++i)
{
for (k = 0; k < status[i].length; ++i)
{
Values[i] =Array(status[i][k], status[k+1][k]);
}
}

var originalArray = [[1, 2, 3, "a", "b"], [3, 4, 5, "c"], [6, 7, 8]];
var theValues = new Array();
var actualArray = originalArray.pop();
var maxLength = originalArray.length;
while (actualArray) {
for (var i = 0; i < actualArray.length; i++){
var newArray = theValues[i];
if (!newArray) {
theValues[i] = new Array();
}
theValues[i].push(actualArray[i])
}
actualArray = originalArray.pop();
}
for (var i = 0; i < theValues.length; i++) {
for (var j = maxLength - theValues[i].length; j >= 0; j--) {
theValues[i].push("");
}
}

Conceptually, this is most easily achieved using nested loops;
var a = [[1, 2, 'x'], ['a', 'b'], ['+', '-', 'y', 'z']]; // input array
function transform(a) {
var i, j, // two loops = two variables to iterate with
foundSomething, // a flag so we know to stop
b = []; // output array
i = 0;
do { // loop for i
foundSomething = false;
b[i] = [];
for (j = 0; j < a.length; ++j) { // loop for j
if (i in a[j]) { // something exists at this level of i
foundSomething = true;
b[i].push(a[j][i]);
} else { // nothing here
b[i].push(''); // insert empty string
}
}
++i;
} while (foundSomething);
b.length = b.length - 1; // snip empty end
return b;
}
transform(a); // [[1, "a", "+"], [2, "b", "-"], ["x", "", "y"], ["", "", "z"]]
Sorting required?
a.sort(function (a, b) {return a.length < b.length;}); // bigger arrays first
transform(a); // [["+", 1, "a"], ["-", 2, "b"], ["y", "x", ""], ["z", "", ""]]
If you're always doing this sort, you can further optimise transform i.e. because you know that a[x].length >= a[y].length for all x < y, you don't need to flag whether you found something

Related

Javascript: Turn a nested array into a single array using a nested for loop

This is just a simple javascript exercise that I'm working on.
I'm trying to convert this array...
var array = [
[1,2],
[3,4],
[5,6]
];
into...
array = [1, 2, 3, 4, 5, 6];
by using this nested for loop.
var series;
var storage = [];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};
console.log(storage);
With an output of...
//Output: [6, 6, 6, 6, 6, 6]
Why is this the output and how can I fix it?
for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element
var array = [
[1, 2],
[3, 4],
[5, 6]
];
var series;
var storage = [];
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
storage.push(array[i][j])
};
};
console.log(storage);
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
Seriously, here you set the same value to each element of the resulting array.
You probably need something like
for(let x of array) {
for(let y of x) {
storage.push(y)
}
}
Or, if your JS machine is experimental enough, simply
var storage = array.flat()
You can use a mix of reduce and concat to achieve what you want in one line
var array = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(array.reduce((a, v) => a.concat(v), []));
As for why your code didn't work, it's mainly down to this bit
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
It would overwrite everything in the array with the last value of series, which in your case would be 6
Array.concat can do this on its own
var merged = [].concat.apply([], array);
var array = [
[1,2],
[3,4],
[5,6]
];
var newArray = [];
for (let i = 0; i < array.length; i++) {
newArray = newArray.concat(array[i]);
}
console.log(newArray)
You can use array.flat()
Reference : flatten
var array = [
[1,2],
[3,4],
[5,6]
];
array.flat();
// 1,2,3,4,5,6....
You could use the ES6 spread syntax like this:
for (let element of array){
storage.push( ... el )
}
storage = [];
for(var i=0; i<array.length; i++)
storage = storage.concat(array[i]);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

How to find non repeated numbers in an Array using JavaScript?

Dear all I'm trying to find non repeated value in an array using javascript.I have written some code but it not working properly ..can you guys tell me where is the problem.thanks.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var n = arr.length;
var result = '';
function nonrep() {
for (var i = 0; i < n; i++) {
var j;
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j]) {
result = arr[i];
break;
}
if (j == n)
return arr[i];
}
return result;
}
console.log(nonrep())
There is possibly a more neat approach to this solution, but this works as expected by filtering the array and compare it's current value with the array items itself (expect current item's index value).
const sampleArray = [1,2,3,7,2,1,3];
const getNonDuplicatedValues = (arr) =>
arr.filter((item,index) => {
arr.splice(index,1)
const unique = !arr.includes(item)
arr.splice(index,0,item)
return unique
})
console.log("Non duplicated values: " , ...getNonDuplicatedValues(sampleArray))
Some changes:
Move all variable declarations inside of the function.
Use a function parameter for the handed over array, keep the function pure.
Declare all needed variables at top of the function in advance.
Take an array as result array unique.
Check i and j and if equal continue the (inner) loop.
Check the value at i and j and exit the (inner) loop, because a duplicate is found.
Take the check at the end of the inner loop and check the index j with the length of the array l, and if equal push the value to unique.
Use a single return statement with unique array at the end of the outer loop.
function getUnique(array) {
var l = array.length,
i, j,
unique = [];
for (i = 0; i < l; i++) {
for (j = 0; j < l; j++) {
if (i === j) {
continue;
}
if (array[i] === array[j]) {
break;
}
}
if (j === l) {
unique.push(array[i]);
}
}
return unique;
}
console.log(getUnique([-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3]));
Another solution could be to check if indexOf and lastIndexOf returns the same value. Then you found a unique value.
var array = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3],
unique = array.filter((v, i) => array.indexOf(v) === array.lastIndexOf(v));
console.log(unique);
You could first use reduce to get one object with count for each number element and then filter on Object.keys to return array of non-repeating numbers.
var arr=[-1,2,5,6,2,9,-1,6,5,-1,3];
var obj = arr.reduce((r, e) => (r[e] = (r[e] || 0) + 1, r), {});
var uniq = Object.keys(obj).filter(e => obj[e] == 1).map(Number)
console.log(uniq)
Solution with for loop.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var uniq = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] == arr[j] && i != j) break;
else if (j == arr.length - 1) uniq.push(arr[i])
}
}
console.log(uniq)
Another simple approach
var arr = [1,1,2,3,3,4,4,5];
let duplicateArr = [];
var repeatorCheck = (item) => {
const currentItemCount = arr.filter(val => val=== item).length;
if(currentItemCount > 1) duplicateArr.push(item);
return currentItemCount;
}
var result = arr.filter((item,index) => {
var itemRepeaterCheck = !duplicateArr.includes(item) && repeatorCheck(item);
if(itemRepeaterCheck === 1){
return item;
}
});
console.log(result);
let arr = [1, 2, 1, 3, 3, 5];
function nonRepeatableNo(arr) {
let val = []
for (let i = 0; i < arr.length; i++) {
let count = 0;
for (let j = 0; j < arr.length; j++) {
if (arr[i] === arr[j]) {
count += 1
}
}
if (count === 1) {
val.push(arr[i])
}
}
console.log(val)
}
nonRepeatableNo(arr)
const arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
const non_repeating = arr.filter(num => arr.indexOf(num) === arr.lastIndexOf(num))
console.log(non_repeating)
Filtering only unique elements according to OP request:
This uses for loops, as requested. It returns an array containing only elements appearing once in the original array.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var n = arr.length;
var result = [];
function nonrep() {
for (var i = 0; i < n; i++) {
for (var j=0 ; j < n; j++)
if (i!=j && arr[i]==arr[j])
break;
if(j==n)
result.push(arr[i]);
}
return result;
}
console.log(nonrep())
var arr1 = [45, 4,16,25,45,4,16, 9,7, 16, 25];
var arr=arr1.sort();
console.log(arr);
var str=[];
arr.filter(function(value){
if( arr.indexOf(value) === arr.lastIndexOf(value))
{ str.push(value);
console.log("ntttttttttttttnnn" +str)
}// how this works ===============A
})
O/P
7,9
Please try the below code snippet.
var arr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
var uniqArr = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr.length; j++) {
if (arr[i] == arr[j] && i != j) break;
else if (j == arr.length - 1){
uniqArr.push(arr[i])
}
}
}
console.log(uniqArr)
this ES6 code worked for me :
a.map(c=>a.filter(b=>c==b)).filter(e=>e.length<2).reduce((total, cur)=> total.concat(cur), [])
Here is a working method with loops.
var arr = [-1,2,5,6,2,9,-1,6,5,-1,3];
var len = arr.length;
const result = arr
.filter(value=>{
var count=0;
for(var i=0;i<len;i++)
{
if(arr[i]===value)
count++;
}
return count===1;
})
console.log(result);
const sampleArr = [-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3];
function getUnique(arr){
const result=[]
const obj={}
for(let i=0;i<arr.length;i++){
if(!obj[arr[i]]){
obj[arr[i]]=true
result.push(arr[i])
}else{
const index= result.indexOf(arr[i])
if(index!==-1){
result.splice(result.indexOf(arr[i]),1)
}
}
}
return result
}
const uniqueArr= getUnique(sampleArr)
console.log(uniqueArr)
Here is the solution..
var x = [1,1,2,3,2,4]
var res = []
x.map(d => {
if(res.includes(d)) {
// remove value from array
res = res.filter((a) => a!=d)
} else {
// add value to array
res.push(d)
}
})
console.log(res) // [3,4]
//without using any filter also with minimum complexity
const array = [1 , 2, 3, 4, 2, 3, 1, 6, 8,1,1 ];
const unique = new Set();
const repetedTreses = new Set();
for(let i=0; i<array.length; i++) {
if(!unique.has(array[i]) && !repetedTreses.has(array[i])){
unique.add(array[i]);
}else{
repetedTreses.add(array[i]);
unique.delete(array[i]);
}
}
let uniqueElements=[...unique];
console.log(uniqueElements);
You can use filter and indexOf for that:
console.log(
[-1, 2, 5, 6, 2, 9, -1, 6, 5, -1, 3].filter((v, i, a) => a.indexOf(v, i + 1) === -1 )
);

Sort an array containing numbers using a 'for' loop

I am new to JavaScript, and I have an array which contains numbers.
var arr = [2,4,8,1,5,9,3,7,6];
How can I sort it using a native for loop in JavaScript?
I know sort function is available, but I want it through for loop.
The output should be:
var res = [1,2,3,4,5,6,7,8,9];
var Arr = [1, 7, 2, 8, 3, 4, 5, 0, 9];
for (var i = 1; i < Arr.length; i++)
for (var j = 0; j < i; j++)
if (Arr[i] < Arr[j]) {
var x = Arr[i];
Arr[i] = Arr[j];
Arr[j] = x;
}
console.log(Arr);
I would do something like this...
var input = [2,3,8,1,4,5,9,7,6];
var output = [];
var inserted;
for (var i = 0, ii = input.length ; i < ii ; i++){
inserted = false;
for (var j = 0, jj = output.length ; j < jj ; j++){
if (input[i] < output[j]){
inserted = true;
output.splice(j, 0, input[i]);
break;
}
}
if (!inserted)
output.push(input[i])
}
console.log(output);
Maybe there are more efficient ways, but if you want to use the for loop, it's my first idea...
First create an empty array where the sorted numbers will be pushed into.
let sorted = [];
Secondly, create a very large amount of numbers that none of the numbers of the array can match. This number will be used for the very first comparison to determine which number of the array is smaller.
let comparison = 9000000000;
Create a for loop.
This loop will have another loop inside of it. The inner loop will check for the smallest number in a given array, and once the smallest number is gotten, it will be push into the empty array we created. The smallest number will also be removed from the initial array and then the array will run again.
for(a = 0; a < arr.length; a++){
//This inner loop fetches the smallest number.
for(b = 0; b < arr.length; a++){
if(comparison > arr[b]){
comparison = arr[b];
}
}
// The smallest number is assigned to comparison
// Now it being pushed to the empty array
sorted.push(comparison);
// Remove the smallest number from the initial array
let indexOfSmallNumber = arr.indexOf(comparison);
arr.splice(indexOfSmallNumber, 1);
// Set the comparison back to 9000000000;
comparison = 90000000000;
a = -1;
// Here, "a" is our main loop index counter and we are
// setting it to -1 because we don't want it to change
// to 2 by default, doing this will make the loop run
// forever until the initial array is empty.
}
let arr = [4, 2, 5, 1]
let temp;
function converter(arr) {
for(let i=0; i<arr.length; i++) {
for (let j=i+1; j<arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
return arr
}
const newArr = converter(arr)
console.log(newArr)
Use:
let s = [4, 6, 3, 1, 2];
for (let i = 0; i < s.length;) {
if (s[i] > s[i + 1]) {
let a = s[i];
s[i] = s[i + 1];
s[i + 1] = a;
i--;
}
else {
i++;
}
}
This is a sorting algorithm which has a best time complexity of O(n) and the worst time of O(n^2).
This code checks for each number, and then compares to all numbers on the left side.
To check the time it takes each code to run, you can also use this code below:
let start = process.hrtime.bigint()
let end = process.hrtime.bigint()
console.log(end - start) // This measures the time used in nano seconds.
Also for microseconds, you can use this performance.now().
Here there is a very simple solution that uses a temporary array to store the values greater than the current one. Then it puts the current value between the lesser and the greater values:
var arr = [2,4,8,1,5,9,3,7,6];
var res = [];
for (const c of arr) {
let tmp = [];
while (c < res[res.length-1]) {
tmp.unshift(res.pop());
}
res = [...res, c, ...tmp];
}
const numberArr = [5, 9, 2, 8, 4, 10, 1, 3, 7, 6];
function sortedFunction(arr) {
let sortedArr = [];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
let n = 0;
if (arr[i] > arr[j]) {
n = arr[i];
arr[i] = arr[j];
arr[j] = n;
}
}
sortedArr.push(arr[i]);
}
return sortedArr;
}
sortedFunction(numberArr);
Under the JavaScript array sort section of W3Schools it talks about how to compare a value in an array with the others and then order them based on the values being returned. I updated the code to use a for loop to sort values.
// Ascending points
var points = [5.0, 3.7, 1.0, 2.9, 3.4, 4.5];
var output = [];
var i;
for (i = 0; i < points.length; i++) {
points.sort(function (a, b) {
return a - b
});
output += points[i] + "<br>";
}
console.log(output);
// Descending points
var points = [5.0, 3.7, 1.0, 2.9, 3.4, 4.5];
var output = [];
var i;
for (i = 0; i < points.length; i++) {
points.sort(function (a, b) {
return b - a
});
output += points[i] + "<br>";
}
console.log(output);
const array = [12, 3, 45, 61, 23, 45, 6, 7];
function sortArray(array) {
for (var i = 0; i < array.length; ++i) {
for (var j = 0; j < array.length - 1 - i; ++j) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
}
}
}
return array;
}
console.log(sortArray(array));
Here are the two solutions for the same algorithm:
Solution 1:
We can directly use JavaScript functions:
let arr = [2, 4, 8, 1, 5, 9, 3, 7, 6]
const changeOrder = (arr) => {
return arr.sort((a, b) => a - b)
}
let result = changeOrder(arr);
console.log(result) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Solution 2:
We can use a JavaScript for loop for doing the same
let arr = [2, 4, 8, 1, 5, 9, 3, 7, 6]
const changeOrder = (arr) => {
for(let i=1; i< arr.length; i++) {
for(let j=0; j < i; j++) {
if(arr[i] < arr[j]) {
let x = arr[i]
arr[i] = arr[j]
arr[j] = x
}
}
}
return arr;
}
let result = changeOrder(arr);
console.log(result) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
An improvement to previous answer
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

Javascript: Bubble Sort

I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code:
function bubble(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
var a = arr[i]
var b = arr[i + 1]
arr[i] = b
arr[i + 1] = a
}
}
return arr;
}
Another bubble sort implementation:
const bubbleSort = array => {
const arr = Array.from(array); // avoid side effects
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
};
You need an inner loop to complete the sort correctly:
function bubble(arr) {
var len = arr.length;
for (var i = 0; i < len ; i++) {
for(var j = 0 ; j < len - i - 1; j++){ // this was missing
if (arr[j] > arr[j + 1]) {
// swap
var temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
}
}
}
return arr;
}
document.write(bubble([1,9,2,3,7,6,4,5,5]));
Please look at the following sequence:
[5, 4, 3, 2, 1]
Now lets say you need to sort this in the ascending order using bubble sort.
So, you iterate the array and swap adjacent elements which are ordered otherwise.
Here is what you will get after the completion of the iteration
[4, 3, 2, 1, 5]
Now if you do this another time, you will get this:
[3, 2, 1, 4, 5]
Likewise, you need to repeat the iteration enough times to get it sorted fully. This means you need 2 nested loops. The inner loop is to iterate the array and the outer loop is to repeat the iteration.
Please see the step-by-step example of this article.
const bubbleSort = (array)=>{
let sorted = false;
let counter =0;
while(!sorted){
sorted = true;
for(let i =0; i < array.length -1 -counter; i++){
if(array[i] > array[i+1]){
helper(i,i+1,array);
sorted = false;
}
}
counter++;
}
return array;
}
//swap function
function helper(i,j, array){
return [array[i],array[j]] = [array[j],array[i]]
}
let array=[8,5,2,9,5,6,3];
console.log(bubbleSort(array))
var array = [6,2,3,7,5,4,1];
function bubbleSort(arr) {
for(let j=0;j<arr.length;j++) {
for(let i = 0; i < arr.length; i++) {
if(arr[i]>arr[i+1] && (i+1 < arr.length)) {
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(array));
My bubble sort with just a while loop :
function bubbleSort(arr){
var sorted = false
while (!sorted){
sorted = true;
arr.forEach(function (element, index, array){
if (element > array[index+1]) {
array[index] = array[index+1];
array[index+1] = element;
sorted = false;
}
});
}
}
function bubble(arr) {//You need Two Loops for Bubble sort
for (var i = 0; i < arr.length; i++) {//Outer Loop
for(var j=0; j < arr.length - 1; j++){//Inner Loop
if (arr[j] > arr[j + 1]) {
var a = arr[j]
var b = arr[j + 1]
arr[j] = b
arr[j + 1] = a
}
}
}
return arr;
}
Another form of bubble sort includes starting at the end of the array and placing the smallest element first and going till the largest. This is the code:
function bubbleSort(items) {
var length = items.length;
for (var i = (length - 1); i >= 0; i--) {
//Number of passes
for (var j = (length - i); j > 0; j--) {
//Compare the adjacent positions
if (items[j] < items[j - 1]) {
//Swap the numbers
var tmp = items[j];
items[j] = items[j - 1];
items[j - 1] = tmp;
}
}
}
}
Note Bubble sort is one of the slowest sorting algorithms.
It works for me. I commented the code for more understanding
bubbleSort = (numbersArray) => {
const arrayLenght = numbersArray.length;
for (let i = 0; i < arrayLenght; i++) {
for(let j = 0; j < arrayLenght; j++) {
// Print only to debug
// console.log(`i: ${i} - j: ${j}`);
// console.log(`numbersArray[i]: ${numbersArray[i]} | numbersArray[j]: ${numbersArray[j]}`);
// Check if current number is greater than the next number
if (numbersArray[j] > numbersArray[j + 1]) {
// Store current value to generate swap
const currentNumber = numbersArray[j];
// Now the current position get value of the next position
// And de next position get value of the current position
numbersArray[j] = numbersArray[j + 1];
numbersArray[j + 1] = currentNumber;
}
}
}
// Debug: Print the sorted array
console.log(`sorted array: ${numbersArray.toString()}`);
}
const numbers = [
[3, 10, 5, 7],
[8, 5, 2, 9, 5, 6, 3],
[4, 50, 28, 47, 9, 2097, 30, 41, 11, 3, 68],
[3, 10, 5, 7, 8, 5, 2, 9, 5, 6, 3]
];
numbers.forEach(element => {
bubbleSort(element);
});
Output:
sorted array: 3,5,7,10
sorted array: 2,3,5,5,6,8,9
sorted array: 3,4,9,11,28,30,41,47,50,68,2097
sorted array: 2,3,3,5,5,5,6,7,8,9,10
var arr = [5, 3, 4, 1, 2, 6];
function sort (arr) {
for(let i=0; i < arr.length - 1; i++) {
if(arr[i] > arr[i+1]) {
let b = arr[i+1];
arr[i+1] = arr[i];
arr[i] = b;
i = -1; // Resets the loop
}
}
return arr;
}
console.log(sort(arr));
Try this (performance upgrade):
function bubbleSort(inputArr, reverse = false) {
const len = inputArr.length;
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
let a = inputArr[i];
let b = inputArr[j];
if (reverse ? a < b : a > b) {
const tmp = inputArr[j];
inputArr[j] = inputArr[i];
inputArr[i] = tmp;
}
}
}
return inputArr;
}
Use:
arr = [234,2,4,100, 1,12,5,23,12];
console.log(bubbleSort(arr)); // or console.log(bubbleSort(arr, true));
You need another loop:
var arr = [2, 1]
for(let i = 0;i<arr.length;i++){
for(let b = 0; b<arr.length;i++){
if(arr[b] > arr[b+1]){
var first = arr[b]
var second = arr[b + 1]
arr[b] = second
arr[b + 1] = first
}
}
}
Hope this helps I would recommend using quick sort if you want a high efficiency though.
const bubbleSort = (inputArr) => {
const len = inputArr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (inputArr[j] > inputArr[j + 1]) {
let tmp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = tmp;
}
}
}
return inputArr;
};
const numbers = [50, 30, 10, 40, 60];
console.log(bubbleSort(numbers));
// Output: [ 10, 30, 40, 50, 60 ]
function bubbleSort(array) {
var done = false;
while (!done) {
//alert(1)
done = true;
for (var i = 1; i < array.length; i += 1) {
if (array[i - 1] > array[i]) {
//alert(2)
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
array[i] = tmp;
}
}
}
return array;
}
Another way would be like this:
function bubbleSort(arr) {
let swapped;
do {
swapped = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
swapped = true;
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
} while (swapped);
return arr;
}
let myArray = [8, 1, 2, 5, 51, 13, 15, 33, 123, 100, 22];
console.log(bubbleSort(myArray));
Explanation:
In this function we are going to declare a swapped variable that is being set to false inside the DO WHILE loop, this is being done as a fail-safe not to end up with an infinite loop.
Inside the loop, we have another FOR loop which iterates through the given array and checks if the current value is greater than the next (which we don't want, we need ascending order).
When the IF the condition is true, we are going to swap the variables and assign true for the swapped variable, this is done because we want to keep on the DO WHILE loop untill everything is sorted.
package hasan;
public class hssd {
public static void main(String[] args) {
int t=9;
int g=20;
for (t=g;t>19;++t){
System.out.println(7);
int f=12;
int r=15;
for(r=f;r>5;++r)
System.out.println(r+1000000000+"*"+1000000000);
}
}
}

Average jagged array by index

I have an array of arrays that looks like: var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
(fiddle here)
I need to be able to create a new array based on each inner array's index. So from the above output I'm looking for
1 - [2,3,5]
2 - [2,9,6]
3 - [3,7]
4 - [8]
helper average method:
Array.prototype.average = function () {
var sum = this.sum();
return sum / this.length;
};
I've got something like :
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
//Sconsole.log(data);
Array.prototype.averageAll = function () {
var avgArrays = [[]];
var self = this;
for (var i = 0; i < self.length; i++) {
avgArrays[0].push(self[i][0]);
}
return avgArrays[0].average();
};
//3.333 to the console
console.log(data.averageAll());
I've hardcoded in the season here because if I try to use avgArrays[i][i] I get an error push is not defined. For my simple example, the function calculates the average of the 0th position of each array in the array. If I have arrays of varying sizes like this, how can I make this go slickly in one fell swoop?
reduce can be handy here-
var data= [[2, 2, 3], [3, 9], [5, 6, 7, 8]];
data.Average= function(itm){
return data.Sum(itm)/(itm.length);
}
data.Sum= function(itm){
return itm.reduce(function(a, b){
return a+b
});
}
data.map(data.Average);
/* returned value: (Array)
2.3333333333333335,6,6.5
*/
A comment reminded me to add a 'shim' for IE8 and lower- the other browsers get map and reduce-
(function(){
var ap= Array.prototype; //IE8 & lower
if(!ap.map){
ap.map= function(fun, scope){
var T= this, L= T.length, A= Array(L), i= 0;
if(typeof fun== 'function'){
while(i<L){
if(i in T){
A[i]= fun.call(scope, T[i], i, T);
}
++i;
}
return A;
}
};
}
if(!ap.reduce){
ap.reduce= function(fun, temp, scope){
var T= this, i= 0, len= T.length, temp;
if(typeof fun=== 'function'){
if(temp== undefined) temp= T[i++];
while(i<len){
if(i in T) temp= fun.call(scope, temp, T[i], i, T);
i++;
}
}
return temp;
}
}
})();
Array.prototype.sum = function () {
var total = 0;
for (var i = 0; i < this.length; i++) {
total += this[i];
}
return total;
};
Array.prototype.average = function () {
var sum = this.sum();
return sum / this.length;
};
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
//Sconsole.log(data);
Array.prototype.averageAll = function () {
var avgArrays = [];
var self = this;
//in an array of arrays, val is an array
var maxLen = 0;
for(var i = 0; i < self.length; i++) {
if(self[i].length > maxLen)
{
maxLen = self[i].length;
}
}
console.log('maxlen is ' + maxLen);
for(var j = 0; j < maxLen; j++) {
avgArrays.push([]);
for(var k = 0; k < self.length; k++) {
if(self[k][j]){
avgArrays[j].push(self[k][j]);
}
}
}
console.log(avgArrays);
var result = []
for (var x = 0; x < avgArrays.length; x++) {
result.push(avgArrays[x].average());
}
return result;
};
console.log(data.averageAll());
I think the following code should construct your array for you:
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
var max = 0;
for(var i = 0; i < data.length; i++) {
max = data[i].length > max ? data[i].length : max
}
var result = [];
for(var i = 0; i < max; i++) {
result[i] = [];
for(var j = 0; j < data.length; j++) {
if(i < data[j].length) {
result[i].push(data[j][i]);
}
}
}
After that, it is trivial to calculate the average:
var averages = [];
for(var i = 0; i < result.length; i++) {
var array = result[i];
var sum = 0;
for(var j = 0; j < array.length; j++) {
sum += array[j];
}
averages.push(sum / array.length);
}
Fiddle here.
So if I understood correctly, you want a new array made up of averages of your sub arrays?
Here is a simple way to do it, by leveraging built-in array functions
var data = [[2, 2,3], [3, 9], [5, 6,7,8]];
var averageAll = function(arr) {
return arr.map(function(a) {
return a.reduce(function(b,c) { return b+c; })/a.length;
});
};
averageAll(data);
// -> [2.3333333333333335, 6, 6.5]
Also, as a rule of thumb, don't mess with the standard types' prototypes, in my experience it only leads to trouble.
var data = [
[2, 2, 3],
[3, 9],
[5, 6, 7, 8]
];
Array.prototype.averageAll = function() {
var result = [],
maxIdx = Math.max.apply(Math, this.map(function(arr) { return arr.length }));
for (var i = 0; i < maxIdx; i++) {
var sum = this.reduce(function(old, cur) {
return old + (cur[i] || 0);
}, 0);
result.push(sum / this.length);
}
return result;
};
console.log(data.averageAll());
//[3.3333333333333335, 5.666666666666667, 3.3333333333333335, 2.6666666666666665]
fiddle

Categories