Bubblesort in javascript can't run? - javascript

Please can you tell me what is wrong to this implementation of bubble sort algorithm in JavaScript?
function bubbleSort(arr) {
var swapped;
do {
swapped = false;
for (var i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped)
}
console.log(bubbleSort([4, 25, 1, 6, 2])); // [ 1, 2, 4, 6, 25 ]
console.log(bubbleSort([13, 1, 9, 38, 8, 3, 1])); // [ 1, 2, 4, 6, 25 ]

Your function will return undefined.You need to return arr from the function.
If you don't want to modify the original array then make a copy of original array using slice(). In the below case it doesnot matter because arrays are not stored in any variable.
function bubbleSort(arr) {
arr = arr.slice()
var swapped;
do {
swapped = false;
for (var i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped)
return arr;
}
console.log(bubbleSort([4, 25, 1, 6, 2])); // [ 1, 2, 4, 6, 25 ]
console.log(bubbleSort([13, 1, 9, 38, 8, 3, 1])); // [ 1, 2, 4, 6, 25 ]

Related

Loops & Control Flow

When I run this I can't seem to get the rest of the values.
Write a function mergingTripletsAndQuints which takes in two arrays as arguments. This function will return a new array replacing the elements in array1 if they are divisible by 3 or 5. The number should be replaced with the sum of itself added to the element at the corresponding index in array2.
function mergingTripletsAndQuints(array1, array2) {
let result = [];
let ctr = 0;
let x = 0;
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
ctr = array1[i] + array2[j];
if (ctr % 3 === 0 || ctr % 5 === 0) {
result.push(ctr);
} else {
return array1[i];
}
}
}
return result;
}
console.log(mergingTripletsAndQuints([1, 2, 3, 4, 5, 15], [1, 3, 6, 7, 8, 9])); // expected log [1, 2, 9, 4, 13, 24]
console.log(mergingTripletsAndQuints([1, 1, 3, 9, 5, 15], [1, 2, 3, 4, 5, 6])); // expected log [1, 1, 6, 13, 10, 21]
It is only logging [1], [1]
I'm not sure, but I suppose there is a typo returning array1[i] in nested loop. I suppose you mean result.push(array1[i]) instead.
I think it should be something like this:
function mergingTripletsAndQuints(array1, array2) {
let result = [];
for (let i = 0; i < array1.length; i++) {
if (array1[i]% 3 === 0 || array1[i]% 5 === 0) {
result.push(array1[i] + array2[i]);
} else {
result.push(array1[i]);
}
}
return result;
}
console.log(mergingTripletsAndQuints([1, 2, 3, 4, 5, 15], [1, 3, 6, 7, 8, 9])); // expected log [1, 2, 9, 4, 13, 24]
console.log(mergingTripletsAndQuints([1, 1, 3, 9, 5, 15], [1, 2, 3, 4, 5, 6])); // expected log [1, 1, 6, 13, 10, 21]
A nested for loop is not necessary, look at this code:
function mergingTripletsAndQuints(array1, array2) {
let sum = [];
for (let i = 0; Math.max(i < array1.length, i < array2.length); i++) {
if (array1[i] % 3 == 0 || array1[i] % 5 == 0) {
sum.push(array1[i] + array2[i])
} else {
sum.push(array1[i])
}
}
return sum;
}

Count repeated numbers in array and return true (Cognitive Complexity)

