algorithm problems Flashcards

1
Q

We’ll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10.

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

Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.

Note: You can return the array with its elements in any order.

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

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Note: You have to use the arguments object.

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

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

For example, if the first argument is [{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], and the second argument is { last: “Capulet” }, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.

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

Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.

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

Pig Latin is a way of altering English Words. The rules are as follows:

  • If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add ay to it.
  • If a word begins with a vowel, just add way at the end.

Create a JS function that takes a string and turns it into Pig Latin.

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

What does the .substring() method do in JavaScript?

A

This method extracts characters, between two indices (positions), from a string, and returns the string formed between those two indices.

It extracts characters from start to end (the end indice excluded).

It does not change the original string.

If start is greater than end, arguments are swapped: (4, 1) = (1, 4).

Start or end values less than 0, are treated as 0.

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

Using JavaScript, perform a search and replace on the sentence using the arguments provided and return the new sentence.

First argument is the sentence to perform the search and replace on.

Second argument is the word that you will be replacing (before).

Third argument is what you will be replacing the second argument with (after).

Note: Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word Book with the word dog, it should be replaced as Dog

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

Pairs of DNA strands consist of nucleobase pairs. Base pairs are represented by the characters AT and CG, which form building blocks of the DNA double helix.

The DNA strand is missing the pairing element. Write a function to match the missing base pairs for the provided DNA strand. For each character in the provided string, find the base pair character. Return the results as a 2d array.

For example, for the input GCG, return [[“G”, “C”], [“C”,”G”], [“G”, “C”]]

The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.

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

In JavaScript, what is the .charCodeAt() method?

A

The charCodeAt() method returns the Unicode of the character at a specified index (position) in a string.

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

Find the missing letter in the passed letter range and return it.

If all letters are present in the range, return undefined.

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

In JavaScript, what does the .fromCharCode() method do?

A

This method converts Unicode values to characters.

It is a static method of the String object.

The syntax is always String.(name of this method)

You cannot use myString.(name of this method)

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

In JavaScript, what is the .new Set() method?

What is a Set?

A

A method that creates a new X.

A JavaScript X is a collection of unique values.

Each value can only occur once in a X.

A X can hold any value of any data type.

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

In JavaScript, what is the .flat() method?

A

The X method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

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

Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.

In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.

The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.

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

Use Javascript to convert the characters &, <, >, “ (double quote), and ‘ (apostrophe), in a string to their corresponding HTML entities.

A
17
Q

Given a positive integer num, create a function that returns the sum of all odd Fibonacci numbers that are less than or equal to num.

The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.

For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than or equal to 10 are 1, 1, 3, and 5.

A
18
Q

What’s the ES6 code to find out the greatest common denominator?

A

const gcd = (a, b) => (b === 0) ? a : gcd(b, a % b);

19
Q

What’s the ES6 code to find out the least common multiple?

A

const lcm = (a, b) => (a * b) / gcd(a, b);

20
Q

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.

A
21
Q

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.

A
22
Q

What will the following lines of code return, respectively?

Array(4)

Array(2)

Array(4,2)

Array(false, 4, 3, 2, true, 35)

A

[, , ,]

[,]

[4, 2]

[false, 4, 3, 2, true, 35]

23
Q

What will the following lines of code return, respectively?

Array(4).fill(0)

Array(4).fill(0, 2)

Array(4).fill(0, 3)

Array(6, false, 34, “empire”, 90, “anti”, “c”).fill(3, 4)

A

[0, 0, 0, 0]

[, , 0, 0]

[, , , 0]

[, , , 0]

[6, false, 34, “empire”, 3, 3, 3]