Chapter 4 - Exercises

Your advice helped me. Thank you!

1 Like

No problem! I’m glad it helped :smiley:

The sum of a range

function range(a,b,c){
  if(c == null) c = 1;
  var array = [];
  
  if (c > 0){
    for( var i=a; i<=b; i+=c)
      array.push(i);      
  }else{
    for( var i=a; i>=b; i+=c)
      array.push(i);
  }
  return array;    
};  

function sum(arrayInput){
  var total = 0;
  for( var x=0; x < arrayInput.length; i++)
    total += array[x];
  return total;
};  

Reversing an Array

function reverseArray(arrayInput){
  arrayInput.reverse();
  return arrayInput; 
};  

function reverseArrayInPlace(arrayInput){
  return reverseArray(arrayInput);
};  
1 Like

Hi!

Here is my final answer to Exercise #1, the sum of a range. Another student assisted me with this one.

function range(start, end, step = start < end ? 1 : -1){
  let arr = [];
  if(step > 0) {
    for(i = start; i <= end; i += step) arr.push(i);
  }
  else {
    for(i = start; i >= end; i += step) arr.push(i);
}
return arr;
}

function sum(arr){
  let total = 0;
  for (let value of arr){
    total += value;
  }
  return total;
}
console.log(sum(range(1,10)));
console.log(range(1, 10, 2));
console.log(range(5, 2, -1));

I will not get help with Exercise #2 until I have tried it so much it is giving me a headache.

Thank you!

1 Like

The second part to Exercise #2, which involves reversing an array in place, is very hard for me to understand. The instructions sound way too similar to those of the first part.

After referring to other posts, I finally figured out the first part to Exercise #2. I will not post it in the forum until I complete the second part.

Could someone please share with me how to better understand the second part to Exercise #2?

Thank you!

Hi all,

The Sum of a Range
I’m struggling with this. I’ve spent hours trying to work it out for myself, I’ve looked at the answer and other student’s answers and tried to work backwards and I’ve tried searching the internet for other sources to explain it differently. I still look at the answer and don’t really understand it. I can see the answer seems to work only in an ascending order, and that the next part is reversing an array, but surely for it to work comprehensively it would need to work both ways; if the value of start was higher than end? I feel my Javascript is starting to suffer in other areas if I spend much more time on this. In my head the answer should look something like this, if you can see what I’m trying to express; :joy:

let array1 = [1,2,3,4,5,6,7,8,9,10];

function range (start, end) {

  let (start = "");
  let (end = ""); {
    return ({[start], ...[end]});
  }

}

I think the problem here is that you are not including all of the information needed to solve the exercise. Yes, you need to include start and end, but you also need to include step = start < end ? 1 : -1. Let array1 equal an empty array if step > 0. You will need to include a for loop and an else loop as well. Make sure the function returns the array. Refer back to the instructions in the book if you need to.

You are not alone in figuring out this exercise. I had to get help with this one, too.

I hope my advice helps you out. But if it doesn’t, don’t hesitate to get assistance from someone else. Javascript is indeed very hard to wrap your brain around. Believe me, I’ve been there.

1 Like

Hi!

If anyone in this forum is having difficulty with reversing an array in place, like I am, here is an article to check out. It features the exact same exercise that we are working on.

Thank you!

5 Likes

Hi!

Here is my current answer to Exercise #2. After spending some time trying to get help, I finally found out how to solve it. I found a very useful article that I used especially for the second half of the exercise: https://medium.com/@vontmer/how-to-reverse-arrays-in-javascript-without-using-reverse-ae995904efbe

//Exercise #2, Pt. 1: Reversing an array
function reverseArray(firstArray){
  var output = [];
  for (i = 0; i < firstArray.length; i++){
    var entry = firstArray[i];
    output.unshift(entry);
  }
  return output;
}
console.log(reverseArray([1, 2, 3, 4, 5]));

//Exercise #2, Pt. 2: Reversing an array in place
function reverseArrayInPlace(secondArray){
    for (var i = 0; i <= (secondArray.length / 2); i++){
      let output = secondArray[i];
      secondArray[i] = secondArray[secondArray.length - 1 - i];
      secondArray[secondArray.length - 1 - i] = output
    }
return secondArray;
}
console.log(reverseArrayInPlace([1, 2, 3, 4, 5]));

But I ask that you carefully proofread my answer and reply to me any corrections that need to be made. I am very doubtful that the second half of the problem is correct, because I got the same result as the first part.

Thank you!

Thank you for that info! I’m reading it now, it looks really good! :slight_smile: :+1:

Hello,
I made the changes you suggested me.

I see tons of answers of this problem on the Internet, but I don’t want to just copy and paste, I want to understand what I’m really doing.

This is so far my code, and what I understand but when the problems come, I don’t see where my errors are on the full code. It is a bit complicate for me to dedicate full time on this due to I have a family and others jobs to do. When I answer late is just because of my limited time the sometimes makes me feel like I do one step forward and to two back.

Thanks for the help

<!DOCTYPE html>
<html lang="en-US">

  <head>
        <title>Range Myown</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<script>

      function range(start, end){
        var arr = [];
        for (let i = start; i <= end; i++){
        arr.push(i);
        console.log(i);
        }
        return arr;
       }

</script>
</body>
</html>
2 Likes

//Sum of a Range

function range(start, end, step = start < end ? 1 : -1) {
  let array = [];

  if (step > 0) {
    for (let i = start; i <= end; i += step) array.push(i);
  } else {
    for (let i = start; i >= end; i += step) array.push(i);
  }
  return array;
}

function sum(array) {
  let total = 0;
  for (let value of array) {
    total += value;
  }
  return total;
}

// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55

1 Like