I need to check if a number repeats itself at least three times in an array. How can I refactor it to decrease the Cognitive Complexity that Lint keeps complaining about.
Heres my code:
let array11 = [1, 3, 2, 3, 5, 6, 7, 8, 9, 0, 1];
function checkDuplicateNumber (array11) {
for (let i = 0; i < array11.length; i += 1) {
let sameNumberLoop = 0;
for (let i2 = i; i2 < array11.length; i2 += 1) {
if (array11[i] === array11[i2]) {
sameNumberLoop += 1;
if (sameNumberLoop >= 3) {
return true;
}
}
}
}
}
Instead of iterating multiple times, iterate just once, while counting up the number of occurrences in an object or Map:
let array11 = [1, 3, 2, 3, 5, 6, 7, 8, 9, 0, 1];
function checkDuplicateNumber (array) {
const counts = {};
for (const num of array) {
counts[num] = (counts[num] || 0) + 1;
if (counts[num] === 3) return true;
}
return false;
};
console.log(checkDuplicateNumber(array11));
console.log(checkDuplicateNumber([3, 1, 3, 5, 3]));
let array11 = [1, 3, 2, 3, 5, 6, 7, 8, 9, 0, 1]
let array22 = [1, 3, 2, 3, 5, 6, 7, 1, 9, 0, 1]
function checkDuplicateNumber(arr) {
const map = new Map()
return arr.some((v) => (map.has(v) ? (++map.get(v).count === 3) : (map.set(v, { count: 1 }), false)))
}
console.log(checkDuplicateNumber(array11))
console.log(checkDuplicateNumber(array22))

How to replace spaces whilst adding new elements?

