기본 콘텐츠로 건너뛰기

1월, 2019의 게시물 표시

JS 5.5 task6 Create an extendable calculator

<HTML>   <body>   </body>   <script> function Calculator() {   let methods = {     "-" : (a, b) => a - b,     "+" : (a, b) => a + b   };   //methods is an object which keeps key for operators   //and value to return the actual operation values   //each returns the result of operation that key(operator) does   this.calculate = function (str){     //calculate is one element in the function Calculator     //it takes the string and returns the value     //in the function element list is delimeted by , not ;     let split = str.split(" "),     a = +split[0],     op = split[1],     b = split [2]     if(!methods[op] || isNaN(a) || isNaN(b)) {       return NaN; // error handling     }     return methods[op](a,b);   }   this.addMethod = function(name, func){     methods[name] = func;     //this is how to add new key and ele to object   } } let powerCalc = new Calculator; powerCalc.addMethod("*&

JS 5.5 task4 Sort in the reverse order

<HTML> <body> </body> <Script> function compareNumeric (a,b){  if(a > b) return -1; // returning -1 means first argument ascends b  if(a == b) return 0;  if (a < b) return 1; // returning 1 means b is asceds a } let arr = [5, 2, 1, -10, 8]; arr.sort(compareNumeric); // ... your code to sort it in the reverse order alert( arr ); // 8, 5, 2, 1, -10 </Script> </HTML>

JS 5.5 task3 Filter range "in place"

<HTML>   <body>   </body>   <script> function filterRangeInPlace(arr, min, max){   arr.forEach(function(item, index, arr){   if(item >= min || item > max){     //alert(`event happened at ${index} value is ${item}`);     arr.splice(index, 1);   } }); return arr;   } let arr = [5 ,3, 8, 1]; arr = filterRangeInPlace(arr, 1,4); alert(arr);   </script> </HTML>

JS 5.5 task 1 Translate border-left-width to borderLeftWidth

<HTML>   <body>   </body>   <script> function camelize(line){   let line1 = line.split("-");   let set = "";   for(let word of line1){     if(word == line1[0]){       set = set + word;      continue;     }     alert(word);    word = word[0].toUpperCase() + word.slice(1);    set = set + word;   } return set; } alert(camelize("background-color") == 'backgroundColor'); //set.join(''); using join "," is all between the words   </script> </HTML>

JS 5.5 task 2 Filter range

<HTML>   <body>   </body>   <script> function filterRange(arr, min, max){   let result = [];   result.push(arr.filter(item => item >= min && item < max));   return result;   } let arr = [5 ,3, 8, 1]; let filtered = filterRange(arr, 1,4); alert(filtered); alert(arr);   </script> </HTML>