Methods Basics Flashcards

(7 cards)

1
Q

greet with first and last name
based on gender

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A program that counts from 0 - 1000 and prints
also print each num

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

write a function findlargestelement that takes an array of numbers and returns the largest element

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

programs prints all even num in an array

A

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])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

print first names in a complex object

A

//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)
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

reverse all elements of an array

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

callback

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly