Skip to content

Instantly share code, notes, and snippets.

@ericberglund117
Last active July 2, 2021 21:38
Show Gist options
  • Save ericberglund117/ac2235fb992f511712d14fc76e95ff36 to your computer and use it in GitHub Desktop.
Save ericberglund117/ac2235fb992f511712d14fc76e95ff36 to your computer and use it in GitHub Desktop.
Codewars Solutions

Codewars Solutions

Create Phone Number (6 kyu)

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example: createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890" The returned format must be correct in order to complete this challenge. Don't forget the space after the closing parentheses!

Answer

function createPhoneNumber(numbers){
  const string = numbers.join("")
  const areaCode = string.slice(0, 3)
  const format = string.slice(3, 6) + "-" + string.slice(6, 10)
  return `(${areaCode}) ${format}`
}

Who Likes it? (6 kyu)

You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.

Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:

likes [] -- must be "no one likes this" likes ["Peter"] -- must be "Peter likes this" likes ["Jacob", "Alex"] -- must be "Jacob and Alex like this" likes ["Max", "John", "Mark"] -- must be "Max, John and Mark like this" likes ["Alex", "Jacob", "Mark", "Max"] -- must be "Alex, Jacob and 2 others like this" For 4 or more names, the number in and 2 others simply increases.

Answer

function likes(names) {
  if(names.length === 0) {
   return 'no one likes this';
  } 
  if (names.length === 1) {
     const one = names.map(name => {
        return `${name} likes this`
    })  
     return one.toString()
  } else if (names.length === 2) {
     for (let i = 0; i < names.length; i++) {
      return `${names[i]} and ${names[i + 1]} like this`
    } 
  } else if (names.length === 3) {
     for (let i = 0; i < names.length; i++) {
      return `${names[i]}, ${names[i + 1]} and ${names[i + 2]} like this`
    }
  } else {
     for (let i = 0; i < names.length; i++) {
      return `${names[i]}, ${names[i + 1]} and ${names.length - 2} others like this`
    }
  }
}

Unique in Order (6 kyu)

Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.

For example:

uniqueInOrder('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B'] uniqueInOrder('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D'] uniqueInOrder([1,2,2,3,3]) == [1,2,3]

Answer

var uniqueInOrder=function(iterable){
  if(typeof(iterable) === 'string') {
    const array = iterable.split('');
    const result = []
    for (let i = 0; i < array.length; i++) {
      if (array[i -1] !== array[i]) {
        result.push(array[i])
      }
    }
    return result
  } else {
    const result = []
    for (let i = 0; i < iterable.length; i++) {
      if (iterable[i -1] !== iterable[i]) {
        result.push(iterable[i])
    }
  }
  return result
  } 
};

Multiples of 3 or 5? (6 kyu)

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0(for languages that do have them)

Answer

function solution(number){
  let sum = 0;
  for (let i = 0; i < number; i++) {
    if(i % 3 === 0 || i % 5 === 0) {
      sum += i
    }
  }
  return sum
}

IronFE Solutions

https://docs.google.com/spreadsheets/d/1R8imTyYD64FPWJ_mD5QlZI0ybyU1QNkm1ntJqRT7r7k/edit#gid=2076278354

Concerts

Level 1

Question 1: Return an array of just the artists names

const artistsNames = artists.map(art => art.name)

Question 2: Write a function that takes a name as a parameter and returns the first artist where the name matches . ex. findByName('Jack Garratt')

const findbyName = (name) => {
  return artists.find(art => art.name === name)
}

findbyName('Jack Garratt')

Question 3: Write some code to add the appropriate ticket price to each artist.

artists.map(art => {
  return {name: art.name, label: art.label, price: ticketPrice[art.label]}
})

Question 4: Write a function that takes a label as a parameter and returns an array containing all the artists of that label . ex. getByLabel('atlantic')

const getByLabel = (label) => {
  return artists.filter(art => art.label === label)
}

getByLabel('atlantic')

Question 5: Write some code to determine how much it would cost to go to all to the shows

let showPrices = artists.map(art => {
  return {name: art.name, label: art.label, price: ticketPrice[art.label]}
})

let onlyPrices = showPrices.map(show => show.price)
onlyPrices.reduce((acc, price) => {
  acc += price
  return acc
}, 0)

Question 6: Make an object containing each label as keys and all the associated artists in an array as the value.

const labelArtists = artists.reduce((acc, art) => {
  if(!acc[art.label]) {
    acc[art.label] = [art.name]
  } else {
    acc[art.label].push(art.name)
  }
  return acc 
}, {})

Question 7: write some code that will add the crowdsize to each artist

artists.map(art => {
  return {name: art.name, label: art.label, crowdSize: crowd[art.label]}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment