Debugging Error on counting empty squares - javascript

Given an array of arrays, count how many are empty. Notes: empty counts as: length of 0 (no elements), empty strings, and the number 0.
var squares1 = [[5], [0], [9], ['','', 10]];
console.log(emptySquares(squares1)) //=> 1
var squares2 = [[],[0, ''],[]];
console.log(emptySquares(squares2)) // => 3;
var squares3 = [['hello', 2], ['maybe'], ['10'], [100]];
console.log(emptySquares(squares3)); //=> 0
function emptySquares(squares) {
var total = 0;
for (var i = 0; i < squares.length; i++) {
var isEmpty = true;
for (var j = 0; j < squares[i].length; j++) {
if (squares[i][j] !== '' || squares[i][j] !== 0 || squares[i].length === 0) {
isEmpty = false;
}
}
if (isEmpty) {
total += 1;
}
}
return total;
}

Not exactly sure about the question, but I would go with a simple recursive design:
const emptySquares = (()=>{
let total = 0, a;
return squares=>{
for(let v of squares){
a = v instanceof Array;
if(a && v.length){
total = emptySquares(v);
}
else if(a || v === '' || v === 0){
total++;
}
}
const r = total;
total = 0;
return r;
}
})();
const squares1 = [[5], [0], [9], ['','', 10]];
console.log(emptySquares(squares1));
const squares2 = [[],[0, ''],[]];
console.log(emptySquares(squares2));
const squares3 = [['hello', 2], ['maybe'], ['10'], [100]];
console.log(emptySquares(squares3));

Related

Counting Array Values by a Filtered Value in Javascript

I am trying to count the number of values in the array I calculated (const C) that fall into a given range, for instance the array returns [2,2,1.5,1.5], I'm trying to count the number of values in the array that is <1, 1<x<2, 2<x, etc. So it should return, count x<1 = 0, 1<x<2 = 4, 2<x = 0. Thanks for the help!
for(let i = 0; i < 1; i++) {
Array.prototype.zip = function (other, reduce, thisArg) {
var i, result = [], args,
isfunc = typeof reduce == "function",
l = Math.max(this.length, other.length);
for (i = 0; i < l; i++) {
args = [ this[i], other[i] ];
result.push( isfunc ? reduce.apply(thisArg, args) : args );
}
return result;
}
const A = [4,6,12,18]
const B = [2,3,4,6]
const C = A.zip(B, function (l, r) { return l / (l - r); }); //[2,2,1.5,1.5]
let id = document.querySelectorAll(`[id^="id"]`)[i]
let problemTypeChoice = 0;
if (problemTypeChoice === 0) {
id.innerText = `${C}`
}
}
Array reduce to group "by" method.
var arr = [2, 2, 1.5, 1.5, 3, -2]
var result = arr.reduce(function(agg, item) {
var key = "other";
if (item < 1) key = "<1";
if (item > 1 && item < 2) key = "1<2";
if (item > 2) key = ">2";
agg[key] = (agg[key] || 0) + 1
return agg;
}, {})
console.log(result)

How to split array into equal parts based on values?

