Swift and JavaScript comparison snippets(5) — Control Flow

Written by unbug | Published 2018/07/01
Tech Story Tags: javascript | swift | swift-and-javascript | swift-vs-javascript

TLDRvia the TL;DR App

For-In Loops

Swift

let names = ["Anna", "Alex", "Brian", "Jack"]for name in names {print("Hello, \(name)!")}// Hello, Anna!// Hello, Alex!// Hello, Brian!// Hello, Jack!

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs {print("\(animalName)s have \(legCount) legs")}// ants have 6 legs// cats have 4 legs// spiders have 8 legs

JavaScript

const names = ["Anna", "Alex", "Brian", "Jack"]for (let i = 0; i < names.length; i++) {console.log(`Hello, ${names[i]}!`)}// Hello, Anna!// Hello, Alex!// Hello, Brian!// Hello, Jack!

const numberOfLegs = {"spider": 8, "ant": 6, "cat": 4}Object.keys(numberOfLegs).forEach(animalName => {const legCount = numberOfLegs[animalName];console.log(`${animalName}s have ${legCount} legs`)})// ants have 6 legs// cats have 4 legs// spiders have 8 legs

While Loops

Swift

let finalSquare = 25var board = [Int](repeating: 0, count: finalSquare + 1)board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08var square = 0var diceRoll = 0while square < finalSquare {// roll the dicediceRoll += 1if diceRoll == 7 { diceRoll = 1 }// move by the rolled amountsquare += diceRollif square < board.count {// if we're still on the board, move up or down for a snake or a laddersquare += board[square]}}print("Game over!")

JavaScript

let finalSquare = 25let board = []for (let i = 0; i < finalSquare.length; i++) {board[i] = 0;}board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08let square = 0let diceRoll = 0while (square < finalSquare) {// roll the dicediceRoll += 1if (diceRoll == 7) { diceRoll = 1 }// move by the rolled amountsquare += diceRollif (square < board.length) {// if we're still on the board, move up or down for a snake or a laddersquare += board[square]}}console.log("Game over!")

Conditional Statements

Swift

var temperatureInFahrenheit = 30temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 {print("It's very cold. Consider wearing a scarf.")} else if temperatureInFahrenheit >= 86 {print("It's really warm. Don't forget to wear sunscreen.")} else {print("It's not that cold. Wear a t-shirt.")}// Prints "It's really warm. Don't forget to wear sunscreen."

JavaScript

let temperatureInFahrenheit = 30temperatureInFahrenheit = 90if (temperatureInFahrenheit <= 32) {console.log("It's very cold. Consider wearing a scarf.")} else if (temperatureInFahrenheit >= 86) {console.log("It's really warm. Don't forget to wear sunscreen.")} else {console.log("It's not that cold. Wear a t-shirt.")}// Prints "It's really warm. Don't forget to wear sunscreen."

Switch

Swift

let someCharacter: Character = "z"switch someCharacter {case "a":print("The first letter of the alphabet")case "z":print("The last letter of the alphabet")default:print("Some other character")}// Prints "The last letter of the alphabet"

JavaScript

const someCharacter = "z"switch (someCharacter) {case "a":console.log("The first letter of the alphabet")break;case "z":console.log("The last letter of the alphabet")break;default:console.log("Some other character")}// Prints "The last letter of the alphabet"

More of Swift and JavaScript comparison snippets


Published by HackerNoon on 2018/07/01