[알고리즘] 2024-02-14(수)

dev-riverkim·2024년 3월 13일
0

8 kyu - Remove exclamation marks

Write function RemoveExclamationMarks which removes all exclamation marks from a given string.

Solution

function removeExclamationMarks(s) {
  // 주어진 문자열에서 모든 느낌표를 제거하는 함수 RemoveExclamationMarks를 작성합니다.

  return s.replaceAll('!','');;
}
function removeExclamationMarks(s) {
  return s.replace(/!/gi, '');
}
function removeExclamationMarks(s) {
  return s.split('!').join('');
}

8 kyu - Calculate average

Write a function which calculates the average of the numbers in a given list.

Note: Empty arrays should return 0.

Solution

function findAverage(array) {
  // 주어진 목록에 있는 숫자의 평균을 계산하는 함수를 작성하세요.
  //참고: 빈 배열은 0을 반환해야 합니다.
  // your code here
  
  let sum = 0;
  let divide = array.length;

  const result = array.map((item) => {
    return  sum += item
  })
  
  
  if(array.length === 0){
    return 0
  } else {
    return sum / divide
  }
  
}
var find_average = (array) => {  
  return array.length === 0 ? 0 : array.reduce((acc, ind)=> acc + ind, 0)/array.length
}
function find_average(array) {
  if (array.length === 0) {
  return 0;
  }
  var result = 0;
  for (i=0; i<array.length; i++) {
    result +=array[i];
  }
  return result/array.length;
}
function find_average(arr) {
    return arr.length > 0? arr.reduce((a, b) => a + b) / arr.length : 0;
}

8 kyu - Sentence Smash

Write a function that takes an array of words and smashes them
together into a sentence and returns the sentence. You can ignore any
need to sanitize words or add punctuation, but you should add spaces
between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!

Example

['hello', 'world', 'this', 'is', 'great']  =>  'hello world this is great'

Solution

function smash (words) {
  // 단어 배열을 받아 한 문장으로 뭉쳐서 문장을 반환하는 함수를 작성합니다. 
  // 단어를 띄어쓰거나 구두점을 추가할 필요는 없지만 각 단어 사이에 공백을 추가해야 합니다. 
  // 문장의 시작이나 끝에 공백이 있으면 안 되니 주의하세요!
  return words.join(" ");
};
// Smash Words
const smash = words => words.join(' ');
profile
dev-riverkim

0개의 댓글