JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<h1>Hello world!</h1>
<body>
<script src="index.js">
</script>
</body>
</html>
console.log(getMax(1, 5))
function getMax(a, b) {
return (a > b) ? a : b;
}
=====================================================
let person = {
name: 'Mosh',
age: 30
};
for (let key in person)
console.log(key, person[key])
let colors = ['red', 'green', 'blue']
for (let index in colors)
console.log(index, colors[index])
console.log(5 + 5 + '5') //answer 105 ,type string
-------------------------------------------------
const output = fizzBuzz(7)
console.log(output)
function fizzBuzz(input) {
if (typeof input !== 'number')
return 'Not a number';
let strout = '';
if (input % 3 === 0) strout = 'Fizz';
if (input % 5 === 0) strout = 'Buzz';
if ((input % 3 === 0) && (input % 5 === 0)) return 'FizzBuzz';
// console.log(strout);
if (strout !== '') return strout;
else
return input;
Math.ceil
}
-------------------------------------------------
checkSpeed(180)
function checkSpeed(speed) {
speedlimit = 70;
const kmPerPoint = 5;
if (speed < speedlimit + kmPerPoint) {
console.log('Ok');
return;
}
const points = Math.floor((speed - speedlimit) / kmPerPoint);
if (points >= 12)
console.log('Licence suspended');
else
console.log('Points', points);
}
-------------------------------------------------
showNumbers(10)
function showNumbers(limit) {
for (let i = 0; i <= limit; i++) {
if (i % 2 !== 0) console.log(i + ' ' + '"ODD"');
else console.log(i + ' ' + '"EVEN"');
}
}
=====================================================
const array = [0, null, undefined, '', 2, 4, 7, 4];
countTruthy(array);
function countTruthy(array) {
let count = 0;
for (let item of array) {
if (item) count++;
}
console.log(count);
}
=======================================================
const movie = {
title: 'a',
releaseYear: 2018,
rating: 4.5,
director: 'b'
};
showProperties(movie);
function showProperties(obj) {
for (let key in obj) {
if (typeof obj[key] === 'string') {
console.log(key, obj[key]);
}
}
}
=======================================================
console.log(sum(10));
function sum(limit) {
let s = 0;
for (let i = 0; i <= limit; i++) {
if (i % 3 === 0) s += i;
if (i % 5 === 0) s += i;
}
return s;
}
==========================================================
const marks = [90, 100, 50];
console.log(calculateGrade(marks))
function calculateGrade(marks) {
let sum = 0;
let avg = 0;
for (let mark of marks) {
sum += mark;
}
avg = sum / marks.length;
console.log('avg=' + avg)
if (avg < 60) return 'F';
if (avg < 70) return 'D';
if (avg < 80) return 'C';
if (avg < 90) return 'B';
}
==============================================================
showStars(10);
function showStars(rows) {
for (let i = 1; i <= rows; i++) {
let pattern = '';
for (let j = 0; j < i; j++)
pattern += '*'
console.log(pattern)
}
}
=================================================================
showPrimes(50)
function showPrimes(limit) {
for (let i = 1; i <= limit; i++) {
let count = 0;
for (let j = 1; j <= i; j++) {
if (i % j === 0) count++;
}
if (count === 2) console.log(i);
}
}
===================================================================
const address = {
street: "pknm",
city: "PTA",
zipCode: "689645"
};
function showAddress(address) {
for (let key in address)
console.log(key, address[key])
}
showAddress(address);
=====================================================================
let address = new Address('a', 'b', 'c');
for (let key in address)
console.log(key, address[key])
//console.log(address);
console.log(address.showName('Jobin'))
//Constructor Function
function Address(street, city, zipCode) {
this.street = street;
this.city = city;
this.zipCode = zipCode;
this.showName = function(name) {
return name + " Jose";
}
}
=============================================
Constructor Function
function Address(street, city, zipCode) {
this.street = street;
this.city = city;
this.zipCode = zipCode;
this.showName = function(name) {
return name + " Jose";
}
}
let address1 = new Address('a', 'b', 'c');
let address2 = new Address('a', 'b', 'c');
let address3 = address1;
console.log(areEqual(address1, address2));
console.log(areSame(address1, address2));
console.log(areSame(address1, address3));
function areEqual(address1, address2) {
if (address1.street === address2.street &&
address1.city === address2.city &&
address1.zipCode === address2.zipCode
)
return true;
else return false;
}
function areSame(address1, address2) {
return address1 === address2;
}
function areSame(address1, address3) {
return address1 === address3;
}
====================================================
let post = {}
post = {
title: 'a',
body: 'b',
author: 'c',
views: 10,
comments: [
{ author: 'a', body: 'b' },
{ author: 'c', body: 'd' },
],
isLive: true
};
function Post(title,body,author){
this.title=title;
this.body=body;
this.author=author;
this.views=0;
this.comments=[];
this.islive=false;
}
console.log(post);
============================================================
section 6 arrays
===================================
const numbers = [3, 4];
numbers.push(5, 6);
numbers.unshift(1, 2);
numbers.splice(2, 0, 'a', 'b')
let outp = numbers.indexOf('a');
console.log(out);
===================================
const numbers2 = [1, 2, 3, 1, 4];
console.log(numbers2.indexOf('1'));
console.log(numbers2.lastIndexOf(1));
console.log(numbers2.includes(1));
console.log(numbers2.indexOf(1, 2));
====================================
var array1 = [5, 12, 8, 130, 44]
var found = array1.find(function(element) {
return element > 10;
});
console.log(found);
const courses = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
];
const course = courses.find(course => course.name === 'a');
console.log(course);
========================================
const numbers = [1, 2, 3, 4];
const last = numbers.pop();
console.log(numbers);
console.log(last);
===========================================
const numbers = [1, 2, 3, 4];
console.log(numbers);
const last = numbers.pop();
console.log('pop()');
console.log(numbers);
console.log('shift()');
const first = numbers.shift();
console.log(numbers);
console.log('first num =' + first);
console.log('last num =' + last);
===============================================
const numbers = [1, 2, 3, 4];
console.log(numbers);
console.log('splice(2,2)');
const splice = numbers.splice(2, 2);
console.log(numbers);
==============================================
const first = [1, 2, 3];
const second = [4, 5, 6];
const combined = first.concat(second)
const combined = [...first, ...second];
const combined = [...first, 'a', ...second, 'b']
console.log(combined);
==============================================
const numbers = [1, 2, 3];
numbers.forEach(function(number) {
console.log(number);
});
numbers.forEach(number => console.log(number));
=================================================
Sort function
//============================
const courses=[
{id:1,name:Node.js}
]
================================
exercise 84
================================
const nums = arrayFromRange(10, 19)
console.log(nums);
function arrayFromRange(min, max) {
let intarray = [];
for (let i = min; i <= max; i++) {
intarray.push(i);
}
return intarray;
}
=======================================
exercise 85
=======================================
const numbers = [1, 2, 3, 4, 'a', '7'];
console.log(includes(numbers, 2))
function includes(array, searchElement) {
for (let item of array) {
if (item === searchElement)
return true;
}
return false;
}
========================================
exec 86
=================
const numbers = [1, 2, 3, 4, 1, 1];
const output = except(numbers, [1, 2]);
console.log(output);
function except(array, excluded) {
let retarray = [];
for (let number of array) {
let include = true;
for (let exnum of excluded) {
if (number === exnum) include = false;
}
if (include) retarray.push(number);
}
return retarray;
}
==============================================
const numbers = [1, 2, 3, 4];
const output = move(numbers, 1, 2);
console.log('output=' + output);
function move(array, index, offset) {
const position = index + offset;
if (position >= array.length || position < 0) {
console.error('invalid offset');
return;
}
const output = [...array];
const element = output.splice(index, 1)[0];
console.log("element=" + element);
output.splice(position, 0, element)
return output;
}
==============================================
factory fn
======================
function createCircle(radius) {
return {
radius,
draw: function() {
console.log('draw');
}
};
}
========================
OOP es6
========================
class Circle {
constructor(radius) {
this.radius = radius;
this.move = function() {
console.log('Move')
}
}
}
const c = new Circle(1);
c.move();
console.log(c.radius);
======================================
const numbers = [1, 2, 3, 4, 1, 4, 1]
const count = countOccurrences(numbers, 4);
console.log(count);
function countOccurrences(array, searchElement) {
let count = 0
for (let num of numbers) {
if (num === searchElement) {
count++;
}
}
return count;
}
===========================================
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(getMax(numbers))
function getMax(array) {
if (numbers.length === 0) return undefined;
array.reduce((accumulator, current) => {
if (current > accumulator) return current;
return accumulator;
});
}
==============================================
console.log(sum([1, 2, 3, 4, 5]));
function sum(...items) {
if (items.length === 1 && Array.isArray(items[0]))
items = [...items[0]];
return items.reduce((a, b) => a + b);
}
==============================================
const circle = {
radius: 1,
get area() {
return Math.PI * this.radius * this.radius;
}
};
console.log(circle.area);