What is the output of these order of operations? Explain why:
console. log(2+’2’);
console. log(2-‘2’);
First line prints 22
Second line prints 0
Reason is because + sign can be applied to strings (via string concatenation) while - sign cannot.
Remember this rule in Javascript.
Remove duplicates, but do so on one line and without using any loops or lambdas:
let nums = [1,2,2,3];
let arr = [... new Set(nums)]; console.log(arr);
What are the 6 possible data types in Vanilla javascript?
Object number text null undefined boolean
What are the 3 possible prototype functions for attaching an object to a function?
call();
apply();
bind();
Explain the 3 most common Object.prototype utility methods (hint: get, has, set)
getPrototypeOf(a);
hasOwnProperty(‘propertystring’);
setPrototypeOf({
newprop : “something”
});
Show syntax for Javascript object creation:
//Regular object creation:
var x = {
a: 1,
b : 2,
c : 'stuff'
};What is the syntax for an object created with a constructor? Include a function.
function Graph() {
this.vertices = [];
this.edges = [];
} //Creates a type of object called Graph
Graph.prototype = {
addVertex : function(v) {
this.vertices.push(v);
}
}var g = Graph(); g.addVertex(5);
How do you solve Palindrome in Javascript? (Note: no loops or Lambdas are allowed.)
Followup: How do you do this on one line?
const palindrome = function(text) {
let orig = [...text];
let rev = Array.from(orig);
rev.reverse();
if(JSON.stringify(orig) === JSON.stringify(rev)) {
return true;
}
else {
return false;
}
}
console.log(palindrome('abcba'));
console.log(palindrome('something'));
//On one line: split reverse join
let text2 = 'abcba';
if(JSON.stringify(text2) === JSON.stringify(text2.split('').reverse().join(''))) {
console.log(true);
}
else {
console.log(false);
}JSON.stringify(text2) === JSON.stringify(text2.split(‘’).reverse().join(‘’)) ? console.log(true) : console.log(false);
TRICKY JAVASCRIPT CONVERSIONS 1 :
number conversions
Create a random number between 0 and 9
Create a random number between 1 to 10
Create a random numbers between 10 to 100
Create a function to create a random number between two integers a and b
//Math.random() always returns a number between 0 to 1 in Javascript. //The "trick" is that you must multiply times one number greater than the place value you are looking for
//To get a number from 0 to 9: Math.floor(Math.random() * 10);
//To get a number from 1 to 10 Math.floor(Math.random() * 10) + 1;
//To get a number from 10 to 100: Math.floor(Math.random() * 90) + 10;
//Function to get random between two values
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}TRICKY JAVASCRIPT CONVERSIONS 2:
Convert a float number .12345 to a string
Convert a string .12345 to a float number
Write a function to add ‘.0’ to the end of an integer number after converting it to text.
Write a function to pad take a number, convert it to text, and pad it to 5 0’s after the decimal point. Eg: convertAndPad(5.67) => 5.67000 in text.
Float conversions
Explain about Map and all of its functions.
new Map() – creates the map.
map. set(key, value) – stores the value by the key.
map. get(key) – returns the value by the key, undefined if key doesn’t exist in map.
map. has(key) – returns true if the key exists, false otherwise.
map. delete(key) – removes the value by the key.
map. clear() – removes everything from the map.
map. size – returns the current element count.
Explain about Set and all of its functions
A Set is a special type collection – “set of values” (without keys), where each value may occur only once.
Its main methods are:
new Set(iterable) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
set. add(value) – adds a value, returns the set itself.
set. delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set. has(value) – returns true if the value exists in the set, otherwise false.
set. clear() – removes everything from the set.
set. size – is the elements count.
Write a function to update a digital clock in the DOM, assuming there is a span element called hourEl and minuteEl
//Update clock:
const updateTime = () => {
const currentTime = new Date();
let currentHour = currentTime.getHours();
const currentMinute = currentTime.getMinutes();
if (currentHour > 12) {
currentHour -= 12;
}
hourEl.textContent = currentHour.toString();
minuteEl.textContent = currentMinute.toString().padStart(2, '0');
}
setInterval(updateTime, 1000);
updateTime();What is the difference between for-in and for-of loops in Javascript?
//for in loop:
const obj = {
a: 1,
b: 2,
c: 3,
d: 4
}
for (const key in obj) {
console.log( obj[key] )
}
// Result: 1, 2, 3, 4
//Used for iterating over objects.
//cannot be guaranteed that the iteration happens in sequence
//for of loop:
const array = ['a', 'b', 'c', 'd'];
for(let v of array) {
console.log(v);
}
const sentence = "Hello";
for(let ch of sentence) {
console.log(ch);
}
//for of introduced in ES2015
//used for iterating over "iterable collections". These are objects that have a [Symbol.iterator] property.
//general purpose iterator for array, string, set