Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Last active June 25, 2023 13:32
Show Gist options
  • Save QuocCao-dev/a7d0d3dae589e6c13fddd4d6273b1a02 to your computer and use it in GitHub Desktop.
Save QuocCao-dev/a7d0d3dae589e6c13fddd4d6273b1a02 to your computer and use it in GitHub Desktop.
Array_Object_Loops

1

We have a user profile with a name, lastname, age, profession and salary. And we want to console log everything just like:

"My name is xxx xxx, i'm xxx years old. I work as xxx and make $xxx."

2

An object with of NBA players.

const players = [
   {jersey:56,name:"Djounte Murray",position:"Point guard",PPG:2.6},
   {jersey:98,name:"Dennis Rodman",position:"Small Forward",PPG:10.8},
   {jersey:1,name:"Michael Jordan",position:"Small Forward",PPG:345.6},
   {jersey:2,name:"Lebron James",position:"Shooting Guard",PPG:26.7},
   {jersey:33,name:"Patrick Ewing",position:"Center",PPG:20.2}
]

Now we need to loop the list , and generate new array for each and in addition we must round the RPG down. For example:

“56 - Djounte Murray -- Position: Point guard -- RPP: 2”

3

const products = [
   {name:'Iphone',price:200},
   {name:'Motorola',price:70},
   {name:'Samsung',price:150},
   {name:'Sony',price:98},
   {name:'Windows pone',price:10}
];

print following:

Name: Iphone Price. - Price: 200$
Name: Motorola Price. - Price: 70$
Name: Samsung Price. - Price: 150$
Name: Sony Price. - Price: 98$
Name: Windows pone Price. - Price: 10$

4

We have an array of paintings, we need to loop the array and create a new array with messages like:

“The Mona lisa is 200 x 200”
const paintings = [
   {name:'Mona lisa',width:200,height:200},
   {name:'The scream',width:400,height:600},
   {name:'The last supper',width:600,height:700}
]

5

We have a list of cars with a brand and a price, and need to create a new array, convert the price to a different currency, and return a string like

“Ford is 40000 rupies”.
const cars = [
   {name:'Ford',price:200},
   {name:'Nissan',price:400},
   {name:'Nissan',price:600}
]

6

We have a list of channels and we want to create a new array only with the premium channels.

const channels = [
   {name:'HBO',premium:true},
   {name:'LIFE',premium:false},
   {name:'Max',premium:true},
   {name:'Cooking channel',premium:false},
   {name:'WOBI',premium:false}
];

7

We need to calculate how many Km or Miles the user traveled.

const trips = [
   {to:'Brazil',distance:1000},
   {to:'Chine',distance: 2500},
   {to:'India',distance: 3000}
]

8

We have an object with computers. And what we want to know, HOW many computer we have with MAC and how many with WINDOWS.

const computers = [
   {type:'Laptop',price:124, os:'Windows'},
   {type:'Desk',price:124, os:'Mac'},
   {type:'Desk',price:124, os:'Windows'},
   {type:'Laptop',price:124, os:'Mac'},
   {type:'Laptop',price:124, os:'Windows'},
];

Challenge 9: Remove Even Integers from an array

Implement a function removeEven(arr), which takes an array arr in its input and removes all the even elements from a given array. Input: An array with only odd integers Output: An array with only odd integers

Example:

Input: [1,2,4,5,10,6,3]
Output: [1,5,3]

Challenge 10: Uppercase

Write a function capitalize(words) that will take an array of words and return an array of those same words with the first letter of each word capitalized. Example:

['Hello', 'there!', 'How', 'are', 'you', 'doing?'] 
=> 
['Hello', 'There!', 'How', 'Are', 'You', 'Doing?']

Challenge 11: EvenOdd

Write a function evenOdd(numbers) that will take in an array of integers. It should return an array telling whether each item in the original array is even or odd.

[1, 2, 3, 4, 5]
=>
['odd', 'even', 'odd', 'even', 'odd']

Challenge 12: Names of Old Relatives

Write a function that takes an array of objects of the following format:

{
    familyMember: 'Uncle',
    age: 52,
    location: 'California, USA'
}

Return an array of the familyMember properties, including only those family members that are above age 40.

