How use forEach to make decisions - javascript

i have the following array:
var array = [10, 15, 20];
and i have also this variable called N:
var N = 20;
I and make a loop and check all the itens inside, the N must be greater them all numbers so i can call functionA, if is not, i want call other function
array.forEach(function (item) {
if (N > All) {
callFunctionA();
} else if (N > onlySomes) {
callFunctionB();
}
});
But the problem is that if the first number of the array is smaller them the number, the function will be called, and i want check all the numbers.. This is possible???

You can use a boolean and set it false if a number is larger than N. Similarly use a second boolean to check if there is at least one number which is smaller than N.
var allAreSmaller = true;
var someAreSmaller = false;
array.forEach(function (item) {
if (N <= item) {
allAreSmaller = false;
}
if (N > item)
someAreSmaller = true;
});
if (allAreSmaller)
callFunctionA();
else if (someAreSmaller)
callFunctionB()

It's unclear if you only want the function to be called once in the event one or more of the numbers in the array is greater N.
If you want it to be called EACH TIME it finds a number in the array greater than N, what you have is fine.
If you only want to determine if the function should be called or not, and CALL IT ONCE, replace your function call with a boolean variable that's set to false when N is defined, and set it to true inside the if statement. It won't matter if multiple iterations set it to true.
After the loop, check if hte boolean is true, and if so call the funciton.
Example:
var array = [10, 15, 20];
var fireCallback = false;
var N = 20;
array.forEach(function (item) {
if (N > item) {
fireCallback = true;
//callFunction();
}
});
if (fireCallback) {
callFunction();
}
Another approach you could try (if you only want it to fire once) is finding the largest number in the array using Math.max:
var largest = Math.max.apply(Math, array); // 306
Then checking if the largest number is bigger than N
if(largest >= N) {
callFunction();
}

If that is all you are doing in your loop, you could use a filter (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter):
var areAllSmaller = array.filter(function(_item){ return _item > N}).length <= 0; // true or false
Tenta aê!

You can use Array.every() and Array.some()
var flag1 = array.every(function (item) {
return N > item;
});
var flag2 = array.some(function (item) {
return N > item;
});
if (flag1) {
callFunctionA();
}
if (flag2) {
callFunctionB();
}

Related

javascript function to find the second largest element in an array

I am completing the hackerrank's 10 days of javascript challenge. The question:
write a function to take an array as an argument and then return the second largest element in the array.
I have written the code but my code is returning the largest element and not the second largest as asked.
function getSecondLargest(nums) {
// Complete the function
var largest=nums[0];
for(let i=1;i<nums.length;++i)
{
if(nums[i]>largest)
largest=nums[i];
}
var large=nums[0];
for(let j=1;j<nums.length;++j)
{
if(large<nums[j]&&large<largest)
large=nums[j];
}
return large;
}
When input array nums={2,3,6,6,5} the result is coming 6 while expected output is 5. Please help and point out the errors in the function code below.
should not initialize large with first value var large=nums[0]; because it may appear the biggest value and won't work
should use nums[j]<largest instead of large<largest as mentioned above
I think don't need second loop as all checks can be done in first loop, and you can assign prev largest to large whenever you change it:
function getSecondLargest(nums) {
var largest = nums[0];
var large;
for (let i = 1; i < nums.length; ++i) {
if (nums[i] > largest) {
large = largest;
largest = nums[i];
} else if (nums[i] > large || typeof large === 'undefined') {
large = nums[i]
}
}
return large;
}
console.log(getSecondLargest([5,1-2,3]))
console.log(getSecondLargest([-5,1,-2,3]))
GET SECOND LARGEST
first, I create new array with unique values.
let arr = [...new Set(nums)];
second, sort value using built-in function .sort().
note : by default .sort() always sorts asciibetically, but for some testcase, it doesn't work. So, I put (a, b) => { return a - b } to make sure it will work properly.
arr = arr.sort((a, b) => { return a -b });
third, get the value from arr
let result = arr[arr.length - 2] || arr[0];
finally, return the result
return result
function getSecondLargest(nums) {
let arr = [...new Set(nums)];
//Javascript's array member method .sort( always sorts asciibetically.
arr = arr.sort((a, b) => { return a - b });
let result = arr[arr.length - 2] || arr[0];
return result
}
Just one minor change:
Use nums[j]<largest instead of large<largest in the second for loop
function getSecondLargest(nums) {
// Complete the function
var largest=nums[0];
for(let i=1;i<nums.length;++i)
{
if(nums[i]>largest)
largest=nums[i];
}
var large;
//To ensure that the selected number is not the largest
for(let j=0;j<nums.length;++j)
{
if (nums[j] !== largest){
large = nums[j];
break;
}
}
for(let j=1;j<nums.length;++j)
{
if(large<nums[j]&&nums[j]!=largest)
large=nums[j];
else
console.log(large)
}
return large;
}
var secondLargest = getSecondLargest([6,3,6,6,5]);
console.log("Second largest number", secondLargest);
If you want to avoid using library functions like #ifaruki suggests, this line
if(large<nums[j]&&large<largest)
should read
if (large<nums[j] && nums[j] < largest)
Sorting and picking the second or second-to-last value fails when there are duplicates of the highest value in the input array.
Another easiest logic is to remove duplicates from the array and sort.
let givenArray = [2, 3, 6, 6, 5];
let uniqueArray = [...new Set(givenArray)];
console.log("The second largets element is", uniqueArray.sort()[uniqueArray.length - 2]);
I know you had your question answered, just thought I would provide my solution for any future users looking into this.
You can use reduce to go through the array while remembering the two largest numbers so far.
You just make a simple reduction function:
function twoMax(two_max, candidate)
{
if (candidate > two_max[0]) return [candidate,two_max[0]];
else if (candidate > two_max[1]) return [two_max[0],candidate];
else return two_max;
}
And then you use it for example like this:
let my_array = [0,1,5,7,0,8,12];
let two_largest = my_array.reduce(twoMax,[-Infinity,-Infinity]);
let second_largest = two_largest[1];
This solution doesn't require sorting and goes through the array only once.
If you want to avoid using **sort method. I think here's the easiest logic to do that, which will also work in arrays where there's duplicates of largest integer exists.
function getSecondLargest(arr) {
const largest = Math.max.apply(null, arr);
for (let i = 0; i < arr.length; i++) {
if (largest === arr[i]) {
arr[i] = -Infinity;
}
}
return Math.max.apply(null, arr);
}
console.log(getSecondLargest([5, 7, 11, 11, 11])); //7

