2

I am writing a function that will return the first n elements of an array and if n is undefined then return the first element.

*Edit: I solved the problem.

_.first = function(array, n) {
var result = [];
if (n == undefined) {
  return array[0];
}
var m;
if (array.length < n) {
  m = array.length;
} else {
  m = n;
}
  for (var i = 0; i < m; i++) {
  result.push(array[i]);
} return result;
};
3
  • 1
    First of all: What are the values of the parameters passed to the function? Secondly: You are returning from the function after the first iteration. That means the result array will always only contain a single element. Commented Sep 7, 2017 at 23:46
  • Why don't you just use array.slice(0, n)? Commented Sep 7, 2017 at 23:58
  • Felix Kling - The parameters are automatically generated when I test the function using an html file called SpecRunner. Thanks for clarifying that it is indeed Line 5 that is throwing me off.. I'll also give .slice a shot. That actually looks simpler. Commented Sep 8, 2017 at 2:27

3 Answers 3

1

This program is basically checking if the n value is bigger than the array's length, if it is, then it exits.

If n is not a number it exits. If it is it executes the program and logs the values of indexes with for loop until i reaches the n value. Also, it pushes the values to the empty array, so you can get the values from the array for later use.

var arr1 = [2, 3, 4, 5, 6, 7, 8];
var arr2 = []; //empty array    

function arrNreturn(arr, n){
  if(typeof n != 'number'){
    console.log('n is not a number');
    return false; //exit the program
  }
  if(n > arr.length){
    console.log("the n value is bigger than the length");
  }else{
    for(var i = 0; i < n; i++){
      console.log(arr[n]);
      arr2.push(arr[n]);
    }
  }
}

arrNreturn(arr1, 10);
Sign up to request clarification or add additional context in comments.

Comments

0
1_.first = function(array, n) {
2 var result = [];
3 for (var i = 0; i < n; i++) {
4   result.push(array[i]);
5   return result;
6 } return array[0];
7};

the problem is on the 5th line, you seem to be returning the first and not taking into account the n parts

a solution might be

_first = function(array, n){
 var result = [];
 if(n === undefined) return array[0];
 for (var i = 0; i < n; i++) {
   result.push(array[i]);
 } 
 return result;
}

1 Comment

I gave your code a shot, but it's giving this error now. "Error: expected undefined to sort of equal []"
0

Basic is

const newArr = function(arr) {return n }

Shorthand

const newArr = arr => { return n}

1 Comment

This doesn't look like it would work. Your function would just be returning n, which is undefined at least within this scope. The OP is trying to return the first n elements, not just return the value of n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.