I have a code written as a function which I would like to automate in another function as long as a certain condition is not met, push the new values in a variable[] like a nested array and return the length of the variable. But the command line just freezes. This is what I have done so far.
First function to be automated which is ok:
function sh(v){
var f = [];
if (v.length == 3){
for (let i = 1; i < v.length; i += 2){
if(v[i - 1] < v[i]){
f.push(v[i - 1]);
}
}
}
else{
for (let i = 1; i < v.length; i += 2){
if(v[i - 1] < v[i]){
f.push(v[i - 1]);
}
}
}
if (v.length % 2 == 1){
var a = v[v.length -1]
f.push(a)
}
return f;
}
Now I want to automate like this:
/*(Point 1)This function should take the results of sh() which is an array and apply sh() repeatedly as long as the given array.length > 1 */
/*Whenever the array is shrunk by sh() function , it should push the new array into an initial empty array var receive = [] , till when the condition is satisfied the empty array will be nested with arrays receive = [[], []] , then return receive.length*/
function automated(x){
var receive = [];
receive.push(sh(x))
receive = receive
while(x.length > 1){
for(let g = 0; g < receive.length; g++){
/*attempting Last index of receive*/
let c = receive[g] - 1
}
receive.push(automated(receive[c]))
}
return receive.length
}
Expected results:
/*If i input an array like*/
z = [ 6, 5, 8, 4, 7, 10,9]
sh (z)
/*Outputs :*/
[7,9]
/*If sh is automated and retakes in [7,9] as input,
Outputs:*/
[7]
/*[7, 9] and [7] should be nested into receive = [] as receive = [ [7,9] , [7]]
Then return receive.length which is 2.*/