Input Arr=[1,2,3,4,5,6,7,8,9,10]
Expected output:-
Arr1 = [1,2,3,4,5,6,7] = 28
Arr2 = [8,9,10] = 27
The sum of arrays should be almost the same..
It can also be 3 or more parts
How to achieve this via custom function?
let Arr = [1,2,3,4,5,6,7,8,9,10]
const numberOfParts = 2
function SplitArr(Array, Parts){
/* ... */
}
let result = SplitArr(Arr,numberOfParts)
/* result should be [[1,2,3,4,5,6,7],[8,9,10]] */
/* output can be in any format as long as it can get the parts */
I think you can't do that directly by JS functions.
You have to create a custom function to achieve this.
I have considered dividing the array into 2 equal parts.
You can't always split the array equally. Here in this array, you can't partition array into more than 2 subparts, otherwise it will give more than 3 parts as some of the elements are present there having sum more than the partitioned Sum.
Note: I treated the array to be sorted, otherwise it depends on the usecase.
Note: I have updated the old implementation based on the updated question requirement
let arr=[1,2,3,4,5,6,7,8,9,10]
function splitArrayEqually(arr, parts=2){
//get the total sum of the array
let sum = arr.reduce((currentSum, value) => currentSum+value ,0);
//get the half sum of the array
let partitionedSum = Math.ceil(sum/parts);
let start=0, end=0, currentSum=0;
let splittedArray=[];
//get the index till which the sum is less then equal partitioned sum
while(end < arr.length){
if(currentSum+arr[end] > partitionedSum){
splittedArray.push(arr.slice(start,end));
start = end; //start new window from current index
currentSum = 0; //make sum =0
}
//add current end index to sum
currentSum += arr[end];
end++;
}
splittedArray.push(arr.slice(start));
return splittedArray;
}
splitted = splitArrayEqually(arr,3);
console.log(splitted)
let Arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const numberOfParts = 3
function sumOfArray(arr) {
if (arr) {
if (arr.length > 0) {
let sum = 0
for (let i = 0; i < arr.length; i++) sum += arr[i]
return sum
} else {
return 0
}
} else {
return 0
}
}
function SplitArr(Array, Parts) {
let lastIndex = 0
let result = []
function getReamingSum(arr) {
let psum = sumOfArray(Array.slice(lastIndex)) / Parts
console.log('psum ' + psum)
return psum + Parts
}
let psum = getReamingSum(Array)
for (let j = 0; j < Parts; j++) {
let total = 0
for (let i = 0; i < Array.length; i++) {
if (i >= lastIndex) {
total += Array[i]
if (total < psum || j === Parts - 1) {
if (result[j]?.length > 0) {
result[j].push(Array[i])
} else {
let arr = []
arr.push(Array[i])
result[j] = arr
}
lastIndex = i + 1
}
}
}
}
return result
}
let result = SplitArr(Arr, numberOfParts)
console.log(result)
Assuming the array isn't sorted,using a 2D array, with each sub array with sum almost equal to (sum of array / n).
let arr = [9,2,10,4,5,6,7,8,1,3]
arr.sort(function(a, b) { return a - b; });
const sum = arr.reduce((a, b) => a + b, 0);
const n = 2;
const result = [];
let s = 0;
let j = 0;
result[j] = [];
for(let i=0; i<arr.length; i++){
if(s <= Math.floor(sum/n)){
result[j].push(arr[i]);
s +=arr[i];
}
else{
s = 0;
j = j + 1;
result[j] = [];
result[j].push(arr[i]);
}
}
console.log(result)
O/P:
[ [1, 2, 3, 4,5, 6, 7], [ 8, 9, 10 ] ]
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const splitArray = (arr,parts) => {
const totalSum = arr.reduce((acc, item) => {
acc += item;
return acc;
}, 0)
const splitSum = Math.floor(totalSum / parts);
const arrObj = arr.reduce((acc, item,index) => {
acc.sum = acc.sum || 0;
acc.split = acc.split || {};
const pointer = Math.floor(acc.sum / splitSum);
//console.log(item,acc.sum, splitSum, pointer);
acc.split[pointer] = acc.split[pointer] || [];
acc.split[pointer].push(item);
acc.splitSum = splitSum;
acc.sum += item;
return acc;
}, {})
return arrObj;
}
console.log(splitArray(arr,2).split)
You're better off making a custom function:
let currentTotal = 0
let tempList = []
Arr.forEach(val => {
if (val >= 27) {
// push tempList to a new array
tempList = [];
currentTotal = val;
} else {
tempList.push(val);
currentTotal += val;
}
})

JavaScript Elements of array being equal function not working