//Reversing an Array…

function reverseArray(array) {
let output = [];
for (let i = array.length - 1; i >= 0; i–) {
output.push(array[i]);
}
return output;
}

function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}

// → [“C”, “B”, “A”];
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]Preformatted text

1 Like

I think this is the first part? I’m going to have to practice writing this code to help me understand it better.

function range (start, end, step = start < end ? 1 : -1) {
  let array = [];

  if (step > 0) {
    for (let i = start; i <= end; i += step) array.push (i);
  } else {
    for (let i = start; i >= end; i += step) array.push (i);
  }
  return array;
}
2 Likes
function range (start, end, step = start < end ? 1 : -1) {
  let rangeArray = [];
  if (step > 0) {
    for (counter = start; counter <= end; counter += step) {
      rangeArray.push(counter);
    };
  } else {
    for (counter = start; counter >= end; counter += step) {
      rangeArray.push(counter);
    }
  };
return rangeArray;
};

function sum(rangeArray) {
  let total = 0;
  for (let value of rangeArray) {
    total += value;
  };
  return total;
};

function reverseArray (toReverseArray) {
  var reversedArray = [];
  for(let i = toReverseArray.length -1; i >= 0; i--) {
    reversedArray.push(toReverseArray[i]);
  }
  return reversedArray;
};

function reverseArrayInPlace (toReverseArrayInPlace) {
  let left = 0;
  let right = toReverseArrayInPlace.length -1;
  while (left < right) {
    let temp = toReverseArrayInPlace[left];
    toReverseArrayInPlace[left] = toReverseArrayInPlace[right];
    toReverseArrayInPlace[right] = temp;
    left++;
    right--;
  }
  return toReverseArrayInPlace;
};

console.log(reverseArray([1,2,3,"test"]));
console.log(reverseArrayInPlace([1,2,3,"test"]));
2 Likes

The sum of a range

function range(start, end, step = start < end ? 1 : -1) {
  let outputArray = [];

  if (step > 0) {
    for (let i = start; i <= end; i += step) outputArray.push(i);
  } else {
    for (let i = start; i >= end; i += step) outputArray.push(i);
  }
  return outputArray;
}

function sum(outputArray) {
  let sum = 0;
  for (let value of outputArray) {
    sum += value;
  }
  return sum;
}

console.log(range(1, 10))
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55

Reversing an array

function reverseArray(array) {
  let outputArray = [];
  for (let i = array.length - 1; i >= 0; i--) {
    outputArray.push(array[i]);
  }
  return outputArray;
}

function reverseArrayInPlace(output) {
  for (let i = 0; i < Math.floor(output.length / 2); i++) {
    let before = output[i];
    output[i] = output[output.length - 1 - i];
    output[output.length - 1 - i] = before;
  }
  return output;
}

let arrayRange = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayRange);

console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];

console.log(arrayRange);
// → [5, 4, 3, 2, 1]
1 Like

Reverse array without reverse method

var array=[1,2,3,4];
function reverseArray(array){
  var newArray=[];
  for( let i=array.length-1;i>=0;i--){
    newArray.push(array[i]);
  }
  return newArray;
}
console.log(reverseArray(array));

Reverse array in place

const reverseArrayInPlace=(arr)=>{
   let temp;
   for(let i=0;i<arr.length/2;i++){
temp=arr[i];
arr[i]=arr[arr.length-1-i];
arr[arr.length-1-i]=temp;
   }
   return arr
 }
 const arr=[1,2,3,4,5]
 console.log(reverseArrayInPlace(arr));

The Sum of Range

var range=function(start,end){
  var arr=[];
  cnt=start;
  while(cnt<=end){
    arr.push(cnt);
    cnt++;
  }
  return arr;
};
var sum=function(arr){
  var total=0;
  while(arr.length>0){
    total=total+arr.pop();
  }
   return total;
}

var sum2=function(arr){
  var total=0;
  for(let i=0;i<arr.length;i++){
    total=total +arr[i];
  }
  return total;
}
console.log(sum(range(1,10)));
console.log(sum(range(1,10)));
1 Like

The sum of the range

function range(start, end, step = start < end ? 1 : -1) {
  let array = [];

  if (step > 0) {
    for (let i = start; i <= end; i += step) array.push(i);
  } else {
    for (let i = start; i >= end; i += step) array.push(i);
  }
  return array;
}

function sum(array) {
  let total = 0;
  for (let value of array) {
    total += value;
  }
  return total;
}

Reversing an array

  let output = [];
  for (let i = array.length - 1; i >= 0; i--) {
    output.push(array[i]);
  }
  return output;
}

function reverseArrayInPlace(array) {
  for (let i = 0; i < Math.floor(array.length / 2); i++) {
    let old = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;
  }
  return array;
}
type or paste code here
1 Like

Sum of A Range Solution:

< script >

  function range(start, end, step = start < end ? 1 : -1) {
    let array = [];

    if (step > 0) {
      for (let i = start; i <= end; i += step) array.push(i);
    } else {
      for (let i = start; i >= end; i += step) array.push(i);
    }
    return array;
  }

  function sum(array) {
    let total = 0;
    for (let value of array) {
      total += value;
    }
    return total;
  }

  console.log(range(11, 21))
  // → Array(11)

  console.log(sum(range(11, 21)));
  // → 176
//</ script >
1 Like

Reversing An Array Solution:

< script >
function reverseArray(array) {
let output = [];
for (let i = array.length - 1; i >= 0; i–) {
output.push(array[i]);
}
return output;
}

function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}

console.log(reverseArray([“BTC”, “ETH”, “LINK”]));
// → [“LINK”, “ETH”, “BTC”];
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
</ script >

1 Like