[
    {
        familyMember: 'Uncle',
        age: 52,
        location: 'San Diego, CA'
    }, {
        familyMember: 'Grandpa',
        age: 84,
        location: 'San Francisco, CA'
    }, {
        familyMember: 'Sister',
        age: 19,
        location: 'New York, NY'
    }
]

=>

['Uncle', 'Grandpa']

Challenge 13: Sum Array

Write a function add(numbers) that takes in an array of numbers. Return the sum of the numbers.

[1, 2, 3, 4, 5]
=>
15

Challenge 14: Find the Longest Word

Array To Object

Write a function toObject(strings) that takes in an array of strings. Return an object that has each of those strings as properties, with a value of true.

toObj(['a', 'b', 'c', 'd'])
=>
{
   a:true, 
   b:true, 
   c:true, 
   d:true
}

Challenge 15: Subset

Write a function isSubset(arr1, arr2) that takes in two arrays and returns true if the second array contains all items that are present in the first array. Otherwise return false.

Input arrays will NOT contain arrays, objects, or functions.

isSubset(
    ['abc', 17, 'def'],
    ['def', 'abc', null, 17, 24]
); 
=> true

Challenge 16: Find the minimum value of array

Implement a function findMinimum(arr) that finds the smallest number in the given array.

arr = [9,2,3,6]

=> 2

Challenge 17: Rearrange Positive & Negative Values

Implement a function, reArrange(arr), which sorts the elements so that all the negative elements appear on the left, and all positive elements appear at the right. we consider zero to be a positive​ integer for this challenge!

[10,-1,20,4,5,-9,-6]
=>
[-1,-9,-6,10,20,4,5]

Test case:

[-1,5,-4,-3] => -1,-3,-4,5	
[300,-1,3,0] => -1,300,3,0	
[0,0,0,-2] => -2,0,0,0
@hainga2505
Copy link

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

function kidsWithCandies(candies, extraCandies) {
    const maxNum = Math.max(...candies)
    
return candies.map(i=>(i+extraCandies)>=maxNum)
};

@hainga2505
Copy link

https://leetcode.com/problems/find-numbers-with-even-number-of-digits

function findNumbers(nums) {
let total = 0;
 nums.forEach((item)=>{
if(item.toString().split('').map(Number).length%2 === 0){
       total ++;
}})
 return total
};

@hainga2505
Copy link

hainga2505 commented Jun 23, 2023

https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/

function subtractProductAndSum(n) {
    const newNums = n.toString().split('').map(Number);
return newNums.reduce((total,i)=>{return total*i},1) - newNums.reduce((total,i)=>{return total+i},0);
};
/**
 * @param {number} n
 * @return {number}
 */
var subtractProductAndSum = function(n) {
    let product = 1;
    let sum = 0;
    while (n !== 0) {
        // n = 2
        let remainder = n % 10; // remainder = 2
        product = product * remainder; //  12 * 2 =. 24
        sum = sum + remainder; //.  7 + 2 =. 9
        n = Math.floor(n / 10) // n = 0
    }
    return product - sum;
};

@QuocCao-dev
Copy link
Author

https://leetcode.com/problems/determine-if-string-halves-are-alike/

function halvesAreAlike(s) {
    const vowels = ["a","i","e","o","u"];
    let count1 = 0;
    let count2 = 0;
    const halve1 = s.slice(0,s.length/2).toLowerCase();
    const halve2 = s.slice(s.length/2).toLowerCase();
for (let i =0;i<halve1.length;i++){
    if(vowels.includes(halve1[i])){
        count1 ++;
    }
}
for (let j =0;j<halve2.length;j++){
    if(vowels.includes(halve2[j])){
        count2 ++;
    }   
}
if(count1 === count2){
     return   true
    }else{
     return   false
    }
};
return count1 === count2

@QuocCao-dev
Copy link
Author

var halvesAreAlike = function(s) {
  let firstCount = 0;
  let secondCount = 0;
  let vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
  for (let i = 0; i < s.length; i++) {
    if (i < s.length / 2 && vowels.includes(s[i])) {
      firstCount++;
    }
    if (i >= s.length / 2 && vowels.includes(s[i])) {
      secondCount++;
    }
  }
    
 return firstCount === secondCount;
 
};

@QuocCao-dev
Copy link
Author

return nums.map(num => num.toString().length).filter(num => num % 2 === 0).length

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment