aveeva Posted July 9, 2019 Share Posted July 9, 2019 String: NodeJs is one of the best technology Quote NodeJs is one of the best technology 6 2 3 2 3 4 10 From above, how to print technology which largest even number. How to print the largest even number? Quote Link to comment Share on other sites More sharing options...
benanamen Posted July 9, 2019 Share Posted July 9, 2019 What have you tried besides posting this on every coding forum? 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 9, 2019 Share Posted July 9, 2019 (edited) WHAT is this? You have a tendency to NOT be very specific in your posts. You don't explain things, you don't ask questions. You simply post something and then hope forum users can figure out what you are trying to say. Please say what you want! For instance - whatever does 'NodeJS' have to do with this list of numbers? And what are the words there for? None of this is obvious to us. Edited July 9, 2019 by ginerjm Quote Link to comment Share on other sites More sharing options...
gw1500se Posted July 9, 2019 Share Posted July 9, 2019 First create an array of even numbers. You can find which are even by using the remainder operator dividing by 2. A zero remainder means it is even. Then use the max function to find the largest. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted July 9, 2019 Share Posted July 9, 2019 $numbers = array(1,3,7,8,10,12); foreach($numbers as $number) { if($number%2==0) { if($number > $old_number) { $max_number = $number; } $old_number = $number; } } echo $max_number; Not sure what nodejs has to do with anything. This is a php forum. Something like this would do it in php. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted July 9, 2019 Share Posted July 9, 2019 I didn't even think about using the max function. $numbers = array(1,3,7,8,10,12); foreach($numbers as $number) { if($number%2==0) { $evens[]=$number; } } $max_number = max($evens); echo $max_number; Quote Link to comment Share on other sites More sharing options...
Barand Posted July 10, 2019 Share Posted July 10, 2019 $numbers = array(1,3,7,8,10,13); $max = max(array_filter($numbers, function($v) { return $v%2==0; })) ; 2 Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 10, 2019 Share Posted July 10, 2019 2 hours ago, Barand said: $numbers = array(1,3,7,8,10,13); $max = max(array_filter($numbers, function($v) { return $v%2==0; })) ; This is the modern/functional programming way of handling a problem like this. I'm not a huge javascript fan, but having to practice it on occasion certainly opened my eyes to the use of filter/map/reduce and other mainstays of functional programming. I've also found this guy's youtube channel to be both educational and inspirational. You do have to do a bit of research for the php functions that are similar, but in the case of arrays there are ones like array_filter that I find are great as glue for so many smallish tasks as demonstrated by Barand's code. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 10, 2019 Share Posted July 10, 2019 array_reduce is a great function for this. Quite rare to see it in the real world, which means bonus points for actually using it. $numbers = array(1, 3, 7, 8, 10, 13); // assuming there is at least one even number >= 0 $max = array_reduce($numbers, function($max, $n) { return ($n % 2) == 0 && $n > $max ? $n : $max; }, 0); Or with PHP 7.4's arrow functions, $max = array_reduce($numbers, fn($max, $n) => ($n % 2) == 0 && $n > $max ? $n : $max, 0); 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted July 11, 2019 Share Posted July 11, 2019 7 hours ago, requinix said: Or with PHP 7.4's arrow functions, $max = array_reduce($numbers, fn($max, $n) => ($n % 2) == 0 && $n > $max ? $n : $max, 0); There's a part of me that thinks they're developing future PHP specifically to make the kids think they're programming in JavaScript... I mean, I actually really like fat arrow functions in JS, but every time I see an example of it in PHP I have to pause a second and readjust my brain. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 11, 2019 Share Posted July 11, 2019 10 minutes ago, maxxd said: There's a part of me that thinks they're developing future PHP specifically to make the kids think they're programming in JavaScript... I mean, I actually really like fat arrow functions in JS, but every time I see an example of it in PHP I have to pause a second and readjust my brain. Emphasis on "think". The nicest thing arrow functions have to offer is implicit variable binding. $a = 1; $add = fn($b) => $a + $b; // function($b) use ($a) { return $a + $b; } echo $add(2); // 3 The bad thing is that the binding is strictly by-value. As in a regular use() and by a by-ref use(&). Which means it's very much not like how Javascript works (which goes by scope, not bindings). $a = 2; echo $add(2); // 3 because $add still thinks $a=1 var a = 1; var add = (b) => a + b; console.log(add(2)); // 3 a = 2; console.log(add(2)); // 4 Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 11, 2019 Share Posted July 11, 2019 maxxd, From what I've seen, it's more a matter of the php team wanting to give developers the same tools and capabilities that exist in other languages. Adding syntax to easily use anonymous functions is yet another step in furthering that longtime goal. With that said, the associative array assignment syntax does make this a change that will cause some head scratching for long time PHP developers. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 11, 2019 Share Posted July 11, 2019 2 hours ago, gizmola said: With that said, the associative array assignment syntax does make this a change that will cause some head scratching for long time PHP developers. As I recall slash can find in my email archive (~summer 2017 was when the discussion first started), ==> was the preferred syntax for a while because Hack uses it, however PHP couldn't implement closures in the same way Hack does (IIRC) so this would break compatibility with HHVM. Plus there would be difficulties in coercing the parser into reading it. And there was a bit of "let's please not establish a precedence of copying Hack". So it couldn't work. => was proposed early but was initially rejected because it would conflict with arrays. Similarly -> would conflict with objects. Another option was ~> but too many people thought it looked weird, and apparently it too had some parser problems. Ruby's |x| form didn't last long in the conversation either. \()=> came up for a while but enough people objected to Yet Another Backslash. And there were other assorted arrangements of symbols - braces, brackets, pipes, you name it. But => did win out once people realized that the ambiguity problem could be resolved by using it as keyword()=>. So then the question was what keyword. "function" already exists but was too long for something that's supposed to be short and easy to type. "lambda" was another but was maybe sounded too technical. "fn" finally won as being short and concise. Whether fn()==> could also have worked, I don't know... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.