简体   繁体   中英

I have implemented function without using function key but not able to print the result

I have written one function, without using function keyword. if age is above 15 take the value and push into one array. I have done that but i am not able print the result.Kindly any one help on this.

my code

<script>
var age = ["14", "20", "25"];
const response = howMany(people) {
  let oldCitizen = [];
  for (let i = 0; i < people.length; i++) {
   if (people[i] >=15) {
    oldCitizen.push(people[i]);
   }
  }
  return oldCitizen;
}
console.log(howMany(age));
</script>

Getting Error

(index):3 Uncaught SyntaxError: Unexpected token '{' (at (index):3:34)

I know below code also will work, but i need without function keyword i wants to implements one function and print the result.

<script>
var age = ["14", "20", "25"];
function howMany(people) {
  let oldCitizen = [];
  for (let i = 0; i < people.length; i++) {
   if (people[i] >=15) {
    oldCitizen.push(people[i]);
   }
  }
  return oldCitizen;
}
console.log(howMany(age));
</script>

updated code

<script>
var age = ["14", "20", "25"];
const howMany = (people) => {
let oldCitizen = [];
for (let i = 0; i < people.length; i++) {
  if (people[i] >=15) {
  oldCitizen.push(people[i]);
  }
}
return oldCitizen;
}
//console.log(howMany(age));
</script>

<?php
echo '<script type="text/javascript">howMany(["14", "20", "25"]);</script>';
?>

you can do something like this:

var age = ["14", "20", "25"];
const response = (people) => {
  let oldCitizen = [];
  for (let i = 0; i < people.length; i++) {
   if (people[i] >=15) {
    oldCitizen.push(people[i]);
   }
  }
  return oldCitizen;
}
console.log(response(age));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM