Access Array Data with Indexes - help!

My first doubts is: Am I collecting the numbers from the array correctly?
Some aid would be greatly appreciated.

// function sumAll(arr) {
  let firstN = arr[0];
  let secondN = arr[1];

  let a = 0;
  if(firstN <= secondN) {
    for(firstN; firstN <= secondN; firstN++) {
      return a += firstN
  } else if (firstN >= secondN) {
      for(firstN; firstN >= secondN; firstN--) {
      return a += firstN

sumAll([1, 4]);

I’d recommend looking into Math.min() and Math.max(). That will help get you started. You’re trying to get the highest number and the lowest number in the array.

why you have commented out your function and second where is your array

I need to take a breather.

You are getting the numbers from the array correctly, use console.log() to double check yourself. You also have a return in your for loops, which will just return a += firstN in the first loop and stop the function right there. If you remove those you are getting the correct number in your function, but you will still need to return it someplace else.

I’ve been working on this for some time. I feel a bit stuck.

/// function sumAll(arr) {
   let firstNL =  arr[0]; //1 5 
    let firstNH = arr[1]; //4 10
    let firstNH2 = Math.max(arr[0], arr[1]); // 4 10
    let firstNL2 =  Math.min(arr[0], arr[1]); // 5 1
  //let secondN = arr[1];
  let a = 0;
  if(firstNL <= firstNH) {
    for(firstNL; firstNL <= firstNH; firstNL++) {
      //a += firstNL
  if(firstNH2 >= firstNL2) {
    for(firstNH2; firstNH2 >= firstNL2; firstNH2--) {
      a += firstNH2 
  return a;

sumAll([1, 4]);

You where very close with your original code. Forget the Math.min and Math.max for now, we can use that later to clean up your original code a little after you get this working if you want.

Like i said your original code was very close. Your if statement is correct in choosing the highest and lowest numbers, but get those returns out of your for loops. And it looks like you are using a to collect your answer, just return that someplace else and it should work.

copy and past your code here to watch it work in order and follow along.

Yep, I was on the cusp there. I’ve noticed that I have the propensity to veer off into the let’s complicate everything land. It’s quite uncanny.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.