Skip to content

Instantly share code, notes, and snippets.

@akaomy
Last active December 7, 2015 17:43
Show Gist options
  • Save akaomy/5a92e9902948276cda09 to your computer and use it in GitHub Desktop.
Save akaomy/5a92e9902948276cda09 to your computer and use it in GitHub Desktop.
JavaScript exercises
var a = []
if (abc instanceof Array){
console.log('true')
}
=> true
______________________________
var a = []
if (abc instanceof Number){
console.log('true')
}else{
console.log('false')
}
=> false
function test(a,b) {
console.log("sum of", a, " and ", b, " is equal to ", a + b)
}
test(20,40);
=> sum of 20 and 40 is equal to 60
________________________________
function testFunc(b,c) { // or var testFunction = function(b,c)
var sum = 0;
for(var i = 0; i < arguments.length; i ++) {
sum = sum + arguments[i];
}
console.log('sum of ', b, ' and ', c, ' is equal to ', sum)
}
testFunc(40,4)
=> sum of 40 and 4 is equal to 44
_______________________________
var arrayOfNumbers = [1,2,3,5,27,88,3,23];
function greaterThan(sourse, value) {
var result = [];
for (var i = 0; i < sourse.length; i++){
if (sourse[i] > value ) {
result.push(sourse[i]);
}
}
return result;
}
console.log(greaterThan4(arrayOfNumbers, 4));
=> [5, 27, 88, 23]
_______________________________
var arrayOfNumbers = [1,2,3,5,27,88,3,23];
function filter(sourse, filterFn) {
var result = [];
for (var i = 0; i < sourse.length; i++){
if (filterFn(sourse[i])) {
result.push(sourse[i]);
}
}
return result;
}
console.log(greaterThan4(arrayOfNumbers, 4));
=> [5, 27, 88, 23]
function greaterThan4(value){
return value > 4;
}
console.log(filter(arrayOfNumbers,greaterThan4));
=> [5, 27, 88, 23]
______________________________
var a = 1;
function f1(b){
var c = 10;
return a + b + c;
}
console.log(f1(2));
=> 13
var a = [1, '3', true, 3.4]
for (var i = 0; i < a.length; i++) {
console.log(i, a[i]);
}
=>
0 1
1 "3"
2 true
3 "add"
var s = 'Hi';
console.log(s.concat(' Anna')); /* or console.log(s + " Anna") */
=> Hi Anna
______________________________________
var s = 'Hello there';
console.log(s.indexOf('there'));
=> 6 /*6th position; if there it doesn't find anything it returns -1*/
var s = 'Hello there';
if (s.indexOf('sdjskjd') == -1 ) {
console.log('position not found');
} else {
console.log('position found');
}
_________________________
var s = 'Hello there';
var i = s.indexOf('e');
if (i == -1 ) {
console.log('position not found');
} else {
console.log('position found');
}
=> position found
_________________________
var s = '1|2|3|4|5|';
var matched = s.match(/\d\|/);*
console.log(matched);
=> ["1|", index: 0, input: "1|2|3|4|5|"]
* or s.match(/\d\|/9); then result is ["1|", "2|", "3|", "4|", "5|"]
list of methods for strings:
replace()
slice()
split()
trim()
toLowerCase()
toUpperCase()
_____________________________________
var s = 'car, box, move, place';
console.log(s.split(','));
=> ["car", " box", " move", " place"]
_____________________________________
var s = 'car, box, move, place';
console.log(s.split(','));
for(var i = 0; i < s.length; i++) {
console.log(s[i].trim());
}
=>
["car", " box", " move", " place"]
c
a
r
,
b
o
x
,
m
o
v
e
,
p
l
a
c
e
// create new object
var s = new String ('objectsstring');
=> s
String {0: "o", 1: "b", 2: "j", 3: "e", 4: "c", 5: "t", 6: "s", 7: "s", 8: "t", 9: "r", 10: "i", 11: "n", 12: "g", length: 13, [[PrimitiveValue]]: "objectsstring"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment