Some useful Js Examples:
If we have an array like
Eg:
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
And we want to print only the first numbers in the array follow the code below,
const secretMessage = animals.map(animal => animal[0]);
console.log(secretMessage.join(' '));
OUTPUT:
HELLOWORLD
If we want to print only first letters of a number Follow the code below
EG:
const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(num => num/100);
console.log(smallNumbers);
OUTPUT:
[1,2,3,4,5]
Take a look at the example array below
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
In this array we want to print the index of the strings which starts with 's'
const startsWithS = animals.findIndex(animal => {
return animal[0] === 's' ? true : false;
});
If we have an array like
Eg:
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
And we want to print only the first numbers in the array follow the code below,
const secretMessage = animals.map(animal => animal[0]);
console.log(secretMessage.join(' '));
OUTPUT:
HELLOWORLD
If we want to print only first letters of a number Follow the code below
EG:
const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(num => num/100);
console.log(smallNumbers);
OUTPUT:
[1,2,3,4,5]
Take a look at the example array below
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
In this array we want to print the index of the strings which starts with 's'
const startsWithS = animals.findIndex(animal => {
return animal[0] === 's' ? true : false;
});
Comments
Post a Comment