Javascript variable assignment in nested for loop

I am pulling my hair out with this algorithm. The goal is to take number array as an input and output a string that has all sequential numbers displayed as a range. For example if my input were [1,2,4,6,7,8,] my output should be "1-2,4,6-8". My issue lies with the variable assignment in my nested for loop's if statement. IndexEn is overridden each time j increments and should ultimately exit the inside loop as the highest value. The problem is that when I try to call the variable outside of the for loop it passes each value of IndexEn instead of the maximum. I don't understand how IndexEn is able to be outside of the scope of the inner loop while the inner loop is still running? Can someone please help me fix and understand whats happening here?
function bkInd(arr){
var bookSt="";
var indexSt;
var indexEn;
for(var i =0;i<arr.length-1;i++){
if(arr[i+1] !== (arr[i]+1)) {
if(i===0 || (i>0 && arr[i]) !== (arr[i-1]+1) ){
bookSt+= arr[i]+",";
}
// check to see if number are sequential and SHOULD output starting index value - last value
}else{
for(var j=i+1;j<arr.length;j++){
var count=0;
if(arr[j]==(arr[i +count]+1)){
indexSt = arr[i];
indexEn = arr[j];
count+=1;
}
}
//concatenate string
//console.log(indexEn); for value of index
bookSt+= indexSt+"-"+indexEn+",";
}
}
return bookSt;
}
var bookList = [1,3,4,5,7,9,10,11];
document.write(bkInd(bookList));
I don't like nested looping (can be slow on large datasets) so I took a different approach, hope you don't mind:
var bkInd = function(arr) {
var result = [];
var seq = []; // 'Temporary' array
for(var i = 0; i < arr.length; i++) {
seq.push(arr[i])
if(arr[i] + 1 !== arr[i + 1]) {
result.push(seq.length > 1 ? seq[0] + '-' + seq.pop() : arr[i]);
seq = [];
}
}
return result.join(', ')
}
// => '1, 3-5, 7, 9-11'
It instead 'builds up' a temporary array of numbers (seq) as it loops through arr. If the next number is sequential, seq keeps the last number and the loop continues. If the next number is more than one above the current one, seq is pushed to result, but if seq is more than one index long it will concatenate these to a hyphenated string. After seq get pushed, it gets reset to an empty array. Finally, it joins the result array with commas.
Code is updated according to your requirement :
function bkInd(arr){
var bookSt="";
var indexSt;
var indexEn;
for(var i =0;i<arr.length-1;i++){
if(arr[i+1] !== (arr[i]+1)) {
if(i===0 || (i>0 && arr[i]) !== (arr[i-1]+1) ){
bookSt+= arr[i]+",";
}
// check to see if number are sequential and SHOULD output starting index value - last value
}else{
var count=1;
indexSt = arr[i];
for(var j=i;j<arr.length;j++){
if(arr[j]==(indexSt+count)){
indexEn = arr[j];
count+=1;
i++;
}
}
//concatenate string
//console.log(indexEn); for value of index
bookSt+= indexSt+"-"+indexEn+",";
}
}
return bookSt;
}
var bookList = [1,3,4,5,7,9,10,11];
console.log(bkInd(bookList));
Your inner for loop logic was incorrect. I have updated the code to meet the requirement.
Scope is the region of the code within which a variable is directly accessible.
In JS scope is defined by functions, blocks (i.e. { and }) for let, const and by catch blocks.
The following seems to work:
function runnify(arr) {
var runFrom;
return arr.reduce((p,c,i)=>{
if (c + 1 === arr[++i]) { // true if next item is sequential
if (runFrom == null ) { // We are at start of run
runFrom = c;
}
return p;
}
if (runFrom) { // We are at the end of a run
p.push(`${runFrom}-${c}`);
runFrom = null;
return p;
}
p.push(c); // Not sequential
return p;
}, []).join(',');
}
var seq = [1, 3, 4, 5, 7, 9, 10, 11];
document.write(runnify(seq));
Basically, you could collect all items with a check, if consecutive in an array and join it at the end, for consecutive elements with a dash and the rest with comma.
function consecutive(array) {
return array.reduce(function (r, a,i) {
var last = r[r.length - 1];
if (!i || last[last.length - 1] + 1 !== a) {
r.push([a]);
} else {
last[1] = a;
}
return r;
}, []).map(function (a) { return a.join('-'); }).join();
}
var array = [1, 3, 4, 5, 7, 9, 10, 11],
grouped = consecutive(array);
console.log(grouped);

