sort
var persons = [
{name: "Иван", age: 17},
{name: "Мария", age: 35},
{name: "Алексей", age: 73},
{name: "Яков", age: 12},
]
function sort(array, key, bool){
function sortType(a,b){
if(bool === undefined)
bool = true;
if(bool){
if(a[key] > b[key])
return 1
else
return -1
}
else{
if(a[key] < b[key])
return 1
else
return -1
}
}
array.sort(sortType);
}
sort(persons, "age");
sort(persons, "name", false);
console.log(persons);
array map
var array1 = ["1", {}, null, undefined, "500", 700];
var array2 = array1.map(function(arrayItem){
if(typeof(arrayItem) === "string")
return +arrayItem;
return arrayItem;
});
console.log(array2);
array reduce
var array = ["0", 5, 3, "string", null].reduce(function(previousValue, currentValue, index, array){
if(typeof(previousValue) !== "number" )
return currentValue;
else if(typeof(currentValue) !== "number")
return previousValue;
else
return previousValue * currentValue;
});
console.log(array);
object filter
var phone = {
brand: "meizu",
model: "m2",
ram: 2,
color: "black",
};
function filter(object, validate){
var obj = {};
var keys = Object.keys(object);
for (var i = 0; i < keys.length; i++) {
if(validate(keys[i], object[keys[i]]))
obj[keys[i]] = object[keys[i]];
}
return obj;
}
var phone2 = filter(phone,function(key,value){
return (key == "color" || value == 2);
});
console.log(phone2);
object map
function map(obj, cond){
var keys = Object.keys(obj);
var newObject = {};
var resObject = {};
for (var i = 0; i < keys.length; i++) {
newObject = cond(keys[i], obj[keys[i]])
var keys2 = Object.keys(newObject);
resObject[keys2[0]] = newObject[keys2[0]];
}
return resObject;
}
var result = map({name: "Иван", age: 17},function(key,value){
var result = {};
result[key+"_"] = value + "$";
return result;
})
console.log(result);
Калькулятор
var userVar = prompt("Введите выражение");
console.log(userVar);
var result = eval(userVar);
alert("Ответ: " + result);
function eval(userVariableInit){
var userVariable = [];
for (var i = 0; i < userVariableInit.length; i++) {
userVariable[i] = userVariableInit[i];
}
if(userVariable.indexOf("(") > -1)
{
var userVar = [];
do{
var k = 0;
var r = 0;
var d;
for (var i = 0; i < userVariable.length+1; i++) {
if(i != 0 && r == 0 && k > 0){
d = i-1;
break;
}
if(userVariable[i] == "("){
k++
r++;
}
if(userVariable[i] == ")"){
k++
r--;
}
}
console.log(k);
console.log(d);
userVariable.splice(userVariable.indexOf("("), 2,eval(userVariable.splice(userVariable.indexOf("(") + 1, d - userVariable.indexOf("(") - 1)));
}
while(userVariable.indexOf("(") > -1)
userVar = userVariable;
}
else
var userVar = userVariable;
var array = [];
var bool = true;
for (var i = 0, p = 0, c = 0; i < userVar.length ; i++,p++) {
switch(userVar[i]){
case "+":
array.push("+");
bool = false;
break;
case "-":
array.push("-");
bool = false;
break;
case "*":
array.push("*");
bool = false;
break;
case "/":
array.push("/");
bool = false;
break;
default:
if(i != 0 && bool)
{
p--;
c++;
array[p] = array[p] + userVar[i];
bool = true;
}
else
{
array[p] = userVar[i]
bool = true;
}
}
}
var result = 0;
for (var i = 0; i < array.length; i++) {
if(!(i % 2))
array[i] = +array[i];
}
console.log(array);
while(array.indexOf("*") > -1 || array.indexOf("/") > -1){
for (var i = 0; i < array.length; i++) {
if(i % 2){
if(array[i] === "*"){
var res = array[i - 1] * array[i + 1];
array.splice(i - 1, 3, res);
continue
}
if(array[i] === "/"){
var res = array[i - 1] / array[i + 1];
array.splice(i - 1, 3, res);
continue
}
}
}
}
console.log(array)
if(array.length === 1)
result = array[0]
for (var i = 0; i < array.length; i++) {
if(i % 2){
if(array[i] === "+"){
if(i === 1)
result += array[i - 1] + array[i + 1];
else
result += array[i + 1];
}
if(array[i] === "-"){
if(i === 1)
result = array[i - 1] - array[i + 1];
else
result -= array[i + 1];
}
}
}
console.log(array);
return result;
}
console.log(result);