Pavel 7 years ago
parent
commit
9764d2bd97
1 changed files with 23 additions and 14 deletions
  1. 23 14
      js06/js06.md

+ 23 - 14
js06/js06.md

@@ -117,10 +117,9 @@ console.log(result);
 ```javascript
 var userVar = prompt("Введите выражение");
 
+console.log(userVar);
 var result = eval(userVar);
 alert("Ответ: " + result);
-
-console.log(userVar);
 function eval(userVariableInit){
 	var userVariable = [];
 	for (var i = 0; i < userVariableInit.length; i++) {
@@ -184,6 +183,24 @@ function eval(userVariableInit){
 		if(!(i % 2))
 			array[i] = +array[i];
 	}
+	console.log(array);
+	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);
+				
+			}
+			if(array[i] === "/"){
+					var res = array[i - 1] * array[i + 1];
+					array.splice(i - 1, 3, res);
+				
+			}
+		}
+	}
+	console.log(array)
 	for (var i = 0; i < array.length; i++) {
 		if(i % 2){
 			
@@ -199,18 +216,7 @@ function eval(userVariableInit){
 				else 
 					result -= array[i + 1];
 			}
-			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);
@@ -218,5 +224,8 @@ function eval(userVariableInit){
 	
 }
 
+
 console.log(result);
+
+
 ```