Strings (JavaScript)

Strings

> Tutorial

Strings

Character strings are a type of value used to represent text.

A string is written between single or double quotes:

String operations

The + operator is the concatenation operator: it "glues" two strings together:

let a = "Hello"
let b = "how are you?"
let c = a + ' ' + b // "Hello how are you?"

Many operations are available on strings. The main ones are:

string.length              // length of the string
string[position]           // gets the character at a given position
string.slice(start, end)   // extracts the substring from index start (included) to end (excluded)
string.toUpperCase()       // converts all letters to uppercase

Examples:

"labyrinthe".length        // returns 10
"pirate"[2]                // returns "r"
"galactique".slice(2, 7)   // returns "lacti"
"mafia".toUpperCase()      // returns "MAFIA"

Greeting your opponent

The enemy.name property returns the name of an entity. We'll use it to greet our opponent on turn 1:

if (Fight.turn == 1) {
	me.say("Hi " + enemy.name + "!")
}

The me.say() method makes your leek say something; it costs 1 TP . For an enemy named Domingo, our leek will say: "Hi Domingo!". Feel free to personalize it, as long as you stay polite!

❓ Quiz

What is the result of the code "10" + 5 + "aa"?

"105aa" "15aa" "10aaaaaaaa" "15aaaaa"

What is the result of the code "eucalyptus".slice(4, 7)?

"lyp" "euc" "ptus" "euca"

Full AI

const me = Fight.me

// We take the pistol if it is not equipped
if (me.weapon == null) {
	me.setWeapon(Weapon.pistol)
}

// We get the nearest enemy
const enemy = Fight.getNearestEnemy()

// We greet our opponent
if (Fight.turn == 1) {
	me.say("Hi " + enemy.name + "!")
}

// We move toward the enemy
me.moveToward(enemy)

// Boosts, shield and heal
me.useChip(Chip.motivation)
me.useChip(Chip.protein)
me.useChip(Chip.helmet)

// We heal ourselves if our life is below 75% of our max life.
if (me.life < me.maxLife * 0.75) {
	me.useChip(Chip.bandage)
}

// We try to shoot at it!
me.useWeapon(enemy)