I have a stepped array of elements that is filled as follows:
class Funnel{
constructor() {
this.funnelContents = [];
this.layer = 0;
}
get content() {
return this.funnelContents;
}
fill(...nums) {
let index, startIndex = 0;
for(let i = 0; i < this.funnelContents.length; i++){
while ((index = this.funnelContents[i].indexOf(' ', startIndex)) > -1 && nums.length > 0) {
this.funnelContents[i][index] = nums.shift();
startIndex = index + 1;
}
}
return nums
.splice(0, 15 - this.funnelContents.reduce((count, row) => count + row.length, 0))
.filter(num => num < 10)
.reduce((arr, num) => {
if (this.funnelContents.length) {
this.funnelContents[this.funnelContents.length - 1] = this.funnelContents[this.funnelContents.length - 1].filter(char => char !== ' ');
if ((this.funnelContents[this.layer] || []).length !== this.funnelContents[this.layer - 1].length + 1) {
this.funnelContents[this.layer] = [...(this.funnelContents[this.layer] || []), num];
} else {
this.layer++;
this.funnelContents[this.layer] = [num];
}
}
else {
this.layer++;
this.funnelContents = [...this.funnelContents, [num]];
}
}, []);
}
toString() {
let str = '', nums = '', spacesCount = 1;
for(let i = 5; i > 0; i--){
str += '\\';
for(let j = 0; j < i; j++) {
if (this.funnelContents[i - 1] !== undefined) {
if (this.funnelContents[i - 1][j] !== undefined) {
nums += this.funnelContents[i - 1][j];
} else {
nums += ' ';
}
} else {
nums += ' ';
}
}
str += nums.split('').join(' ') + '\/\n' + ' '.repeat(spacesCount);
nums = '';
spacesCount++;
}
return str.substring(0, str.length - 6);
}
}
let funnel1 = new Funnel();
let funnel2 = new Funnel();
let funnel3 = new Funnel();
let funnel4 = new Funnel();
let funnel5 = new Funnel();
let funnel6 = new Funnel();
let funnel7 = new Funnel();
funnel1.fill(5,4,3,4,5,6,7,8,9,3,2,4,5,6,7,5,6,7,8); //15 elements will be added, the rest are ignored
funnel2.fill(5,4,3,4,5,6,7,8);
funnel2.fill(9,3,2,4,5,6,7);
funnel3.fill(' ');
funnel3.fill(1,5,7);
funnel4.fill(1,2,3);
funnel4.fill(' ');
funnel4.fill(3,4,5);
funnel5.fill(1);
funnel5.fill(' ', ' ', ' ');
funnel5.fill(8,2,1);
funnel6.fill(' ',' ');
funnel6.fill(1,8,2,1);
funnel7.fill(' ',' ',' ',' ',' ');
funnel7.fill(1,8,2,1);
console.log(funnel1.toString()); // the output is as expected.
console.log(funnel2.toString()); // the same result
console.log(funnel3.toString()); // expected [ [1], [5,7] ] and it really is
console.log(funnel4.toString()); // expected [ [1], [2,3], [3,4,5] ] and it really is
console.log(funnel5.toString()); // expected [ [1], [8,2], [1] ] and it really is
console.log(funnel6.toString()); // expected [ [1], [8,2], [1] ] but got [ [], [1,8], [2], [1] ]
console.log(funnel7.toString()); // nothing is changed
Here you can see that at the very beginning of the function fill a cycle was written to insert elements that came to the input instead of spaces. I added spaces artificially, in fact, there is another function that adds them. But:
1) For some reason this does not always work, for an array in the example it does not work. With a simpler space search algorithm, it also does not work properly:
for (let i = 0; i < this.funnelContents.length; i++) {
for (let j = 0; j < this.funnelContents[i].length; j++) {
if(this.funnelContents[i][j] === ' '){
this.funnelContents[i][j] = nums.shift();
}
}
}
2) It looks very cumbersome and I would like to do something similar more elegantly. I was thinking of two for loops to find elements I need, but I still hope that I can implement insertion instead of spaces inside reduce function.
You could take a single loop and slice substrings with increasing length.
function funnel(array) {
var i = 0,
l = 0,
result = [];
while (i < array.length) result.push(array.slice(i, i += ++l));
return JSON.stringify(result);
}
console.log(funnel([1]));
console.log(funnel([1, 2]));
console.log(funnel([1, 2, 3]));
console.log(funnel([1, 2, 3, 4]));
console.log(funnel([1, 2, 3, 4, 5]));
console.log(funnel([1, 2, 3, 4, 5, 6]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]));
console.log(funnel([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
As I understand the comment, you have an array in the given style with incrementing count, but some elements are spaces and this elemenst should bereplaced with the given data.
In this case, a nested loop for getting the result set right and another index for getting the value from the data array should work.
class Funnel {
constructor() {
this.funnelContents = [];
}
get content() {
return this.funnelContents;
}
fill(...nums) {
var i = 0,
j = 0,
l = 1,
k = 0,
target = this.funnelContents;
while (k < nums.length) {
if (!target[i]) target.push([]);
if ([undefined, ' '].includes(target[i][j])) target[i][j] = nums[k++];
if (++j === l) {
if (++i > 4) break; // max 15 elements in result set
j = 0;
l++;
}
}
}
}
var funnel = new Funnel;
funnel.fill(' ', ' ', ' ', ' ');
console.log(JSON.stringify(funnel.content));
funnel.fill(1, 2, 3, 4, 5, 6, 7, 8, 9);
console.log(JSON.stringify(funnel.content));
funnel.fill(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
console.log(JSON.stringify(funnel.content));

How to find group of three elements in array whose sum equals to target sum?

I can do this with three loops but complexity will be O(n3), can it be done with less complexity?
Adding js fiddle code for three loops approach
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var sum = 8;
find(arr, sum);
function find(arr, sum) {
var found = false;
for (var x = 0; x < arr.length - 3; x++) {
for (var y = x + 1; y < arr.length - 2; y++) {
for (var z = y + 1; z < arr.length; z++) {
if (arr[x] + arr[y] + arr[z] == sum) {
found = true;
break;
}
}
}
}
if (found) {
console.log("found");
} else {
console.log("not found");
}
}
You could use a hash table and one loop for the array and one for the sum of two items.
function find(array, sum) {
var hash = Object.create(null);
return array.some((v, i, a) => {
a.slice(0, i).forEach(w => hash[v + w] = true);
return hash[sum - v];
});
}
console.log(find([1, 2, 3, 4, 5, 6, 7, 8], 8));
console.log(find([1, 2, 3, 4, 5, 6, 7, 8], 42));
If you like it more functional with a closure over a Set for the sum two values, then this would work, too.
function find(array, sum) {
return array.some(
(s => (v, i, a) => a.slice(0, i).reduce((t, w) => t.add(v + w), s).has(sum - v))
(new Set)
);
}
console.log(find([1, 2, 3, 4, 5, 6, 7, 8], 8));
console.log(find([1, 2, 3, 4, 5, 6, 7, 8], 42));

Find Max Slice Of Array | Javascript

I need to find the maximum slice of the array which contains no more than two different numbers.
Here is my array [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]
My thought process on this is to find the numbers that are not repeated and return their index within a new array.
Here is what I have so far:
function goThroughInteger(number) {
var array = [];
//iterate the array and check if number is not repeated
number.filter(function (element, index, number) {
if(element != number[index-1] && element != number[index+1]) {
array.push(index);
return element;
}
})
console.log(array);
}
goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]);
I'm unsure where to go next, I'm struggling to understand the question that being - find the maximum slice which contains no more than two different numbers - that confuses me.
A solution with a single loop, which checks the last values and increments a counter.
function getLongestSlice(array) {
var count = 0,
max = 0,
temp = [];
array.forEach(function (a) {
var last = temp[temp.length - 1];
if (temp.length < 2 || temp[0].value === a || temp[1].value === a) {
++count;
} else {
count = last.count + 1;
}
if (last && last.value === a) {
last.count++;
} else {
temp.push({ value: a, count: 1 });
temp = temp.slice(-2);
}
if (count > max) {
max = count;
}
});
return max;
}
console.log(getLongestSlice([58, 800, 0, 0, 0, 356, 8988, 1, 1])); // 4
console.log(getLongestSlice([58, 800, 0, 0, 0, 356, 356, 8988, 1, 1])); // 5
console.log(getLongestSlice([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8])); // 10
function goThroughInteger(array) {
var solutionArray = [];
var max = 0;
for (var i = 0; i <= array.length; i++) {
for (var j = i + 1; j <= array.length; j++) {
var currentSlice= array.slice(i,j);
var uniqSet = [...new Set(currentSlice)];
if(uniqSet.length <3) {
if(currentSlice.length>max) {
max= currentSlice.length;
}
}
}
}
console.log(max);
}
goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]);
This solution checks every possible slice of the array, checks if it has not more than 2 different numbers and finally prints out the length of the longest slice.
This is a possible solution, with complexity O(n²) (as pointed out by #le_m in the comments)
goThroughInteger = (list) => {
let scores = list.reduce((slices, num, pos) => {
let valid = [num];
let count = 0;
for (let i = pos; i < list.length; i++) {
if (valid.indexOf(list[i]) == -1) {
if (valid.length < 2) {
valid.push(list[i]);
count++;
} else {
break;
}
} else {
count++;
}
}
slices[pos] = { pos, count };
return slices;
}, []);
scores.sort((a, b) => b.count - a.count);
let max = scores[0];
return list.slice(max.pos, max.pos + max.count);
};
console.log(goThroughInteger([1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8]));
console.log(goThroughInteger([58, 800, 0, 0, 0, 356, 8988, 1, 1]));
```
The solution calculates the 'score' at every position of the input list, counting the length of a sequence of no more than 2 different values, then takes the result with the highest score and extracts a slice from the original list based on that information.
It can definitely be cleaned and optimized but I think it's a good starting point.
Using the sliding window algorithm in O(n) time:
const arr = [1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 6, 2, 1, 8, 1, 1 ,1 ,1, 8, 1, 1, 8, 8];
const map = {
length: 0
};
let required = [];
for(start = 0, end = 0; end <= arr.length; ){
if(map.length > 2){
if(map[arr[start]] === 1){
delete map[arr[start]];
map.length --;
}else{
map[arr[start]]--;
};
start++;
}else{
if(end - start > required.length){
required = arr.slice(start, end);
};
if(map[arr[end]]){
map[arr[end]]++;
}else{
map[arr[end]] = 1;
map.length++;
}
end++;
}
}
console.log(required);

Categories

Resources