Negating Callback Function Return Value

Straightforward question:
Can I negate a callback function that returns true or false o an array.filter() statement? e.g.
//the callback function
function isEven(element, index, array){
if (index%2 == 0 || index == 0){
return true;
}
return false;
}
//what i want to do with that function
//arr[i] is a string
var evens = arr[i].split("").filter(isEven); //works
var odds = arr[i].split("").filter(!isEven); // doesn't work
the above line gives me the error TypeError: false is not a function
Question with some background:
I'm taking on some Hackerrank challenges and i've come across an exercise that requires to take a string and process it, so the output is: The characters with even index values make a new string and the characters in odd index positions make another string , 0 counts as even.
Input
airplane
Output
evens = 'arln'
odds = 'ipae'
I have already solved it by looping through the string, evaluating the index and then pushing the value to the correspondent new array (which i later convert to a string), but it has occurred to me i could be done in a more functional way, using the Array.prototype.filter() function.
now I create a new function that evaluates whether the index number is even or not, and I'd like to use that same function to fill both arrays (evens and odds), like this (now you can refer to the straightforward question part):
var evens = arr[i].split("").filter(isEven); //works
var odds = arr[i].split("").filter(!isEven); // doesn't work
The simplest way to do this would be to just pass an anonymous function which returns the negated result of isEven.
var evens = arr[i].split("").filter(function(el, index, array) {
return !isEven(el, index, array);
});
But you could take this a step further and write a not function which essentially generates the anonymous function for you. Here's an example of such a function.
var input = [0, 1, 2, 3, 4, 5];
function isEven(value) {
return value % 2 === 0;
}
function not(f) {
return function() {
return !f.apply(null, arguments);
}
}
var output = input.filter(not(isEven));
console.log(output);
If you're in an environment that supports rest parameters then you could write your not function like this.
var input = [0, 1, 2, 3, 4, 5];
function isEven(value) {
return value % 2 === 0;
}
function not(f) {
return function(...args) {
return !f.apply(null, args);
}
}
var output = input.filter(not(isEven));
console.log(output);
You would need to pass in an anonymous function and then negate isEven in there:
var odds = arr[i].split("").filter(function(a, index, array) {
return !isEven(a, index, array);
});
Simple Example:
Working Example
function isEven(n) {
return n % 2 === 0;
}
var arr = [0,1,2,3,4,5,6,7,8,9];
var a = arr.filter(isEven);
var b = arr.filter(function(a) {
return !isEven(a);
});
The solution I use is something like this:
var numbers = [0,1,2,3,4,5];
var evens = [];
var odds = [];
function isEvenOrNot(getEven) {
return function(num) {
if (num % 2 == 0 || num == 0){
return true;
}
return false;
}
}
evens = numbers.filter(isEvenOrNot(true));
odds = numbers.filter(isEvenOrNot(false));
console.log(evens); // [0,2,4]
console.log(odds); // [1,3,5]

