JS Practical

JS Practical

by lshettyl

HTML

>Write a pipefy function where a string received is returned, but with the | character between each character. Make it possible to execute function in this way: 'javascript'.pipefy()
> Return the longest word from the given string
> Reverse a string
> Write a closure that returns sum of two numbers.
> How can we retrieve a DOM node collection of all div elements having the class row-fluid on a website?
> Swap 2 nums with no temp var
let a = 1, b = 2;
[a, b] = [b, a];


let obj = {
	name: 'Name of the obj',
  getName () {
  	alert (this.name);
  }
};
obj.getName();
/* obj.getName.bind(document.getElementById('test')); */

function Point(x, y) {
  this.x = x;
  this.y = y;
}
Point.prototype.toString = function() { 
  return this.x + ',' + this.y; 
};
//var p = new Point(1, 2);
//p.toString(); // '1,2'


> Function that determines if a string consists of hexadecimal digits only. The digits A–F can either be in lower case or in upper case.
let checkHexNum = hexString => /^[0-9a-fA-F]+$/.test( hexString );

> Merge objects eliminating dups
var dest = { quux: 0 }
var src1 = { foo: 1, bar: 2 }
var src2 = { foo: 3, baz: 4 }
= {quux: 0, bar: 2, foo: 3, baz: 4}
Object.assign(dest, src1, src2)

> String interpolation
var message = "Hello " + customer.name + ",\n" +
"want to buy " + card.amount + " " + card.product + " for\n" +
"a total of " + (card.amount * card.unitprice) + " bucks?";

> Object def short hand
obj = { x, y }; // { x: x, y: y };

> Which line of the below code will be executed with an error? Why?
10 .toString();
(10).toString();
10..toString();

> What is the order of alerts?
setTimeout(function(){
    alert('gorilla');
    setTimeout(function(){
        alert('classical inheritance')
    }, 0);
    alert('drumroll');
}, 0);
alert('banana');

> What is the result after code execution: 1, 2 or 3?
var x = 1;
var foo = {
  x:2,
  bar: function() {
    x = 3;
    return this.x;
  }
}
var run = foo.bar;
alert(run());

What below code will return: true or false....