Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#1
- function printTimeout(str, n){
- setTimeout(function() {
- console.log(str);
- },n*1000);
- }
- printTimeout('Hello', 10);
- //#2
- function sumAll(n) {
- return n == 1? n : n + sumAll(n-1);
- }
- console.log(sumAll(2));
- console.log(sumAll(4));
- //#3
- function bombTimer(str, time){
- let i = setInterval (function () {
- if (time >= 1){
- console.log(time--);
- }else{
- console.log(str);
- clearInterval(i);
- }
- },1000);
- }
- bombTimer('Booom', 3);
- //#4
- function factorial(n) {
- return n == 1 ? n : n * factorial(n-1);
- }
- console.log(factorial(3));
- console.log(factorial(5));
- //#5
- function bombTimer(str, time){
- setTimeout(function lime() {
- if (time >= 1){
- console.log(time--);
- setTimeout(lime,1000);
- }else{
- console.log(str);
- }
- },0);
- }
- bombTimer('Booom', 3);
- //#6
- function filterNumbers(arr, maxNumber) {
- let filteredArr = [];
- for (let i = 0; i < arr.length; i++){
- if (arr[i] <= maxNumber){
- filteredArr.push(arr[i]);
- }
- }
- return filteredArr;
- }
- console.log(filterNumbers([1, 4, 8, 1, 20], 5));
- //#7
- function minMax(arr){
- return 'max: ' + Math.max(...arr) + ' min: ' + Math.min(...arr);
- }
- console.log(minMax([1, 4, 8, 2, 20]));
- #8
- function arrAvg(arr) {
- return arr.reduce((a,b) => a+b, 0)/arr.length;
- }
- console.log(arrAvg([1,4,2]));
- //#9
- function concatFirstNestedArrays(arr){
- let joinArr = [].concat.apply([], arr);
- return joinArr;
- }
- console.log(concatFirstNestedArrays([[0, 1], [2, 3], [4, 5]]));
- //#10
- function usersToObject(users){
- let newObject = {};
- for(i=0; i < users.length; i++){
- newObject[users[i]['id']] = users[i];
- }
- return newObject;
- }
- const users = [
- { id: 1, name: 'John', birthday: '1999-2-12'},
- { id: 2, name: 'Bill', birthday: '1999-1-19'},
- { id: 3, name: 'Carol', birthday: '1999-1-11'},
- { id: 4, name: 'Luce', birthday: '1999-2-22'}
- ]
- console.log(usersToObject(users));
- //#11
- function filterUsersByMonth(users, month){
- return users.filter(function(elem){
- if ((elem['birthday'].split('-'))[1] == month){
- return true;
- }else{
- return false;
- }
- });
- }
- const users = [
- { name: 'John', birthday: '1999-2-12' },
- { name: 'Bill', birthday: '1999-1-19' },
- { name: 'Carol', birthday: '1999-0-11' },
- { name: 'Luce', birthday: '1999-2-22' }
- ]
- console.log(filterUsersByMonth(users, 0));
- //#12
- function getAdultNames(users) {
- let adultNames = [];
- for (i = 0; i < users.length; i++){
- let age = new Date().getFullYear() - new Date(users[i].birthday).getFullYear();
- if(age>=18){
- users[i].birthday = age;
- adultNames.push(Object.values(users[i]).join(' '));
- }
- }
- return adultNames.join(', ');
- }
- const users = [
- { name: 'John', birthday: '1999-6-12' },
- { name: 'Bill', birthday: '2005-5-19' },
- { name: 'Carol', birthday: '2003-10-11' },
- { name: 'Luce', birthday: '2000-11-22' }
- ];
- console.log(getAdultNames(users));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement