So we are practicing functional javascript in my programming class with this assignment and I just can't get it to work right. Any advice in making this code work would be appreciated. Everything but the body was given for me to use. Here is what I have come up with:
(It is always sending me just the first array index content rather than all of them reversed. I tried changing it to
if(arr.length <= 1) return arr;
but that never hits the base case.)
function ReverseArray(arr) {
//base case
if(arr.length == 1)
{
return arr[0];
}
if(arr.length == 0)
{
return 0;
}
var head = arr.pop;
var newArr = [head, ReverseArray(arr)];
return newArr;
}
x = y <--assignment
z == y <-- comparison
Looking at your code:
if(arr.length = 1)
needs to be
if(arr.length == 1)
same with the zero check
AND you are not calling pop
var head = arr.pop;
you need to parenthesis
var head = arr.pop();
This is the most precise and clean way to do it in a single line with the ternary operator.
function reverse(arr) {
return arr.length < 2 ? arr : [arr.pop()].concat(reverse(arr));
}
console.log(reverse([4, 3, 3, 1]));
Here's a runnable example with a working function. FYI there is also a built-in function that will do this for you.
I think other than the confusion with assignment/comparison operators, when you were building the result array, you should be using Array.concat() to concatenate your array, instead you were building a new array where the second item was an array itself.
var a = [1,2,3,4,5];
console.log(ReverseArray(a));
function ReverseArray(arr) {
if(arr.length < 2) {
return arr;
} else {
return [arr.pop()].concat(ReverseArray(arr));
}
}
Related
I am trying to write a function called slice that accepts an array and two numbers.
The function should return a new array with the elements starting at the index of the first number and going until the index of the second number.
If a third parameter is not passed to the function, it should slice until the end of the array by default.
If the third parameter is greater than the length of the array, it should slice until the end of the array.
function slice(s, n, m) {
let a = [];
a = s.splice(n, m);
if(m === undefined || m > s.length) {
a = s.splice(n, s.length);
}
return a;
}
let s = [1, 2, 3, 4, 5];
slice(s, 1, 7);
output []
UPDATE:
Thanks for everyone's help; I GOT IT!!! happy dance
function slice(arr, start, end) {
let result = [];
from = Math.max(start, 0);
to = Math.min(end);
if((!end) || (end > arr.length)) {
for(let i = from; i<arr.length; i++) {
result.push(arr[i]);}
} else {
for(let i = from; i<to; i++) {
result.push(arr[i]);
}
}
return result;
}
slice([1, 2, 3, 4, 5], 2, 4);
The main problem is that .splice mutates the array, so on your first call to it you remove a part of the array (or in your case the whole array). Therefore if the code enters the if, and you call .splice a second time, the array is already empty. An if / else would work so that .splice gets only called once.
But that would still then not replicate the behaviour of .slice, as .slice does not mutate the original array. Therefore you rather need a loop, that copies one element after the other:
// if "do" doesn't get passed, initialize with array.length (default parameter)
function slice(array, from, to = array.length) {
// make sure the bounds are within the range
from = Math.max(from, 0);
to = Math.min(to, array.length);
// initialize an array we can copy values into
const result = [];
for(let index = from; index < to; index++) {
// left as an exercise :)
}
return result;
}
Answering this since the OP said the time for the homework has passed.
One way to solve this problem is to have two pointers to second and third arguments. If you have all the arguments given, this is how you should start
start = n
end = m
// if m is greater than length of s or if m is not given
if(m == undefined || m > s.length()){
end = s.length() - 1;
}
then it is a simple for loop from start to end, both inclusive.
int[] result = new int[end-start+1];
for(int i = start; i <= end; i++){
result[j] = s[i];
}
Code may not be syntactically correct but you can fix that.
I am learning Javascript, and I saw a function on SO for comparing arrays to check if they are same. However, my current function returns false if the two arrays are [string1,string2] & [string2,string1]. Basically, the two positions are interchanged. The code is as follows:
function _compareArrays(arr1,arr2){
var result = arr1 != null && arr2 != null && arr1.length == arr2.length && arr1.every(function(element) {
return arr2.indexOf(element);
});
return result ;
}
However, I want these two arrays to be returned as same. So, I changed .every to .indexOf() and it seemed to work. But, I have a doubt, how exactly is the counter getting incremented here to make sure the comparison is being done for every element?
I mean, like, in C++, we do,
for (int i = 0; i < 10; i++)
if (arr1[i] == arr2[i]
cout<<"Elements are same\n";
Here, I have an explicit i++ which increments the counter. How does it happen in the above function?
Thanks!
Convert the array into an object instead. Convert array values into keys and their respective count as their value. This is more performant as you iterate over the arrays only once.
function compare(a, b) {
if (a.length !== b.length) {
return false;
}
let set = {};
a.forEach((i) => {
if (set[i] !== undefined) {
set[i]++;
} else {
set[i] = 1;
}
});
let difference = b.every((i) => {
if (set[i] === undefined) {
return false;
} else {
set[i]--;
if (set[i] === 0) {
delete set[i];
}
return true;
}
});
return Object.keys(set) == 0 && difference;
}
The first loop on the first array initialises the the set (object), the second loop on the second array subtracts the count and removes the keys when the count hits 0. If a key is not found or if the set is not empty at the end of the procedure, then the arrays are not similar.
Your current has these issues:
It will return true for these 2 arrays (I hope that you understand that this is not specific to these 2): [1, 2, 2], [1,1,2]. This problem is why I set indices to undefined after a match in my solution below.
indexOf returns -1 for element's it cannot find, and any number >= 0 if it can find the element, -1 is truthy, so if an element cannot be found, your comparison will return true, and 0 is falsy, so if an element is located in the first index of the second array, your method will return false (which means the whole thing will become false, because of every...). So, you should add a ~ before the result of indexOf call: ~arr2.indexOf(element).
See this
MDN page on the Bitwise Not operator (~) and how it solves the
indexOf problem I mentioned.
I also recommend that you take a look at this answer of mine on truthy/falsy values and how they interact with && and ||.
So Try this out (it's mostly your example, except there is no indexOf used and I have fixed problem #2):
function _compareArrays(arr1,arr2){
if(!(arr1 != null && arr2 != null && arr1.length == arr2.length)) {
return false;
}
/* copy the arrays so that the original arrays are not affected when we set the indices to "undefined" */
arr1 = [].concat(arr1);
arr2 = [].concat(arr2);
return arr1.every(function(element, index) {
return arr2.some(function(e, i) {
return e === element && (arr2[i] = undefined, true);
});
});
}
var x = ["str", "boo", "str"];
var y = ["boo", "str", "str"];
var z = ["abc", "def", "ghi"]
console.log(_compareArrays(x, y));
console.log(_compareArrays(x, z));
console.log(_compareArrays(z, z));
It won't work if the array has any undefined elements, though.
So the fastest way would be to sort both arrays, then compare each one element by element. This requires 2n * log(n) + n time rather than n2 time.
function compareArrays(arr1, arr2){
if(arr1.length !== arr2.length) return false;
// implement custom sort if necessary
arr1.sort();
arr2.sort();
// use normal for loop so we can return immediately if not equal
for(let i=0; i<arr1.length; i++){
if(arr1[i] !== arr2[i]) return false;
}
return true;
}
I want to enhance merge sort algorithm so that it sorts two arrays and removes duplicates.
I've came up with the following code:
function mergeSortEnhanced(arr, arr2)
{
while(arr2.length)
{
arr.push(arr2.shift());
}
if (arr.length < 2)
return arr;
var middle = parseInt(arr.length / 2);
var left = arr.slice(0, middle);
var right = arr.slice(middle, arr.length);
return merge(mergeSortEnhanced(left), mergeSortEnhanced(right));
}
function merge(left, right)
{
var result = [];
while (left.length && right.length) {
if (left[0] < right[0])
{
result.push(left.shift());
}
else if(left[0] > right[0])
{
result.push(right.shift());
}
else
{
result.push(left.shift());
right.shift()
}
}
while (left.length)
result.push(left.shift());
while (right.length)
result.push(right.shift());
return result;
}
var a = [1, 2 , 2 , 1];
var b = [1, 1, 6 ,8];
console.log(mergeSortEnhanced(a, b).join());
The problem is that i encounter an error in the fourth line
while(arr2.length)
which states that compiler cannot compute length of undefined. i don't understand why the second array is undefined inside the function and how do I fix it
The problem is with the
return merge(mergeSortEnhanced(left), mergeSortEnhanced(right));
you pass only first parameter in function here, thus second one is undefined.
maybe you wanted to do
return merge(left, right);
BTW just as an advice - be extremely careful with recursion
Add check before while. Update your code as
if (arr2){
while(arr2.length)
{
arr.push(arr2.shift());
}
}
The shift() method removes the first item of an array, and returns that item. So after 4 iterations your arr2 becomes undefined.
Its a very small issue and for the life of me I can't figure out what it is. My brain has locked itself from thinking. I need someone else to have a look this code.
The output of the code should be: [1,0,0,0]
UPDATE:
The function should be able to read an array of numbers and if it finds any zeros within the array it should move them to the end of the array.
The output of the code keeps coming as: [0,1,0,0]
var arrNum = [0,0,0,1];
function test() {
for(var i=0; i<arrNum.length; i++){
if(arrNum[i] == 0){
arrNum.splice(i,1)
arrNum.splice(arrNum.length, 1, 0)
}
}
return alert(arrNum)
}
Here is a working plunker.
Apologies for this, I know the issue is something very small but my brain has stopped working now and I need a fresh pair of eyes.
With the way you have it written, you need to loop in the reverse order. You end up skipping indexes when you remove the index. Looping in the reverse direction keeps you from skipping them.
for(var i=arrNum.length-1; i>=0; i--){
You can use unshift() to insert at beginning of an array and push() to the end...
var arrNum = [0,0,0,1];
var output = [];
function test()
{
for(var i=0; i<arrNum.length; i++)
{
if(arrNum[i] == 0)
output.push(0);
else
output.unshift(arrNum[i]);
}
return alert(output)
}
var arrNum = [0,0,0,1];
var result = [];
arrNum.forEach(function(v) {
!!v ? result.unshift(v) : result.push(v);
});
console.log(result);
You are iterating with index i = 0,1,2,3 and at the same time removing first elements of array. So your iteration can not see the 1, it jumps over as it is moved to already iterated index. Easiest would be to just reverse the loop to bypass the issue.
var arrNum = [0,0,0,1];
function test() {
for(var i= arrNum.length; i >= 0; i--){
if(arrNum[i] == 0){
arrNum.splice(i,1)
arrNum.splice(arrNum.length, 1, 0)
}
}
return alert(arrNum)
}
Prefer built-in functions every time possible.
var output = [];
[0,0,0,1].forEach(function(num) {
if(num == 0) output.push(0);
else output.unshift(num)
})
Why don't you use a temporary array to help? The problem with your code is that the splice() function modifies the original array, and you are doing it inside the loop.
The code below produces what you need:
var arrNum = [0,0,0,1];
var arrResult = new Array();
function test() {
for(var i=arrNum.length-1; i>=0; i--)
{
arrResult.push(arrNum[i]);
}
arrNum = arrResult;
return alert(arrNum);
}
With another array to store the new values, you gain flexibility to do whatever you need with the data of the first array.
A nice little way using Objects - busy learning them so just posting a variation of deligation
var methods = {
moveZero: function(arr){
//console.log(arr);
var newArr = [];
for(var i = 0; i < arr.length; i++){
if(arr[i] === 0){
newArr.push(arr[i]);
}else{
newArr.unshift(arr[i]);
}
}
console.log(newArr);
}
}
var arrNum = Object.create(methods);
arrNum.moveZero([0,0,50,56,85,0,0,43,10,0,1]);
JSFiddle - https://jsfiddle.net/ToreanJoel/qh0xztgc/1/
The problem was you are modifying an array while looping over it in if statement.
Here is a working plunker of your example.
var len = arrNum.length;
var index = 0;
while(len) {
if(arrNum[index] == 0) {
arrNum.splice(index,1);
arrNum.push(0);
} else {
++index;
}
--len;
}
As the operation you want to do is actually sorting, for readability and compactness of code maybe you should be doing this instead:
var arrNum = [0,1,0,0];
arrNum.sort(function(a, b) {
return a == 0 ? 1 : 0;
});
It can contain any number and will keep order of others than 0
I am trying to fill an array with ranges of values in a recursive function, but i see a weird thing happening while returning the array, though the array has values, when i alert it outside the function it gives me undefined. Not Sure whether its my code issue or any kind of behavior.
Same implementation when i tired using simple for loop it worked fine.
I don't know what title to give for this problem, please suggest me a good one.
JS Fiddle
With Recursion
var arr = [];
function fillArray(n,i){
if(arr.length !== n){
if(i===0)
arr[i] = i;
else
arr[i] = arr[i-1] + 1;
fillArray(n,++i);
}
else{
console.log(arr);
return arr;
}
}
console.log(fillArray(10,0));
With For Loop
var arr = [];
function fillArray(start,end){
for(var i=start,j=0;i<=end;i++,j++){
arr[j] = i;
}
return arr;
}
alert(fillArray(1,10));
function fillArray(n, i, a) {
a.push(i);
return a.length < n ? fillArray(n, ++i, a) : a;
}
console.log(fillArray(10, 0, []));
First, this is not a good example of something that should be implemented recursively in JavaScript. It's unidiomatic.
The reason the result is undefined outside the function is that your code fails to return the result of each successive recursive call. The recursion statement should look like:
return fillArray(n,++i);
Without that, the final call that does return the array will have its return value simply ignored by the penultimate call.
Take a look on your example:
var arr = [];
function fillArray(n,i){
if(arr.length !== n){
if(i===0)
arr[i] = i;
else
arr[i] = arr[i-1] + 1;
fillArray(n,++i); // ends without returning anything
}
else{
console.log(arr);
return arr; // return array
}
}
console.log(fillArray(10,0));
First of all I wouldn't declare value outside function and I wouldn't do with recursion (as you pollute your closure. But if you do so and you keep your function as it is, don't expect value as return here (as you edit variable outside of it).
var arr = [];
function fillArray(n,i){
if(arr.length !== n){
if(i===0)
arr[i] = i;
else
arr[i] = arr[i-1] + 1;
fillArray(n,++i);
} // return not needed
}
fillArray(10,0);
console.log(arr);