Drop last element of javascript array when array reaches specific length

I would like to cache some data in javascript, but the cache should be limited to 10 elements for example.
I can place the objects in javascript array, but what is the best way to keep the array limited to 10 elements?
Example:
function getData(dataId) { return new NextDataObject(dataId); }
var array = new Array();
array.push(getData(0));
array.push(getData(1));
(...)
array.push(getData(10)); // this should result in dropping "oldest" data, so getData(0) should be removed from the array, so that in array there are only 10 objects at maximum
Should such mechanism be written manually (using splice() for example?) or are there better ways to achieve such "cache" structure in javascript?
BTW: in this particular situation I'm using angular.
Override the push function of your caching array.
var array = new Array()
array.push = function (){
if (this.length >= 10) {
this.shift();
}
return Array.prototype.push.apply(this,arguments);
}
Plunker
To make this more reusable I created a method which returns new instance of such array (basing on above code).
function getArrayWithLimitedLength(length) {
var array = new Array();
array.push = function () {
if (this.length >= length) {
this.shift();
}
return Array.prototype.push.apply(this,arguments);
}
return array;
}
var array = getArrayWithLimitedLength(10);
To remove first element from array use shift:
if (arr.length > 10) {
arr.shift(); // removes the first element from an array
}
How about this object?
function Cache(maxLength) {
this.values = [];
this.store = function(data) {
if(this.values.length >= maxLength) {
this.getLast();
}
return this.values.push(data);
}
this.getLast = function() {
return this.values.splice(0,1)[0];
}
}
cache = new Cache(3);
// => Cache {values: Array[0]}
cache.store(1)
// => 1
cache.store(2)
// =>2
cache.store(3)
// => 3
cache.store(4)
// =>3
cache.values
// => [2, 3, 4]
cache.getLast()
// => 2
cache.values
[3, 4]
You could create new method in Array.prototype to mimic your needs.
Array.prototype.push_with_limit = function(element, limit){
var limit = limit || 10;
var length = this.length;
if( length == limit ){
this.shift();
}
this.push(element);
}
var arr = []
arr.push_with_limit(4); // [4]
arr.push_with_limit(9); // [4, 9]
....
// 11th element
arr.push_with_limit(3); // [9, ..., 3] 10 elements
Simple fixed length queue:
Array.prototype.qpush = function( vals, fixed ) {
if (arguments.length) {
if (Array.isArray(vals)) {
for (var v of vals) {
this.push(v);
}
} else {
this.push(vals);
}
var _f = (typeof this.fixed != undefined) ? this.fixed : 0;
if (typeof fixed != undefined) {
_f = (Number(fixed)===fixed && fixed%1===0 ) ? fixed : _f;
}
this.fixed = _f;
if (this.fixed>0) this.splice(0, this.length - _f);
}
}
var q = new Array();
q.push(0);
q.qpush( [1, 2, 3], 10 );
q.qpush( [4] );
q.qpush( 5 );
q.qpush( [6, 7, 8, 9, 10, {k:"object"} ] );
console.log(q);
if(array.length == 10) {
array.splice(0, 1);
// this will delete first element in array
}
If you do a check whether the array has reached 10 entries with array.length, just remove the first element before pushing a new element. This can be done several ways as Tushar states, array.shift() would be the fastest, but you can indeed use array.splice() aswell.
It would look like this:
if(array.length > 10) {
array.shift();
array.push(getData(10));
}
On a side note, instead of using var array = new Array() I suggest you simply use var array = [];. This is because the new keyword in Javascript sometimes has bad side effects. If you for example want to create an array with 1 element being a digit, and you use var arr = new Array(12);, an array with 12 undefined elements will be created. Whereas var arr = [12]; will create an array with 1 element, the digit 12.
But I guess that's a minor thing to consider..
You could use an object instead...
var obj = {}; //your cache object
obj[window.performance.now()] = getData(val); //add value, index by microsecond timestamp
if(Object.keys(obj).length > 10){ // then if the length ever gets bigger than 10..
var array = Object.keys(obj).sort(); //sort the properties by microsecond asc
delete obj[array[0]]; //delete the oldest one
}
Here is a jsFiddle example showing how it works: https://jsfiddle.net/uhkvk4mw/
just check if the length is reached then pop it
if(arr.length > someNumber){
arr.pop(); // pop() will remove the last element
}

Javascript unlimited nested array handling

