greet with first and last name
based on gender
A program that counts from 0 - 1000 and prints
also print each num
write a function findlargestelement that takes an array of numbers and returns the largest element
let arr = [10,20,30,40,50,60,70,80,90,100];
let num = 0;
function FindLargestArray (arr){
for (let i=0; i<arr.length; i++) {
if (num < arr[i]) {
num = arr[i];
}
}
console.log(num)
}
FindLargestArray(arr);
can use return as well
programs prints all even num in an array
let arr = [11,20,33,45,57,69,71,83,90,100];
let num
function FindEven (arr){
for (let i=0; i<arr.length; i++) {
if (arr[i] % 2 == 0) {
num = arr[i]
console.log(num)
}
}
}
FindEven(arr);
or
for (let i=0; i<arr.length; i++) {
if (arr[i] % 2 == 0) {
console.log(arr[i])
print first names in a complex object
//Complex object: an array of people with details
const people = [
{ firstName: “John”, lastName: “Doe”, gender: “male”, age: 25 },
{ firstName: “Sarah”, lastName: “Smith”, gender: “female”, age: 30 }
];
for (let i=0; i<people.length; i++) {
if (people[i].gender == “male”) {
console.log (people[i].firstName)
}
}
reverse all elements of an array
function reverseArray(arr) {
let reversed = [];
for (let i= arr.length - 1; i >= 0; i–) {
reversed.push(arr[i]);
}
return reversed;
}
let numbers = [10,20,30,40,50];
console.log(reverseArray(numbers));
there is a reverse builtin function
callback
function sum(num1, num2, fnToCall) {
let result = num1 + num2;
fnToCall(result);
}
function displayResult(data) {
console.log(“Result of the sum is : “ + data);
}
sum(1, 2, displayResult);