Just as a curiosity, Is it possible to return an element of an array during recursive iteration?
var index = 0;
function iterArray(arr){
if(arr && index <= arr.length ){
console.log(arr[index]); //Wanted to return this value instead calling any function here
index++
iterArray(arr)
}
}
NOTE :- Above code will not execute as I expect., But I want that it should work sort of arr.pop. Like:
k = ["a", "b", "c"];
ret1 = iterArray(k);
ret2 = iterArray(k);
ret3 = iterArray(k);
console.log(ret1, ret2, ret3)//"a", "b", "c"
Array.pop doesn't need recursive implementation.
But if it's general question - I'd think of some ECMAScript 6 generators.
function* iterArray(arr, idx) {
yield arr[idx];
yield* iterArray(arr, ++idx);
}
var arr = [1,2,3];
var g = iterArray (arr, 0);
console.log(g.next().value);
console.log(g.next().value);
console.log(g.next().value);
It's easy with generators, but to do similar things in ES5 environment - you'll need to transpile this code, or implement similar mechanism manually.
P.S: I used babel for transpilation.
This function may be use for searching exist key in array
function iterArray(arr){
if(arr instanceof Array && arr.length) {
return arr.shift();
};
}
k = ["a", "b", "c"];
ret1 = iterArray(k);
ret2 = iterArray(k);
ret3 = iterArray(k);
console.log('ret1 '+ ret1 + ' ret2 '+ ret2 + ' ret3 ' + ret3);
But, a function iterArray() changed a original array. Be carefully use this function
Function recurseOverArrayLookingForValue(array, value, currentIndex){
if (currentIndex == undefined)
{
currentIndex = 0;
}
if (currentIndex >= array.length)
{
Return -1; //not found
}
if (array[currentIndex] == value){
return currentIndex;
}
RecurseOverArrayLookingForValue(array,value,currentIndex);
}
That will recursively iterate over an array and return a value that matches the input. You call it without defining the currentIndex and it will start at the beginning and recurse to the end or you can define it and recurse from there.
The thing about recursion is you have to have a predefined exit state that will cause the function to terminate execution. Otherwise you get an infinite loop, basically. So in your example, you need to define some reason to return the value in that index or otherwise continue to recurse until you hit the end of the the array at which point you should, again, exit the function - this is why I choose to add the look for a value logic. If you're not looking for a specific value then you need to define another reason to exit returning a given value. Maybe call another function that returns true ilor false based on whatever you need the number for or if it meets certain criteria. Otherwise your exit status will always be the end of the array.
I don't see the need of recursion here, if you want to return a function then re-run the function you can use setTimeout depending on the framework, will work just fine in browsers.
var index = 0;
function iterArray(arr, index){
if(arr && index <= arr.length ){
index++;
setTimeout( fucntion(){iterArray(arr, index);^, 10 ); // Index is a global var, why you're passing it?
return (arr[index]);
}
}
This will continue the recursion but will return just the first match. To do what you're trying to accomplish by your example, the best way AFAIK is to mimic a class:
function nextIndexContainer( arrayToStore ){
// Make a copy so it doesn't get changed on the fly, or remove the JSON's if this behaviour is desireable
this.array = JSON.parse( JSON.stringify( arrayToStore ) );
this.index = 0;
this.next = function(){
if( this.array[ this.index ] )
return this.array[ this.index++ ];
else
return 'Last index reached';
}
}
To use it:
var arrIndex = new nextIndexContainer([1, 5, 8]);
console.log(arrIndex.next()); // '1'
console.log(arrIndex.next()); // '5'
console.log(arrIndex.next()); // '8'
console.log(arrIndex.next()); // 'Last index reached'
Related
I'm trying to use findIndex() on a object in google script but it does not work. Here is a exemple:
function myFunction() {
var searchingJohn = "John";
var DATA = {namesList: []};
DATA.namesList.push({name: "John", otherDataForThisName: []});
var result = DATA.namesList.findIndex(function (element)
{return element.name == this;}, searchingJohn);
return result;
}
this work well in a javascript consol but google script return me a "TypeError: Fonction findIndex not found in object....."
You can't.
You are trying to use findIndex() on a object, but findIndex() is for arrays.
From the MDN documentation on findIndex():
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
The reason is that objects don't hold key-value pairs in any particular order. For example,
{key1:value1, key2:value2}
and
{key2:value2, key1:value1}
are exactly the same object. So there is no index to be found.
This is the example of using findIndex() function for searching string in object in google script. Example:
function myFunction() {
var DATA = {namesList: []};
DATA.namesList.push({name: "John", otherDataForThisName: []});
DATA.namesList.push({name: "Mark", otherDataForThisName: []});
DATA.namesList.push({name: "Twin", otherDataForThisName: []});
const searching = "Mark";
var result = DATA.namesList.findIndex(function (element){
return element.name.toLowerCase() == searching.toLowerCase();
});
return result;
}
findIndex() function is also use for 2 dimensional array.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cellRange = ss.getRange("B5:Q10").getValues();
var someString = "John";
if (cellRange == "John")
{
//Do this code.
}
This is the only way I can think to pull out the information you're trying to get. IndexOf(); pulls from an index ergo if "John" was in an array it would look like this ["J", "o", "h", "n"] which is why it can't be found that way.
The code above will find it in a range of cells and you can do the whole sheet but the more "Empty" you add to it the slower it will run. Another nested if loop could clean that up for you if you need a massive sheet checked.
I have the same issue with many basic javascript functionalities working with gscript, I added the polyfill at the top of my script and it worked with many functionalities.
Paste this code on the top of your script, it will support indexOf.
// This version tries to optimize by only checking for "in" when looking for undefined and
// skipping the definitely fruitless NaN search. Other parts are merely cosmetic conciseness.
// Whether it is actually faster remains to be seen.
if (!Array.prototype.indexOf)
Array.prototype.indexOf = (function(Object, max, min) {
"use strict"
return function indexOf(member, fromIndex) {
if (this === null || this === undefined)
throw TypeError("Array.prototype.indexOf called on null or undefined")
var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len)
if (i < 0) i = max(0, Len + i)
else if (i >= Len) return -1
if (member === void 0) { // undefined
for (; i !== Len; ++i) if (that[i] === void 0 && i in that) return i
} else if (member !== member) { // NaN
return -1 // Since NaN !== NaN, it will never be found. Fast-path it.
} else // all else
for (; i !== Len; ++i) if (that[i] === member) return i
return -1 // if the value was not found, then return -1
}
})(Object, Math.max, Math.min)
reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill
My understanding is that the contents of a while loop executes while the condition is true. While working off of an example from an awesome O'Riely book, I've come across this implementation of the while loop...
window.onload = function(){
var next, previous, rewind; // globals
(function(){
// Set private variables
var index = -1;
var data = ['eeny', 'meeny', 'miney', 'moe'];
var count = data.length;
next = function(){
if (index < count) {
index ++;
};
return data[index];
};
previous = function(){
if (index <= count){
index --;
}
return data[index];
};
rewind = function(){
index = -1;
};
})();
// console.log results of while loop calling next()...
var a;
rewind();
while(a = next()){
// do something here
console.log(a);
}
}
I guess I'm wondering why, in this code, the while loop is not resolving to true infinitely? The function, next() isn't returning false after var index stops incrementing (++), is it? Shouldn't the console just be outputting eeny, meeny, miney, moe, moe, moe, moe.....etc...
I know this has probably been asked in some form, but have done searching and can't find a question or answer that explains using while (a = function()) {// do something} and how this loop is stopping after one pass through the array.
About why while (a = next()) {/*do something*/} doesn't repeat infinitely, it's about being coerced to false that counts - arguments are converted to booleans before being tested by the while loop. Things that coerce to false include 0, -0, undefined, null, "", NaN, and of course false itself.
When you assign something, it returns the value of the assignment itself. For example, if you do something like this:
var a;
console.log(a = '1234567890abcdefghijklmnopqrstuvwxyz');
It will log 1234567890abcdefghijklmnopqrstuvwxyz.
When the next performs index++, this increments the counter for the element index in the data array. This means that it will look for the next element in the data array every time you run the next() function - if there are no more elements, it will return undefined and therefore end the loop.
For example, see this:
var index = 0;
data = ['a','b','c'];
data[index]; // 'a'
index++;
data[index]; // 'b'
index++;
data[index]; // 'c'
index++;
data[index]; // undefined - if passed this will coerce to false and end the loop
Boolean(data[index]); // false
if (index < count) {
index ++;
};
When index is count - 1, this will still change index to count, right? And count is data.length. So, it then does this:
return data[index];
Which becomes
return data[data.length];
Since the length of an array is out of bounds of the array (they are zero-based), it will give undefined.
while(a = next()){
will become
while(a = undefined){
Since undefined is a falsy value, the loop will not be entered.
No,
It is not going to be an infinite loop. The while loop is basically going through the array and outputting it and when it is at the end of the array it just returns false and quits the loop.
This is something like;
foreach(a as nextArray)
{
//output
}
Hope this helps.
I have a function that computes product of numbers in an array. The function should work like this
function prod (array){
//compute and return product
}
var arr = [1,2,3,0,4,5,0,6,7,8,0,9];
the function call:
prod(arr); //should return 6
prod(arr); //should return 20
prod(arr); //should return 336 (6*7*8)
prod(arr); //should return 9
prod(arr); //should return 0
prod(arr); //should return 0
prod(arr); //should return 0
In scheme, this is done with continuations, by storing previous state of the function (state of the function is captured just before its exit point) see this
So, in short, I want the javascript function return different values at different times with same parameter passed everytime.
JavaScript is a well designed language, so I hope there must be something which can emulate this. If there happens to be nothing in JS to do it, I do not mind to conclude with failure and move on. So, feel free to say its impossible.
Thanks.
JavaScript is not capable of supporting continuations: it lacks tail-calls.
Generally I would write this to use a "queue" of sorts, although CPS is also do-able (just have a finite stack :-) Note that other state can also be captured in the closure, making it an "explicit continuation" of sorts ... in a very gross sense.
Example using a closure and a queue:
function prodFactory (array){
// dupe array first if needed, is mutated below.
// function parameters are always locally scoped.
array.unshift(undefined) // so array.shift can be at start
// also, perhaps more closured state
var otherState
// just return the real function, yippee!
return function prod () {
array.shift()
// do stuff ... e.g. loop array.shift() and multiply
// set otherState ... eat an apple or a cookie
return stuff
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
// array at "do stuff", at least until "do stuff" does more stuff
prod() // [1,2,3,0,4,5,0,6,7,8,0,9]
prod() // [2,3,0,4,5,0,6,7,8,0,9]
prod() // [3,0,4,5,0,6,7,8,0,9]
Happy coding.
"Finished implementation". Although this particular problem can avoid array mutation and just use an index: the same concepts apply. (Well, slightly different. With just an index the closed over variable would be altered, whereas with this approach an object is mutated.)
function prodFactory (array) {
array = array.slice(0)
return function prod () {
var p = 1
for (var n = array.shift(); n; n = array.shift()) {
p *= n
}
return p
}
}
var prod = prodFactory([1,2,3,0,4,5,0,6,7,8,0,9])
prod() // 6
prod() // 20
prod() // 336
You can give the function a property that will be remembered between calls:
function prod (array){
if (typeof prod.index === "undefined" || prod.currentArray != array) {
prod.currentArray = array;
prod.index = 0;
}
if (prod.index >= array.length)
return 0;
//compute and return product
var p = 1,
c;
while (prod.index < array.length) {
c = array[prod.index++];
if (c === 0)
return p;
p *= c;
}
return p;
}
I'm just guessing from your description of what should be returned that on an individual call to the function it should take the product of all of the numbers up to but not including the next zero or the end of the array. Calls after the end of the array should return 0? I may have the algorithm wrong for that, but you get the idea for what I'm suggesting to remember the function state between calls.
I've added a property to remember the current array being processed. As long as you keep passing the same array in to the function it will continue with the next elements, but if you pass a different array it will reset...
you can try something like
var index = 0;
function prod (array){
if(index < array.length){
var prod=1;
for(int i=index;i<array.length;i++){
if(array[i] != 0){
prod = prod * array[i];
}
else{
index = i+1;
return prod;
}
}
}
return 0;
}
this will update the global variable index everytime the function is called.
What you're looking for here are generators. As of 1.7, JavaScript supports them.
Possible Duplicate:
Easiest way to find duplicate values in a javascript array
How do I check if an array has duplicate values?
If some elements in the array are the same, then return true. Otherwise, return false.
['hello','goodbye','hey'] //return false because no duplicates exist
['hello','goodbye','hello'] // return true because duplicates exist
Notice I don't care about finding the duplication, only want Boolean result whether arrays contains duplications.
If you have an ES2015 environment (as of this writing: io.js, IE11, Chrome, Firefox, WebKit nightly), then the following will work, and will be fast (viz. O(n)):
function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
If you only need string values in the array, the following will work:
function hasDuplicates(array) {
var valuesSoFar = Object.create(null);
for (var i = 0; i < array.length; ++i) {
var value = array[i];
if (value in valuesSoFar) {
return true;
}
valuesSoFar[value] = true;
}
return false;
}
We use a "hash table" valuesSoFar whose keys are the values we've seen in the array so far. We do a lookup using in to see if that value has been spotted already; if so, we bail out of the loop and return true.
If you need a function that works for more than just string values, the following will work, but isn't as performant; it's O(n2) instead of O(n).
function hasDuplicates(array) {
var valuesSoFar = [];
for (var i = 0; i < array.length; ++i) {
var value = array[i];
if (valuesSoFar.indexOf(value) !== -1) {
return true;
}
valuesSoFar.push(value);
}
return false;
}
The difference is simply that we use an array instead of a hash table for valuesSoFar, since JavaScript "hash tables" (i.e. objects) only have string keys. This means we lose the O(1) lookup time of in, instead getting an O(n) lookup time of indexOf.
You could use SET to remove duplicates and compare, If you copy the array into a set it will remove any duplicates. Then simply compare the length of the array to the size of the set.
function hasDuplicates(a) {
const noDups = new Set(a);
return a.length !== noDups.size;
}
One line solutions with ES6
const arr1 = ['hello','goodbye','hey']
const arr2 = ['hello','goodbye','hello']
const hasDuplicates = (arr) => arr.length !== new Set(arr).size;
console.log(hasDuplicates(arr1)) //return false because no duplicates exist
console.log(hasDuplicates(arr2)) //return true because duplicates exist
const s1 = ['hello','goodbye','hey'].some((e, i, arr) => arr.indexOf(e) !== i)
const s2 = ['hello','goodbye','hello'].some((e, i, arr) => arr.indexOf(e) !== i);
console.log(s1) //return false because no duplicates exist
console.log(s2) //return true because duplicates exist
Another approach (also for object/array elements within the array1) could be2:
function chkDuplicates(arr,justCheck){
var len = arr.length, tmp = {}, arrtmp = arr.slice(), dupes = [];
arrtmp.sort();
while(len--){
var val = arrtmp[len];
if (/nul|nan|infini/i.test(String(val))){
val = String(val);
}
if (tmp[JSON.stringify(val)]){
if (justCheck) {return true;}
dupes.push(val);
}
tmp[JSON.stringify(val)] = true;
}
return justCheck ? false : dupes.length ? dupes : null;
}
//usages
chkDuplicates([1,2,3,4,5],true); //=> false
chkDuplicates([1,2,3,4,5,9,10,5,1,2],true); //=> true
chkDuplicates([{a:1,b:2},1,2,3,4,{a:1,b:2},[1,2,3]],true); //=> true
chkDuplicates([null,1,2,3,4,{a:1,b:2},NaN],true); //=> false
chkDuplicates([1,2,3,4,5,1,2]); //=> [1,2]
chkDuplicates([1,2,3,4,5]); //=> null
See also...
1 needs a browser that supports JSON, or a JSON library if not.
2 edit: function can now be used for simple check or to return an array of duplicate values
You can take benefit of indexOf and lastIndexOf. if both indexes are not same, you have duplicate.
function containsDuplicates(a) {
for (let i = 0; i < a.length; i++) {
if (a.indexOf(a[i]) !== a.lastIndexOf(a[i])) {
return true
}
}
return false
}
If you are dealing with simple values, you can use array.some() and indexOf()
for example let's say vals is ["b", "a", "a", "c"]
const allUnique = !vals.some((v, i) => vals.indexOf(v) < i);
some() will return true if any expression returns true. Here we'll iterate values (from the index 0) and call the indexOf() that will return the index of the first occurrence of given item (or -1 if not in the array). If its id is smaller that the current one, there must be at least one same value before it. thus iteration 3 will return true as "a" (at index 2) is first found at index 1.
is just simple, you can use the Array.prototype.every function
function isUnique(arr) {
const isAllUniqueItems = input.every((value, index, arr) => {
return arr.indexOf(value) === index; //check if any duplicate value is in other index
});
return isAllUniqueItems;
}
One nice thing about solutions that use Set is O(1) performance on looking up existing items in a list, rather than having to loop back over it.
One nice thing about solutions that use Some is short-circuiting when the duplicate is found early, so you don't have to continue evaluating the rest of the array when the condition is already met.
One solution that combines both is to incrementally build a set, early terminate if the current element exists in the set, otherwise add it and move on to the next element.
const hasDuplicates = (arr) => {
let set = new Set()
return arr.some(el => {
if (set.has(el)) return true
set.add(el)
})
}
hasDuplicates(["a","b","b"]) // true
hasDuplicates(["a","b","c"]) // false
According to JSBench.me, should preform pretty well for the varried use cases. The set size approach is fastest with no dupes, and checking some + indexOf is fatest with a very early dupe, but this solution performs well in both scenarios, making it a good all-around implementation.
function hasAllUniqueChars( s ){
for(let c=0; c<s.length; c++){
for(let d=c+1; d<s.length; d++){
if((s[c]==s[d])){
return false;
}
}
}
return true;
}
Live code: http://jsfiddle.net/fCUZC/
//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
entireLoop:
for (var i = 1; i<=input.length; i++)
{
if(input[i] > input[i-1])
{
for(var o = i; o>=0; o--)
{
if(input[i-1] > input[o])
{
input.splice(i,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
else if(input[o] > input[0])
{
input.splice(0,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
}
}
}
document.write(input);
I'm trying to order the array from largest to smallest, but there's a 32 stuck somewhere. I know there's the sort method, but I'm a newbie and want to try this for myself.
** edit **
First have a look at the Array's native .sort() method. It leaves the original array intact and accepts a comparison function. The latter makes .sort() pretty powerful.
var input = [28,32,21,11,8,2,14,32,64];
var low2high = function ( a , b ) {
return a > b;
};
var high2low = function ( a , b ) {
return a < b;
};
var resultHigh2low = input.sort( high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = input.sort( low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
So if we want to use bubbleSort ( link provided by T.J. Crowder , see OP comments ) we can write the following:
// javascript bubbleSort implementation
var bubbleSort = function ( list , comparison ) {
var swapped;
var i;
var val;
list = [].concat( list ); // do not destroy original
comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
do {
i = list.length;
while ( --i ) {
if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
val = list[ i ];
list[ i ] = list[ i - 1 ];
list[ i - 1] = val;
swapped = true;
}
}
} while ( swapped );
return list;
}
// using comparison functions from previous example.
var resultHigh2low = bubbleSort( input , high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = bubbleSort( input , low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
Lets walk through it step by step:
var bubbleSort = function ( list , comparison ) {
..code..
}
Our function accepts 2 parameters, first the array and 2nd an optional comparison function.
var swapped;
var i = list.length;
var val;
We store the list's length under variable i, and declare 2 empty variables ( swapped and val ) we're going to use later on.
list = [].concat( list ); // do not destroy original
We clone the list using [].concat( array ) and overwrite the local list variable leaving the original intact.
comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
We test the typeof the comparison argument, if it's a function we use that one, otherwise we fall back on our own comparison function. Our fallback comparison function will return true if a is bigger than b.
do {
..code..
} while ( swapped );
A do/while loop will run at least once, our swapped variable is currently undefined so it will be interpreted as falsy. If our comparison function returns true, a swap occurs and the swapped variable will be set to true, so it will loop again.
while ( --i ) {
..code..
}
Here I loop from the list's length downward, the -- operator is put before the i variable to ensure it is handled first before anything, i-- would go off after while evaluation causing erronous results since list[ list.length ] does not exist. I always do it this way (bad habbit perhaps), but if it confuses you, go for absolute transparancy.
if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
..code..
}
First we check if i has a truthy value ( 0 evaluates to falsy ) and then we run the comparison function passing list[ i ] and list[ i - 1 ] as a and b parameters. If the comparison function returns true, we perform a swap.
val = list[ i ];
list[ i ] = list[ i - 1 ];
list[ i - 1] = val;
swapped = true;
Here I perform the swap without using the .splice() method, it's just an educated guess atm., but I figure direct assignments are faster then function calls. I use the val variable as a place holder. After the swap is done, I set swapped to true so our do/while loop will continue.
return list;
Well... return the result.
I've excluded some checks, like what do we do when the list's length is 0 and whatnot. Basically when writing helper functions, we also need to deal with error handling. Like for example throwing a TypeError when the passed comparison argument is not a function, ensuring the comparison method returns a boolean value and so on.
//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
for (var i = 1; i<input.length; i++)
{
if(input[i] > input[i-1])
{
for(var o = i-1; o>=0; o--)
{
if(input[i] > input[o])
{
input.splice(i+1,0,input[o]);
input.splice((o),1);
i--;
}
}
}
}
document.write(input);
While it's still not great, it should work. Keep in mind I barely tested this and I'm fairly inexperienced with javascript. Your intentions weren't all bad and everyone needs to start somewhere.
The biggest issue was simply the inner conditional. I can see your logic of looping backwards from the large value you've found and pushing all smaller values to the right. Unfortunately your indexes are a little off. You also need to run this loop until it finishes normally instead of continuing, else you'll only switch one value. When this conditional is fixed the second one is no longer needed
The alternate form would have been to start from the lowest index and find the first value that is smaller than input[i] and place it there. This is potentially clearer.
I think this was a pretty good first shot and wasn't that hard to get working. Good luck!