0

Let's say you have an array like this:

//Input [1,2,3,4,5,6,7]

How to write the function which will get us the output of

//Output

 Array1 = [1]
 Array2 = [1,2]
 Array3 = [1,2,3]
 Array4 = [1,2,3,4]
 Array5 = [1,2,3,4,5]

And

//Output 1. [1,1,2,1,2,3,1,2,3,4...]

// tried this

for (i = 0; i < arr.length; i++) { 
    arr = new Array(arr[i]);
}
3
  • Can you think of how you’d print a triangle? * ** *** ****? Commented Dec 9, 2017 at 6:09
  • 2
    Could you please show what you've tried? What exactly are you stuck on? This isn't a code-writing service. We would much prefer to act as your mentors rather than your employees. Commented Dec 9, 2017 at 6:10
  • StackOverflow isn't a free code-writing service, and expects you to try to solve your own problem first. Please update your question to show what you have already tried, showcasing a specific problem you are facing in a minimal, complete, and verifiable example. For further information, please see how to ask good questions, and take the tour of the site. – Commented Dec 9, 2017 at 6:16

4 Answers 4

2

You can use functions like Array#map and Array#slice to easily create the first function, and use Array#concat with spread syntax to flatten a 2D array.

function prefixes(array) {
  return array.map((_, index) => array.slice(0, index + 1));
}

function flatten(array) {
  return [].concat(...array);
}

const output = prefixes([1,2,3,4,5,6,7]);
console.log(output);
console.log(flatten(output));

Sign up to request clarification or add additional context in comments.

Comments

1

You could reduce the array by slicing the wanted part with spread syntax ....

var array = [1, 2, 3, 4, 5, 6, 7],
    result = array.reduce((r, _, i, a) => [...r, ...a.slice(0, i + 1)], []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Use forEach and for loop

var arr = [1, 2, 3, 4, 5, 6, 7];
var arrys = [];
//lopping over the array, and creating the new array and putting values
// equal to number of index
arr.forEach(function(item, index) {
  var tempArray = [];
  for (var m = 0; m <= index; m++) {
    tempArray.push(arr[m])
  }
  arrys.push(tempArray)
})
console.log(arrys)
// flat the previously created array
var flatten = [];
arrys.forEach(function(item) {
  item.forEach(function(item2) {
    flatten.push(item2)
  })
})
console.log(flatten)

Comments

0

You can do this simply by using two nested for loops like this,

let arr = [1, 2, 3, 4, 5, 6, 7];
function foo(arr) {
    let rarr = [];//result array
    for (let i = 0; i < arr.length; i++) {
        let a = [];//array[i]
        for (let j = 0; j <= i; j++) {
            a.push(arr[j]);
        }
        rarr.push(a);
    }
    return rarr;
}
console.log(foo(arr));

Comments

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.