Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Last active October 1, 2022 05:53
Show Gist options
  • Save QuocCao-dev/b08a40e740eae46eb3b6bef89d5b220f to your computer and use it in GitHub Desktop.
Save QuocCao-dev/b08a40e740eae46eb3b6bef89d5b220f to your computer and use it in GitHub Desktop.
  • Test your concepts of arrays by determining the location of an item in an array.
const indexOf = (arr, item) => {
    // Your code here
}
indexOf([1,2,3,4], 3) => 2
indexOf([5,6,7,8], 8) => 3
indexOf([1,2,3,4], 5) => -1

  • Evaluate your concepts by populating an array.
const sum = (arr) => {
    // Your code here
}
sum([1,2,3,4]) => 10
sum([5,6,7,8]) => 26
sum([-1,-2,1,2]) => 0

  • Evaluate yourself by returning a new array after removing all instances of a value from an array.
// Returns a new array
const remove = (arr, item) => {
  // Your code here
}
remove([1,2,3,4,2], 2) => [1,3,4]
remove([5,6,7,8,6], 6) => [5,7,8]
remove([1], 1) => []

  • Evaluate yourself by adding an item to the end of an array
const append = (arr, item) => {
  // Your code here
}
append([1,2,3,4], 5) => [1,2,3,4,5]
append([], 1) => [1]

  • Evaluate yourself by removing the last item of an array
const truncate = (arr) => {
    // Your code here
}
truncate([1,2,3,4,5]) => [1,2,3,4]
truncate([1]) => []

  • Test yourself by Adding an item at the beginning of an array.
// Prepends to the original array
const prepend = (arr, item) => {
    // Your code here
}
prepend([2,3,4,5], 1)	=> [1,2,3,4,5]
prepend([], 1) => [1]

  • Test yourself by removing the first item of an array.
// Modifies the original array
const curtail = (arr) => {
    // Your code here
}
curtail([1,2,3,4,5]) => [2,3,4,5]
curtail([1]) => []

  • Test yourself by adding an item anywhere in an array.
// Inserts an element in the original array
const insert = (arr, item, index) => {
      // Your code here
}
insert([1,2,4], 3, 2) => [1,2,3,4]
insert([2], 1, 0)	=> [1,2]
insert([], 1, 0)	=> [1]

  • Test yourself by counting the occurrences of an item in an array.
const count = (arr, item) => {
    // Your code here
}
count([1,2,3,2,4,2], 2) => 3
count([5,6,7,8], 2) => 0
count([], 1) => 0

  • Test yourself by finding the duplicates in an array.
const duplicates = (arr) => {
    // Your code here
}
duplicates([1,2,3,2,4,2,1])	 => [1,2]
duplicates([5,6,7,8]) => []
duplicates([]) => []

  • Test yourself by Squaring each number in an array.
const square = (arr) => {
      // Your code here
}
square([1,2,3,10])	=> [1,4,9,100]	
square([])	=> []

  • Test yourself by finding all occurrences of an item in an array.
const findAllOccurrences = (arr, target) => {
  
}
findAllOccurrences([1,2,3,4], 3) => [2]
findAllOccurrences([5,6,7,8,6], 6) => [1,4]
findAllOccurrences([1,2,3], 4) => []
findAllOccurrences([], 1)	=> []

Exercise 2: Make Maximum Donuts

Problem statement:

Create a function maxDonuts that returns the maximum number of whole donuts that can be cooked from a recipe.

Input

Two objects:

  • The first object is the recipe for the donut
  • the second object is the available quantity of ingredients.
Output
maxDonuts(
  { flour: 2, sugar: 40, butter: 20 },
  { flour: 5, sugar: 120, butter: 500 })
=> 2

maxDonuts(
    {flour: 100, sugar: 4, butter: 10} , 
    {flour: 1500, sugar: 10, butter: 100})
=> 2

maxDonuts(
    ({flour: 100, sugar: 50, butter: 10} , 
    {flour: 190, sugar: 50, butter: 5}))
=> 0

Exercise 3: Calculate Score

Problem statement:

In this challenge, you have to implement the function boySum that takes the parameter records. Here records is an array of objects that contains two properties, gender and value. The gender can either be BOYS or GIRLS. Your task is to return the sum of all the values that have BOYS as the gender.

Input:

The array records containing objects

Output:

The sum of all the values of the objects which have BOYS as the gender.

const records = [
    {
        value: 24,
        gender: "BOYS"
    },
    {
        value: 42,
        gender: "BOYS"
    },
    {
        value: 85,
        gender: "GIRLS"
    },
    {
        value: 12,
        gender: "GIRLS"
    },
    {
        value: 10,
        gender: "BOYS"
    }
]
=> 76
  • Evaluate your string concepts by reducing the consecutive duplicate characters to the desired minimum.
const reduceString = (str, amount) => {
    // your code here

}
reduceString('aaaabbbb', 2) // 'aabb'
reduceString('xaaabbbb', 2) // 'xaabb'
reduceString('aaaabbbb', 1) // 'ab'
reduceString('aaxxxaabbbb', 2) // 'aaxxaabb'

  • Test yourself by reversing a string.
const reverseString = (str) => {
    // your code here

}
reverseString('abc') // 'cba'
reverseString('hello') // 'olleh'
reverseString('hello world') // 'dlrow olleh'
Copy link

ghost commented Oct 1, 2022

//Exercise 1
1.
const findIndex = (arr, item) => {
return arr.indexOf(item);
};
console.log(findIndex([1, 2, 3, 4], 3))
console.log(findIndex([5, 6, 7, 8], 8))
console.log(findIndex([1, 2, 3, 4], 5))

const sum = (arr) => {
return arr.reduce((a, b) => a + b, 0);
};
console.log(sum([1, 2, 3, 4]), sum([5, 6, 7, 8]), sum([-1, -2, 1, 2]));

const remove = (arr, item) => {
const result = arr.filter((num) => num !== item)
console.log(result);
}
remove([1,2,3,4,2], 2)
remove([5,6,7,8,6], 6)
remove([1], 1)

const append = (arr, item) => {
arr.push(item);
return arr;
};
console.log(append([1, 2, 3, 4], 5), append([], 1));

const truncate = (arr) => {
arr.pop();
return arr
}
console.log(truncate([1,2,3,4,5]),
truncate([1]))

const curtail = (arr) => {
arr.shift()
return arr
}
console.log((curtail([1,2,3,4,5])),(curtail([1])))

const insert = (arr, item, index) => {
arr.splice(index, 0, item);
return arr;
};
console.log(insert([1, 2, 4], 3, 2), insert([2], 1, 0), insert([], 1, 0));

const count = (arr, item) => {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == item) count++;
}
return count
};
console.log(count([1, 2, 3, 2, 4, 2], 2), count([5, 6, 7, 8], 2), count([], 1));

const duplicates = (arr) => {
const count = new Set();
return arr.filter(num => count.size === count.add(num).size);
};
console.log(
duplicates([1, 2, 3, 2, 4, 2, 1]), duplicates([5, 6, 7, 8]), duplicates([])
);

const square = (arr) => {
for (let i = 0; i < arr.length; i++) {
console.log(Math.pow(arr[i], 2))
}
};
square([1, 2, 3, 10])
square([]);

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