-2

I'm new to coding so apologies (and please correct me) if I use wrong terminology. I was doing the following coding challenge: Given an array with multiple values, write a function that returns a new array that only contains the maximum, minimum, and average values of the original array.

This is the code I came up with during my first attempt: First attempt

Eventually I figured out through trial and error that the problem was the location of my arrnew variable. I also fixed the average Expression and the correct algorithm is: Correct algorithm

I assumed that since the max, min, and avg were changing (is there a better term for this?), the var arrnew would pick up these new values but obviously that wasn't the case. I guess my question is when does line order matter? Or probably more specifically, are there any simple rules or principles I should be aware of to help me better understand command line coding (I dont know if this is called command line coding).

Thanks

Dinamo788
  • 11
  • 1
  • 5
  • Your solution is good, not sure what you've asked about though. – zerkms Jul 23 '16 at 05:00
  • How does command line have anything to do with *JavaScript*? JavaScript is in the browser, command line is on the machine. The array is created with those initial values and *does not* change as the values inside the array change. – Andrew Li Jul 23 '16 at 05:00
  • @AndrewL. they don't know the proper terminology, so just try to guess. (see the very last statement in the question) – zerkms Jul 23 '16 at 05:01
  • @zerkms Pointing out for the OP, so that he/she may edit appropriately – Andrew Li Jul 23 '16 at 05:02
  • Here on stack overflow, code relevant to the question MUST be copied into the question itself, not only available via an external link. This is because external links have a tendency to get changed or stop working rendering the question useless as a reference for future readers. – jfriend00 Jul 23 '16 at 05:06
  • You are confusing people by referring to "command line" which most people think means something you type into a console to execute system commands. These are simply "lines of code". – jfriend00 Jul 23 '16 at 05:18
  • @jfriend00 Thanks for the clarification. I thought I must be using the wrong term; lines of code is what I meant. And now I know js is strictly for web dev and is only in the browser. I thought using Gist makes the post cleaner, but if it's preferable to just include the code into the body of the question then i'll do that from now on, thanks. – Dinamo788 Jul 23 '16 at 18:55
  • @Dinamo788 - FYI, Javascript is not only in the browser. It is being used a lot in servers now with the node.js environment that runs Javascript. – jfriend00 Jul 23 '16 at 20:05

2 Answers2

1

When you do this:

var arrnew = [max, min, avg];

This places a copy of the contents of max, min, avg into the array. Future changes to what max, min and avg contain will not affect the array in any way.

So, if you do;

var max = 3, min = 1, avg = 2;
var arrnew = [max, min, avg];    // 3, 1, 2
max = 5;
console.log(arrnew);             // 3, 1, 2

Note, the contents of arrnew are not affected at all by subsequent changes to what max, min or avg contain. This is just how Javascript (and most languages work).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Ok this is what I was looking for. So most languages don't recompile (is that the right word?) changes to variables? As in, once values are recorded (e.g. arrnew) any subsequent changes to max,min,avg won't overwrite the original since it's already "locked in"? – Dinamo788 Jul 23 '16 at 19:03
  • @Dinamo788 - Languages, in general, have assignment and reference/pointer. An assignment copies a value from one place to another and once it's been copied, it is its own copy of the data - completely independent of the original. That's what you were doing here. – jfriend00 Jul 23 '16 at 20:03
  • @Dinamo788 - If you want to learn more about this concept in general, you can read this [Do objects pushed into an array in javascript deep or shallow copy?](http://stackoverflow.com/questions/8660901/do-objects-pushed-into-an-array-in-javascript-deep-or-shallow-copy/8661088#8661088) and [Javascript by reference vs. by value](http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) – jfriend00 Jul 23 '16 at 20:03
  • I've been looking at deep copy vs shallow copy and while it's still kind of confusing, but is my example one of deep copy? Because the max/min/avg values are copied to a new destination for arrnew and therefore do not change when the original values are changed? If this operated as a shallow copy, would the line order not matter since arrnew would reference the original destination of max/min/avg? Does all js operate on the basis of deep copy or how would I know? – Dinamo788 Jul 25 '16 at 05:43
  • @Dinamo788 - Your example is just a simple variable assignment. I wouldn't even call it shallow copy. Deep and shallow copy refers to make a copy of an array or object in Javascript. A deep copy recursively copies even embedded objects whereas a shallow copy just assigns (not copies) the embedded objects. Neither a deep or shallow copy will help with this type of problem. – jfriend00 Jul 25 '16 at 05:44
0

It is because of the compiler, the compiler will always go from top to bottom, so always try to structure your code in an ordered way.

Pablo Ivan
  • 280
  • 2
  • 15
  • Ok so the compiler will not recompile anything unless there is an operation to change a variable value. Thanks – Dinamo788 Jul 23 '16 at 19:05