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

dev-riverkim·2024년 3월 13일
0

8 kyu - Remove String Spaces

Write a function that removes the spaces from the string, then return the resultant string.

Examples:

Input -> Output
"8 j 8   mBliB8g  imjB8B8  jl  B" -> "8j8mBliB8gimjB8B8jlB"
"8 8 Bi fk8h B 8 BB8B B B  B888 c hl8 BhB fd" -> "88Bifk8hB8BB8BBBB888chl8BhBfd"
"8aaaaa dddd r     " -> "8aaaaaddddr"

Solution

function noSpace(x){
  return x.split(" ").join('')
}
function noSpace(x){
  return x.replace(/\s/g, '');
}
function noSpace(x){return x.split(' ').join('')}

8 kyu - Grasshopper - Messi goals function

Messi goals function

Messi is a soccer player with goals in three leagues:

  • LaLiga
  • Copa del Rey
  • Champions

Complete the function to return his total number of goals in all three leagues.

Note: the input will always be valid.

For example:

5, 10, 2  -->  17

Solution

function goals (laLigaGoals, copaDelReyGoals, championsLeagueGoals) {
  return laLigaGoals + copaDelReyGoals + championsLeagueGoals
}
const goals = (...a) => a.reduce((s, v) => s + v, 0);
const goals = (a,b,c) => a + b + c;

7 kyu - Reverse words

Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples

"This is an example!" ==> "sihT si na !elpmaxe"
"double  spaces"      ==> "elbuod  secaps"

Solution

function reverseWords(str) {
  
  const words = str.split(" ");

  const newWord = words.map((item)=>{
   return item.split("").reverse().join("")
  })

  return newWord.join(" ")
  
  
}
function reverseWords(str) {
  return str.split(' ').map(function(word){
    return word.split('').reverse().join('');
  }).join(' ');
}
function reverseWords(str) {
  // Go for it
  //split words into seperate arrays
  return str.split("").reverse().join("").split(" ").reverse().join(" ");
}
function reverseWords(str) {
  return str.split(' ').map( str => str.split('').reverse().join('') ).join(' ');
}
profile
dev-riverkim

0개의 댓글