Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,326 members, 7,811,963 topics. Date: Monday, 29 April 2024 at 02:19 AM

Can Someone Explain This Line Of Code To Me - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can Someone Explain This Line Of Code To Me (1460 Views)

What Is Wrong With This Line Of Code? / I Need Help With This Line Of Code (wordpress Theme Php 7 Issue) / Someone Explain This Javascript Snippet (2) (3) (4)

(1) (Reply) (Go Down)

Can Someone Explain This Line Of Code To Me by BabyApple(m): 6:08pm On Sep 10, 2018
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>The highest number is <span id="demo"></span>.</p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo"wink.innerHTML = myArrayMax(points);

function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
</script>

</body>
</html>


My question is where does (null, arr); comes from
Re: Can Someone Explain This Line Of Code To Me by codemarshal08(m): 7:03pm On Sep 10, 2018
BabyApple:
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>The highest number is <span id="demo"></span>.</p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo"wink.innerHTML = myArrayMax(points);

function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
</script>

</body>
</html>


My question is where does (null, arr); comes from

null is built in Javascript . it is used to represent "nothing"
Javascript apply function accepts two (2) parameters. The first parameter is optional . passing null to it means no value.
The arr is used as a parameter in the myArrayMax function definition. parameters are used to pass data to a function.
In your code, the arr is used to pass the values in your point arrays to the myArrayMax function
parameters are defined by programmers.

3 Likes

Re: Can Someone Explain This Line Of Code To Me by fnep2smooth(m): 12:14am On Sep 11, 2018
BabyApple:
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>The highest number is <span id="demo"></span>.</p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo"wink.innerHTML = myArrayMax(points);

function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
</script>

</body>
</html>


My question is where does (null, arr); comes from
You are simply telling the program to sort your number you listed in your array by showing the highest number.

1 Like

Re: Can Someone Explain This Line Of Code To Me by ogunjobiolaide: 3:37am On Sep 11, 2018
fnep2smooth:
You are simply telling the program to sort your number you listed in your array by showing the highest number.
You are on point

1 Like 1 Share

Re: Can Someone Explain This Line Of Code To Me by fnep2smooth(m): 6:17am On Sep 11, 2018
ogunjobiolaide:
You are on point
its our work o
Re: Can Someone Explain This Line Of Code To Me by BabyApple(m): 7:11am On Sep 11, 2018
codemarshal08:


null is built in Javascript . it is used to represent "nothing"
Javascript apply function accepts two (2) parameters. The first parameter is optional . passing null to it means no value.
The arr is used as a parameter in the myArrayMax function definition. parameters are used to pass data to a function.
In your code, the arr is used to pass the values in your point arrays to the myArrayMax function
parameters are defined by programmers.

what is the value of arr?

1 Like

Re: Can Someone Explain This Line Of Code To Me by codemarshal08(m): 8:44am On Sep 11, 2018
BabyApple:
what is the value of arr?
40, 100, 1, 5, 25, 10


View
Math.max.apply(null, arr);
as
Math.max.apply(40, 100, 1, 5, 25, 10)

1 Like

Re: Can Someone Explain This Line Of Code To Me by kaypompee: 3:21pm On Sep 11, 2018
The JS function returns the largest array value into a html span tag specified by the ID

1 Like

(1) (Reply)

I Need A Programmer To Develop This Algorithm Into App / HNG Internship Or Google Africa Developer Scholarship? / Ideas For Phone/PC Applications

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.