I am trying to have fun with my buddy who solved the problem mentioned in 8m 7s, and for me it is already 20m gone. I can't figure out how to handle unlimited nested array in javascript.
The problem is this:
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
So
arraySum([[1,2,false],'4','5']) will return 3 (passed)
arraySum([[1,2,3],4,5]) will return 15 (passed)
arraySum([[[[[[[[[1]]]]]]]], 1]) will return 2 (failed)
The code I wrote is:
function arraySum(i) {
sum = 0;
tmp =0;
for (var a=0; a<i.length; a++){
if (i[a] instanceof Array) {
ar = i[a];
for (var j=0; j<ar.length; j++){
tmp +=ar[j];
}
}
if (typeof i[a] == "number")
sum += i[a];
console.log(sum);
}
return sum + tmp;
}
As you can see it does not handle the last situation that I failed as I can't figure out how to handle unlimited nest in JS.
Any idea will be much appreciated.
Also try to finish it before 8m 7s, which my buddy finished in.
Inside of the if (i[a] instanceof Array) { part, you'll have to use recursion to operate on nested arrays with the same arraySum function, not just use another loop. Try this:
var arraySum = (function () {
"use strict";
var sumFunc, isArray;
sumFunc = function (arr) {
var sum, i, j, cur, toAdd;
sum = 0;
for (i = 0, j = arr.length; i < j; i++) {
cur = arr[i];
toAdd = 0;
if (isArray(cur)) {
toAdd = sumFunc(cur);
} else if (typeof cur === "number") {
toAdd = cur;
}
sum += toAdd;
}
return sum;
};
isArray = Array.isArray || function (obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
};
return sumFunc;
}());
DEMO: http://jsfiddle.net/Q7JPM/1
The function just loops through all items in an array, and returns the sum of any numbers found inside. If the item is an array itself, it calls arraySum and passes that array...adding the result to the sum. If it's a number, it simply adds that to the sum.
You have to use recursion:
http://jsfiddle.net/HMnat/2
function arraySumRec(theArray)
{
var sum=0;
for (var i=0;i<theArray.length;i++)
{
if (theArray[i] instanceof Array)
{
sum=sum+arraySumRec(theArray[i]);
}
else
{
if (typeof(theArray[i])=="number")
{
sum=sum+theArray[i];
}
}
}
return sum;
}
Took me 3 minutes 47 seconds (due to a typo, ha ha).
The Javascript Array reduce method is perfect for solving this kind of problem. The reduce method takes a function with at least two arguments: the accumulator and the current element of the array. In the body of the function, you specify how each element should affect the accumulator. The second argument to the function is the starting value of the accumulator.
function sum(x) {
return x.reduce(function(accumulator, currentValue) {
if (typeof currentValue === "number") {
return accumulator + currentValue;
} else if (currentValue instanceof Array) {
return accumulator + sum(currentValue);
} else {
return accumulator;
}
}, 0);
}
JSFIDDLE
The function sum takes an array, and the reduce method reduces it to a single value. In the "else if" branch, where we find a nested array, we can simply call sum on it, get back a single value, and add that to our accumulator. In the "else" branch, we haven't found the kinds of values we're interested in so we leave the accumulator unchanged.
The documentation at MDN provides a good explanation of Array reduce with examples.
function arraySum(i) {
var l = i.length, sum = 0;
while (l--) {
if (typeof i[l] !== 'number' && !(i[l] instanceof Array)) continue;
if (i[l] instanceof Array) { sum += arraySum(i[l]); continue; }
sum += i[l];
}
return sum;
}
Non-Recursive using a stack.
function arraySum(arr)
{
var sum = 0;
while(arr.length != 0)
{
var value = arr.pop();
if(value instanceof Array)
{
for (i= 0; i< value.length; ++i)
arr.push(value[i]);
}
else if(typeof value === "number")
sum += value;
}
return sum;
}
var arr = [1, 2, [3, 4, [[[5]]]]];
console.log(arraySum(arr));
If we focus on the right parts, we can save ourselves the tedium from focusing on the wrong parts -
function arraySum (t)
{ switch (t?.constructor)
{ case Array:
return t.reduce((r, v) => r + arraySum(v), 0)
case Number:
return t
default:
return 0
}
}
console.log(arraySum([[1,2,false],'4','5']))
console.log(arraySum([[1,2,3],4,5]))
console.log(arraySum([[[[[[[[[1]]]]]]]], 1]))
3
15
2
If ?. is not yet supported in your environment, you can swap it out -
switch (t?.constructor)
switch (t && t.constructor) // <- where ?. is unsupported
Edit: it took me 2,769 days to answer the question but only a few minutes to write it :D

Categories