While taking some online JS tests, I am stuck on why my function for testing if the elements within two arrays are equal is not working. I am using the function to make sure that the new array does not have duplicate values.
The following is my function for testing if the elements of the arrays are equal or not:
const isEqualArr = (a, b) => {
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
};
I call to the function within a for loop, then I test for undefined, because the first time around, the outputArr is empty
for (let x = 0; x < numsLength; x++) {
if (typeof outputArr[x] != 'undefined') {
if (isEqualArr(tempArr, [outputArr[x]])) {
However, the result shows that there are duplicates:
0: Array [ -1, 0, 1 ]
​
1: Array [ -1, 2, -1 ]
​
2: Array []
​
3: Array [ -1, -1, 2 ]
​
4: Array []
​
5: Array [ 0, 1, -1 ]
​
6: Array [ 0, -1, 1 ]
​
7: Array [ 1, 0, -1 ]
​
8: Array [ -1, 0, 1 ]
The other issue is that not only do I have two null arrays (duplicates), but when I test for null before the push, it is not working either:
if(tempArr != null ) {
outputArr.push(tempArr);
}
I have tried tempArr[0] != null, tempArr[0] != '' as well as
if (typeof tempArr[0] != 'undefined')
But it still inserts null arrays into the outputArr.
The following is the premise of the test:
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets. Given nums = [-1,0,1,2,-1,-4]
The following code is my solution, but the duplicate entries, as well as the null entries, makes this an incorrect solution. If you guys can see why my isEqualArr function is at fault, then I would appreciate it.
const threeSum = (nums) => {
let numsLength = nums.length;
let tempArr = [];
let outputArr = [];
if (numsLength > 3) {
for (let i = 0; i < numsLength; i++) {
for (let j = 1; j < numsLength; j++) {
for (let k = 2; k < numsLength; k++) {
if (
i != j &&
i != k &&
j != k &&
nums[i] + nums[j] + nums[k] == 0
) {
tempArr[0] = nums[i];
tempArr[1] = nums[j];
tempArr[2] = nums[k];
for (let x = 0; x < numsLength; x++) {
if (typeof outputArr[x] != 'undefined') {
if (!isEqualArr(tempArr, [outputArr[x]])) {
if (typeof tempArr[0] != 'undefined') {
outputArr.push(tempArr);
console.log(
'temp: ' + tempArr + ' outputArr: ' + outputArr[x]
);
console.log(
'compare: ' + isEqualArr(tempArr, outputArr[x])
);
console.log(outputArr);
tempArr = [];
}
}
}
}
if (i == 0) {
outputArr.push(tempArr);
tempArr = [];
} else {
// do nothing
}
}
}
}
}
}
return outputArr;
};
const isEqualArr = (a, b) => {
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
};
EDIT: I was able to eliminate the null entries by moving the code that checks if it is the first time to run above the for loop:
if (i == 0) {
outputArr.push(tempArr);
tempArr = [];
} else {
// do nothing
}
for (let x = 0; x < numsLength; x++) {
if (typeof outputArr[x] != 'undefined') {
if (!isEqualArr(tempArr, [outputArr[x]])) {
if (typeof tempArr[0] != 'undefined') {
outputArr.push(tempArr);
console.log(
'temp: ' + tempArr + ' outputArr: ' + outputArr[x]
);
console.log(
'compare: ' + isEqualArr(tempArr, outputArr[x])
);
console.log(outputArr);
tempArr = [];
}
}
}
}
BTW, another thing that is not working if the following logic:
i != j &&
i != k &&
j != k &&
You can see by the outputArr that this logic is being completely ignored.
REFACTORED: even though the online test marked my solution as wrong, you can see with the results, that it is correct. Especially when you factor in the conditionals for making sure that nums[i] != nums[j] and so forth:
const threeSum = (nums) => {
let numsLength = nums.length;
let tempArr = [];
let outputArr = [];
for (let i = 0; i < numsLength; i++) {
for (let j = 0; j < numsLength; j++) {
for (let k = 0; k < numsLength; k++) {
if (
nums[i] != nums[j] &&
nums[i] != nums[k] &&
nums[j] != nums[k] &&
nums[i] + nums[j] + nums[k] == 0
) {
if (typeof outputArr[0] != 'undefined') {
/**********
* if it reaches this conditional
* then outputArr has at least one element
***********/
if (typeof outputArr[k] != 'undefined') {
tempArr[0] = nums[i];
tempArr[1] = nums[j];
tempArr[2] = nums[k];
if (isEqualArr(tempArr, outputArr)) {
// do nothing because there is already an element within the array
} else {
outputArr.push(tempArr);
// empty tempArr after insert
tempArr = [];
}
}
} else {
// push triplet elements into temp array for the first iteration only
tempArr[0] = nums[i];
tempArr[1] = nums[j];
tempArr[2] = nums[k];
// insert tempArr for the first iteration only
outputArr.push(tempArr);
// empty tempArr after insert
tempArr = [];
}
}
}
}
}
return outputArr;
};
const isEqualArr = (a, b) => {
for (elem in b) {
for (let i = 0; i < a.length; i++) {
if (a[i] != elem[i]) {
// do nothing
} else {
return true;
}
}
}
return false;
};
RESULTS:
(3) [Array(3), Array(3), Array(3)]
0: (3) [-1, 0, 1]
1: (3) [1, 0, -1]
2: (3) [-1, 1, 0]
length: 3
Your inner for loop is confusing, if you replace it by a forEach you can see what's going on, you should pass as argument outputArr[x] not [outputArr[x]] and there's more stuff.
const threeSum = (nums) => {
let numsLength = nums.length;
let tempArr = [];
let outputArr = [];
if(numsLength < 3) return;
for (let i = 0; i < numsLength; i++) {
for (let j = 1; j < numsLength; j++) {
for (let k = 2; k < numsLength; k++) {
if (
i != j &&
i != k &&
j != k &&
nums[i] + nums[j] + nums[k] == 0
) {
tempArr.push(nums[i], nums[j], nums[k]); // push three at the same time, it's cleaner and avoid problems
outputArr.forEach((arr, index) => {
if(!isEqualArr(arr, outputArr[index])) outputArr.push(tempArr); // outputs correctly
});
if (i == 0) {
outputArr.push(tempArr);
tempArr = [];
}
}
}
}
}
return outputArr;
};
const isEqualArr = (a, b) => {
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
};
console.log(threeSum([-1,0,1,2,-1,-4]));

how to get the count value based on comparing string in javascript

I have array object in which I compare each string in a array and if one letter is not matching, increment the value. If three characters match with the string then increment the count value, else 0
var obj = ["race", "sack", "grass", "brass", "beat", "pack", "cake"]
fucntion getValue(obj) {
var count = 0
for (var i = 0; i <= obj.length; i++) {
for (var j = 1; j <= obj.length; j++) {
if (obj[i].split("") == obj[j].split("") {
count++;
}
}
}
}
Expected Output
race 1
sack 2 // (pack, cake matches 3 letters with sack so 2)
grass 1
brass 1
beat 0
pack 2
cake 3
function getSameCount(str1, str2) {
let count = 0;
const obj = str2.split("");
for(str of str1){
let idx = obj.findIndex(s => s === str);
if(idx >= 0){
count++;
obj.splice(idx, 1);
}
}
return count;
}
var obj = ["race", "sack", "grass", "brass", "beat", "pack", "cake"]
const res = {}
for (var i = 0; i < obj.length; i++) {
res[obj[i]] = 0
for (var j = 0; j < obj.length; j++) {
if (i != j) {
matchCount = getSameCount(obj[i], obj[j])
if (matchCount === 3) {
res[obj[i]]++
} else {
if (obj[i].length - matchCount == 1) {
res[obj[i]]++
}
}
}
}
}
console.log(res)
You can create an object for output,
parse through each element in the array, replace characters from comparing elements.
check if at least 3 chars got removed if so +1 to respective element.
const arr = ["race", "sack", "grass", "brass", "beat", "pack", "cake"];
const out = Object.fromEntries(arr.map(e => [e, 0]));
for(i in arr) {
for(j in arr) {
if(i == j) continue;
if(arr[i].length !== arr[j].length) continue;
const res = [...arr[i]].reduce((acc, e)=> acc.replace(e, ''), arr[j]);
if(res.length <= arr[j].length - 3) {
out[arr[i]] = out[arr[i]] + 1
}
}
}
console.log(out);

Write an algorithm that takes an array and moves all of the zeros to the end

Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
For example:
moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
My code:
var moveZeros = function (arr) {
var zeros = [];
var others = [];
var res;
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
if (arr[i] == 0) {
zeros.push(arr[i]);
} else {
others.push(arr[i]);
}
}
var res = others.concat( zeros );
return res;
}
I get the following result:
Expected: ["a","b",null,"c","d",1,false,1,3,[],1,9,{},9,0,0,0,0,0,0,0,0,0,0],
Instead got: ["a","b",null,"c","d",1,1,3,1,9,{},9,0,0,0,false,0,0,[],0,0,0,0,0]
The expected result is quite close to what I achieved (see above) . I don't understand why I have false in a different place?
try this simply
var arr = [false,1,0,1,2,0,1,3,"a"];
arr.sort(function(a,b){if (a===0){return 1}});
document.body.innerHTML += JSON.stringify(arr);
Try to use a normal for loop and splice at this context to make your job done,
var arr = [false,1,0,1,2,0,1,3,"a"];
for(var i=arr.length;i>0;i--){
if(arr[i] === 0){ arr.push(arr.splice(i,1).pop()); }
}
console.log(arr); //[false, 1, 1, 2, 1, 3, "a", 0, 0]
document.body.innerHTML += JSON.stringify(arr);
Use Array#splice() and Array#push().
function moveZeros(a) {
var i = a.length - 1;
while (i--) {
if (a[i] === 0) {
a.push(a.splice(i, 1)[0]);
}
}
return a;
};
var array = [false, 1, 0, 1, 2, 0, 1, 3, "a"];
document.write('<pre>' + JSON.stringify(moveZeros(array), 0, 4) + '</pre>');
Another approach with swapping items.
function moveZeros(array) {
var length = array.length,
i = length;
while (--i) {
if (array[i] !== 0) continue;
length--;
while (i < length) {
[array[i + 1], array[i]] = [array[i], array[i + 1]];
i++;
}
}
return array;
}
var array = [false, 1, 0, 1, 2, 0, 1, 3, "a"];
console.log(...moveZeros(array));
Please use === operator to compare if value is 0:
if (arr[i] === 0) {
zeros.push(arr[i]);
} else {
others.push(arr[i]);
}
The code snippet below should work , please try , reason for null is that the length of the arr starts count from zero so when using <= you should subtract 1 from the total length.
var moveZeros = function (arr) {
// TODO: Program me
let zero = []
let others = []
let together = []
for (let i =0; i <= arr.length-1; i++){
if (arr[i] === 0){
zero.push(arr[i])
}
else{
others.push(arr[i])
}
}
together = others.concat(zero)
return together
}
var titleCase = function(title) {
var arr = [];
for (i = 0; i < title.length ; i++) {
if (title[i] !== 0 ) {
arr.push(title[i]);
}
}
for (i = 0 ; i < title.length ; i++) {
if (title[i] == 0 ) {
arr.push(title[i]);
}
}
return arr;
}
You could use the filter() method to achieve your goal. With the arrow notation you can shorten the code. For example: arr => arr === 0 is the shorthand for the anonymous filter function function(arr) { return arr === 0; }
var moveZeros = function (arr) {
const zeros = arr.filter (arr => arr === 0);
const others = arr.filter (arr => arr !== 0);
return others.concat(zeros);
}
var arr = [false,1,0,1,2,0,1,3,"a"]
function moveZeros(arr){
var zeros = [];
var others = [];
var output;
for (var i=0; i< arr.length; i++){
if (arr[i]===0){
zeros.push(arr[i]);
}else{
others.push(arr[i])
}
}
output = others.concat(zeros);
console.log(output);
}
moveZeros([false,1,0,1,2,0,1,3,"a"]);

Categories

Resources