AJS and Strings practice questions

 1) Check whether a string is Palindrome or not 

/**
* Palindrome
Check if the given string is a palindrome string.

A sequence is said to be palindrome if reverse of the sequence is same as the initial sequence.

Input
Single line containing a string with no end of line character.

String sequence contain only alphabets. Consider lower case letters as equivalent to upper case letters.

Output
Single containing YES if it is a palindrome

Single containing NO if it isn't a palindrome

Example
Input:

Tenet

Output:

YES
*/

let fs = require("fs");
let data = fs.readFileSync(0, "utf-8");
let idx = 0;
data = data.split("\n");

function readLine() {
idx++;
return data[idx - 1].trim();
}

// -------- Do NOT edit anything above this line ----------
// Use readLine() for taking input, it will read one line of from the input and is stored in string format

let string = readLine();

let lenght = string.length;

let mark = true; // we are assuming that initially the sting is Palindrome.

for (let i = 0; i < lenght / 2; i++) {
if (string[i] != string[lenght - 1 - i]) {
console.log("NO");
mark = false;
break;
}
}
if (mark == true) {
console.log("YES");
}





2) Airplane-prototypes 














class Airplane {
constructor(name) {
this.name = name;
this.isflying = false;
}

takeoff() {
this.isflying = true;
}
land() {
this.isflying = false;
}

// this function return if the Airplane is flying or not
ifAirplaneFlying() {
return this.isflying;
}
}

let airplane = new Airplane("indigo");

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not before taking off

airplane.takeoff(); // it will takeoff and set isflying as true.

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not after takeoff

airplane.land(); // it will land and set isflying to false;

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not after landing.




//By using .prototype property of the class

class Airplane {
constructor(name) {
this.name = name;
this.isflying = false;
}

// this function return if the Airplane is flying or not
// ifAirplaneFlying() {
// return this.isflying;
// }
}

let airplane = new Airplane("indigo");

// Creating a method ifAirplaneFlying in the prototype property of the class Airplane
Airplane.prototype.ifAirplaneFlying = function () {
return this.isflying;
};

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not before taking off

// Creating a method takeoff() the prototype property of the class Airplane
Airplane.prototype.takeoff = function () {
this.isflying = true;
};

airplane.takeoff(); // calling the .takeoff() method using the airplane object

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not after takeoff

// Creating a method land() the prototype property of the class Airplane
Airplane.prototype.land = function () {
this.isflying = false;
};

airplane.land(); // calling the .land() method using the airplane object.

console.log(airplane.ifAirplaneFlying()); // this will print in the console if airplane is
flying or not after landing.










3) 

Comments

Popular posts from this blog

Object.getOwnPropertyDescriptor & Object.defineProperty Methods.

Scope and Closures / Callback Hell

DOM - Document Object Model (document.getElementById etc.)