[알고리즘] 2024-02-06(화)

dev-riverkim·2024년 3월 13일
0

8 kyu - Filter out the geese

Write a function that takes a list of strings as an argument and returns a
filtered list containing the same elements but with the 'geese' removed.

The geese are any strings in the following array, which is pre-populated in your solution:

  ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"]

For example, if this array were passed as an argument:

 ["Mallard", "Hook Bill", "African", "Crested", "Pilgrim", "Toulouse", "Blue Swedish"]

Your function would return the following array:

["Mallard", "Hook Bill", "Crested", "Blue Swedish"]

The elements in the returned array should be in the same order as in
the initial array passed to your function, albeit with the 'geese'
removed. Note that all of the strings will be in the same case as those
provided, and some elements may be repeated.

Solution

function gooseFilter (birds) {
  var geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"];
  return birds.filter(x=> !geese.includes(x))
};
function gooseFilter (birds) {
  var geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"];
  return birds.filter(b => !geese.includes(b));
};
function gooseFilter (birds) {
  var geese = ["African", "Roman Tufted", "Toulouse", "Pilgrim", "Steinbacher"];
  return birds.filter( bird => geese.indexOf(bird) < 0 );
};

8 kyu - A Needle in the Haystack

Can you find the needle in the haystack?

Write a function findNeedle() that takes an array full of junk but containing one "needle"

After your function finds the needle it should return a message (as a string) that says:

"found the needle at position " plus the index it found the needle, so:

Example(Input --> Output)

["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5"

Note: In COBOL, it should return "found the needle at position 6"

Solution

function findNeedle(haystack) {
  
  const index = haystack.findIndex((element, index, arr) => element === 'needle');
  
  return `found the needle at position ${index}`
  
}
function findNeedle(haystack) {
  return "found the needle at position " + haystack.indexOf("needle");
}
const findNeedle = haystack => `found the needle at position ${haystack.indexOf('needle')}`;

7 kyu - Binary Addition

mplement a function that adds two numbers together and returns their
sum in binary. The conversion can be done before, or after the addition.

The binary number returned should be a string.

Examples:(Input1, Input2 --> Output (explanation)))

1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary)
5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary)

Solution

function addBinary(a,b) {
  // 10진수를 2진수로 변경

  return (a+b).toString(2)

}

자바스크립트에서는 toString() 과parseInt() 를 이용해 손쉽게 숫자 진수들을 변환 할 수 있다.

var decimal = 1023; 

var binary = decimal.toString(2);	// 2진수로
var octal = decimal.toString(8);	// 8진수로
var hex = decimal.toString(16);		// 16진수로
profile
dev-riverkim

0개의 댓글