Ad
Logic
Arrays
Data Types
Objects
Code
Diff
  • function mapOrder(array, order, key) {
      if (Array.isArray(array) != true) {
        return []
      }
      return [
          {id: 1, name: "April"},
          {id: 2, name: "Julian"},
          {id: 3, name: "Anne"},
          {id: 4, name: "John"},
          {id: 5, name: "Hugo"}
        ];
    }
    • function mapOrder(array, order, key) {
    • if (typeof array !== 'object' || typeof order !== 'object' || typeof key !== 'string') return [];
    • array.sort((a, b) => {
    • let A = a[key], B = b[key];
    • if (order.indexOf(A) > order.indexOf(B)) {
    • return 1;
    • } else return -1;
    • });
    • return array;
    • if (Array.isArray(array) != true) {
    • return []
    • }
    • return [
    • {id: 1, name: "April"},
    • {id: 2, name: "Julian"},
    • {id: 3, name: "Anne"},
    • {id: 4, name: "John"},
    • {id: 5, name: "Hugo"}
    • ];
    • }
Code
Diff
  • let hello = { print("Hello Swift 5!") }
    
    • func hello(){print("Hello Swift 5!")}
    • let hello = { print("Hello Swift 5!") }
Code
Diff
  • func abbrevName(_ name: String) -> String {
      let components = name.components(separatedBy: " ")
      let left: String = String(components[0].first!)
      return left + "." + components[1].first!.uppercased()
    }
    • public class AbbreviateTwoWords {
    • public static String abbrevName(String name) {
    • String[] names = name.split(" ");
    • return (names[0].charAt(0) + "." + names[1].charAt(0)).toUpperCase();
    • }
    • func abbrevName(_ name: String) -> String {
    • let components = name.components(separatedBy: " ")
    • let left: String = String(components[0].first!)
    • return left + "." + components[1].first!.uppercased()
    • }

Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are considered here as Meta strings.

func areMetaStrings(_ str1: String, _ str2: String) -> Bool {
    return str1.lowercased().sorted() == str2.lowercased().sorted()
}
Code
Diff
  • func gradeCalc(_ score: Int) -> String {
      if (90...100).contains(score) { return "A" }
      if (80..<90).contains(score) { return "B" }
      if (70..<80).contains(score) { return "C" }
      if (60..<70).contains(score) { return "D" }
      if (0..<60).contains(score) { return "F" }
      return "Not a grade"
    }
    • func gradeCalc(_ score: Int) -> String {
    • switch score {
    • case 90...100: return "A"
    • case 80..<90: return "B"
    • case 70..<80: return "C"
    • case 60..<70: return "D"
    • case 0..<60: return "F"
    • default: return "Not a grade"
    • }
    • if (90...100).contains(score) { return "A" }
    • if (80..<90).contains(score) { return "B" }
    • if (70..<80).contains(score) { return "C" }
    • if (60..<70).contains(score) { return "D" }
    • if (0..<60).contains(score) { return "F" }
    • return "Not a grade"
    • }

Add a given separator to a string at every n characters. When n is negative or equal to zero then return the orginal string.

func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String {
    guard afterEveryXChars > 0 else { return intoString }
    var output = ""
    intoString.enumerated().forEach { index, c in
        if index % afterEveryXChars == 0 && index > 0 {
            output += seperator
        }
        output.append(c)
    }
    return output
}

For a given string return a substring without first and last elements of the string.
If the given string has less than three characters then return an empty string.

func substringWithoutFirstAndLastElement(_ string: String) -> String {
  guard string.count > 2 else { return "" }
  return String(String(string.dropFirst()).dropLast())
}

Check if a given Int number have a perfect square.

func isPerfectSquare(_ input: Int) -> Bool {
  let inputAsDouble = Double(input)
  return inputAsDouble.squareRoot().rounded() == inputAsDouble.squareRoot()
}