You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
enumWeekday{case monday
case tuesday
case wednesday
case thursday
case friday
}varday=Weekday.monday
day =Weekday.tuesday
day =.wednesday
enumWeekend{case saturday, sunday
}
Type Annonations
constname: string="Ali"constscore: number=0constpi: number=3.141constisAdmin: boolean=trueconstalbums: string[]=["Century Child","Human. :II: Nature."]constolympics: Map<number,string>=newMap([[2012,"London"],[2016,"Rio"],[2021,"Tokyo"],])constbooks: Set<string>=newSet(["The Name of the Wind","The Wise Man's Fear"])
letname:String="Ali"letscore:Double=0letpi:Double= 3.141
letisAdmin:Bool= true
letalbums:[String]=["Century Child","Human. :II: Nature."]letolympics:[Int:String]=[2012:"London",2016:"Rio",2021:"Tokyo"]letbooks:Set<String>=Set(["The Name of the Wind","The Wise Man's Fear"])
Conditions
if/else
constage=18constisAdmin=trueif(age>=18&&isAdmin){console.log("You're authenticated")}elseif(age<18){console.log("You're not an adult")}elseif(!isAdmin){console.log("Contact with an admin")}else{console.log("You're unauthenticated")}
letage=18letisAdmin= true
if age >=18 && isAdmin {print("You're authenticated")}else if age <18{print("You're not an adult")}else if !isAdmin {print("Contact with an admin")}else{print("You're unauthenticated")}
letalphabet=["a","b","c"]
for char in alphabet {print(char) // "a", "b", "c"
}
for num in 1...10{
if num ==5{
break
}
if num %2==1{
continue
}print(num) // 2, 4
}
for num in 3..<5{print(num) // 3, 4
}
while Loop
letnum=0while(num!==4){num=Math.ceil(Math.random()*10)console.log(num)// 1 to 10}
varnum=0
while num !=4{
num =Int.random(in:1...10)print(num) // 1 to 10
}
Functions
constisEven=({ num })=>{constres=num%2===0returnres}console.log(isEven({num: 3}))// false
func isEven(num:Int)->Bool{letres= num %2==0return res
}print(isEven(num:3)) // false
constisOdd=(num)=>{returnnum%2===1}console.log(isOdd(3))// trueconstprintTimesTable=({for: num})=>{for(leti=1;i<=3;i++){console.log(`${i} x ${num} = ${i*num}`)}}printTimesTable({for: 5})// "1 x 5 = 5", "2 x 5 = 10", "3 x 5 = 15"
func isOdd(_ num:Int)->Bool{
num %2==1}print(isOdd(3)) // true
func printTimesTable(for num:Int){
for i in 1...3{print("\(i) x \(num) = \(i * num)")}}printTimesTable(for:5) // "1 x 5 = 5", "2 x 5 = 10", "3 x 5 = 15"
Default Values for Parameters
constprintNumbers=({ from, to =10})=>{for(leti=from;i<=to;i++)console.log(i)}printNumbers({from: 8})// 8, 9, 10
func printNumbers(from:Int, to:Int=10){
for i in from...to {print(i)}}printNumbers(from:8) // 8, 9, 10
Handle Errors in Functions
constPasswordError=Object.freeze({short: Symbol("short"),obvious: Symbol("obvious"),})constcheckPassword=(password)=>{constlength=password.lengthif(length<5)throwPasswordError.shortif(password==="12345")throwPasswordError.obviousif(length<8)return"Ok"if(length<10)return"Good"return"Excellent"}try{constres=checkPassword("12345")console.log(res)}catch(err){if(err===PasswordError.short){console.log("Short password")}elseif(err===PasswordError.obvious){console.log("Try a stronger password.")// "Try a stronger password."}else{console.log("An error occured.")}}
enumPasswordError:Error{case short, obvious
}func checkPassword(_ password:String)throws->String{letcount= password.count
if count <5{throwPasswordError.short
}
if password =="12345"{throwPasswordError.obvious
}
if count <8{return"Ok"}
if count <10{return"Good"}return"Excellent"}do{letres=trycheckPassword("12345")print(res)}catchPasswordError.short {print("Short password")}catchPasswordError.obvious {print("Try a stronger password.") // "Try a stronger password."
}catch{print("An error occured")}