x = y;
x == y;
x === y;
var x = transform(5)(6);
// x = y * z
var x = transform(5)(6); // x === 30
function transform (y) {
return function (z) {
return y * z;
};
}
typeof 'Hello';
typeof Array.prototype.shift;
typeof new Date();
typeof {a:1};
typeof [1, 2, 3];
typeof null;
typeof NaN;
typeof 'Hello'; // 'string'
typeof Array.prototype.shift; // 'function'
typeof new Date(); // 'object'
typeof {a:1}; // 'object'
typeof [1, 2, 3]; // 'object'
typeof null; // 'object'
typeof NaN; // 'number'
// other typeof return values
'undefined'
'boolean'
#!/usr/bin/node
function bar () {
var a = 'cat';
if (1) {
var a = 'bunny';
b = 'dog';
}
console.log(a);
console.log(b);
}
function bang () {
console.log(b);
console.log(a);
}
bar();
bang();
#!/usr/bin/node
bar();
bang();
function bar () {
let a = 'cat';
if (1) {
let a = 'bunny';
b = 'dog';
}
console.log(a);
console.log(b);
}
function bang () {
console.log(b);
console.log(a);
}
function Dog (name) {
this.name = name;
}
Dog.prototype.speak = function speak () {
console.log(`Woof. My name is ${this.name}`);
};
var rex = Dog('Rex');
var Dog = function (name) {
return {
name,
speak: function speak () {
console.log(`Woof. My name is ${name}`);
}
};
};
var rex = Dog('Rex');
let a = (100 + 1/2) - 100;
let b = 1/2;
assert(a === b);
let x = (100 + 1/3) - 100;
let y = 1/3;
assert(x === y);
FizzBuzz
Write a function, using JavaScript syntax, that takes an array of integers as its only argument and, for each element, performs the following:
- If the number is evenly divisible by 3, print "Fizz"
- If the number is evenly divisible by 5, print "Buzz"
- If the number is evenly divisible by both 3 and 5, print "FizzBuzz"
- If none of the above, print the number.
FizzBuzz
function fizzBuzz (integers) {
integers.forEach((i) => {
let output = '';
if (i % 3 === 0) {
output += 'Fizz';
}
if (i % 5 === 0) {
output += 'Buzz';
}
console.log(output ? output : i);
});
}
FizzBuzz
#!/usr/bin/env node
'use strict';
fizzbuzz(...process.argv.slice(2));
function fizzbuzz(...numbers) {
numbers.map(n => parseInt(n, 10)).forEach(num => {
let output = '';
if (num % 3 === 0) output += 'fizz';
if (num % 5 === 0) output += 'buzz';
if (output === '') output = num;
console.log(output);
});
}
Palindrome
Write a function that takes a string as its only argument and returns a Boolean indicating whether or not the word is a palindrome.
Palindrome
// solution using just Array methods
function isPalindrome (str) {
return str.split('').reverse().join('') === str;
}
// solution using iteration and Array methods
function isPalindrome (str) {
let chars = str.split('');
while (chars.length > 1) {
if (chars.shift() !== chars.pop()) {
return false;
}
}
return true;
}
jsinterview
By Kyle Anderson
jsinterview
- 32