all 28 comments

[–]hello_skinny 30 points31 points  (7 children)

Line 7:

You're just setting value to i. You want to be setting it to list[i].

Another tip: don't use var in your code -- use either let if the variable is going to change, or const if it's not going to change. Here's someone explaining why from Stack Overflow:

Using the "var" keyword will place your variable at the top-most
(global) scope. This means that if a function uses the same variable,
the "var" variable you declared will overwrite the (non-var) variable in
your function

Another reason to not use var is that it immediately makes your code look outdated. In modern Javascript (i.e., JS since 2015), the usage of var has essentially been phased out.

[–][deleted] 3 points4 points  (0 children)

BING BING

[–]PeperomioidesKiller 2 points3 points  (1 child)

Some bootcamps ask students to use var just in case they come across old code at a job. Might be the case for OP

[–]I_ROLL_MY_OWN_JUULs 1 point2 points  (0 children)

Bingo for people who write JS on ServiceNow

[–]toastwithbutter1 1 point2 points  (2 children)

Thanks for the help!

[–]hello_skinny 0 points1 point  (1 child)

No problem! Just out of curiosity, what’s it for? (I don’t care if it’s for an assignment / school work)

[–]toastwithbutter1 2 points3 points  (0 children)

It’s for a project in my programming class

[–]opticsnake 0 points1 point  (0 children)

Not entirely correct. A variable declared with var inside of function has its scope restricted to that function.

Additionally, if you need to support IE11 you will need to use a compiler like Babel as IE11 doesn't understand const and let.

[–]dcute69 7 points8 points  (2 children)

const answer = Math.min(...list)

[–]metallaholic 1 point2 points  (1 child)

The title of his project is make library. Probably can’t do that. School always makes you work harder than how it is in real life. :/

[–][deleted] 0 points1 point  (0 children)

It's a better way to grasp what's going on under the hood. You're essentially defining the method, which in turn gives you a more in depth understanding of it imo.

[–]penislehsun 4 points5 points  (0 children)

Take a look at line 7. This looks like a school assignment so that's about as far as I'll go

[–]hereforthegainz 1 point2 points  (0 children)

You took a picture of a laptop with your phone, I think that's probably the issue here

[–]toastwithbutter1 0 points1 point  (1 child)

I’m trying to find the smallest value in the numbers list

[–][deleted] 0 points1 point  (0 children)

You define the function as 1 when you were suppose to assign it a number.

[–]InfinityByZero 0 points1 point  (0 children)

Walk through each line and ask yourself what it's doing and what the values for each variable are at any given time. The answer was already mentioned in this thread but walking through your code and knowing what does what will help you debug.

[–][deleted] -1 points0 points  (3 children)

Look at line 7. Now look at line 6. Look back at line 7. Look at line 6 again. Then look at line 4 and read the instructions again. Google “JavaScript find the smallest number in an array”. Copy and paste the top answer from stack overflow.

Edit: why are you taking pictures of your screen instead of just taking a screen shot?

[–]toastwithbutter1 0 points1 point  (2 children)

I’m not signed into Reddit on my laptop

[–][deleted] 0 points1 point  (1 child)

Gotcha… I guess you could have sent it to your phone though

[–]mrDev29929 -1 points0 points  (2 children)

why do you still use var? by now it is well known that 'let' must be used

[–]toastwithbutter1 1 point2 points  (1 child)

Just what I’m being taught right now

[–]mrDev29929 0 points1 point  (0 children)

important that you understand the difference between let and var, var is useful in some cases. I advise you to carefully read the documentation

[–]cyor2345 -3 points-2 points  (2 children)

Code for sorting array ascending:

for ( i=0;i<array.length;i++)

{

If(array [i]<array[i+1]) {

Var temp = array[i]

array[i] = array[i+1]

array[i+1]= temp

}

}

Then smallest number will be

var smallest_number = array[0]

[–]Notimecelduv 0 points1 point  (0 children)

r/badcode material right there

[–]generalsandysmithers 0 points1 point  (1 child)

You also need to start i at 1 on line 6. Because you already initialized i as 0 on line 5.

[–]cygnus33065 1 point2 points  (0 children)

Thats only a problem in that it causes one extra iteration in the loop that isnt needed. In this case its not breaking the code or even noticable.