Hei Coders,
I have a string "1+1", with javascript build in function eval() i can do eval("1+1") so the return value will be 2
But how about if i want to implement this concept to recursive function in javascript?
function evaluate(str) {
}
evaluate("1+1");
evaluate("1-1");
evaluate("1*1");
evaluate("1/1");
What i've tried are
function evaluate(str) {
if (str.length === 0) {
return "";
}else{
let angka;
let symbol;
for (let i = 0; i < str.length; i++) {
if (isNaN(str[i])) {
symbol = str[i];
break;
}else{
angka = str[i];
}
}
switch (symbol) {
case "+":
return angka + evaluate(str.slice(1));
case "-":
return angka - evaluate(str.slice(1));
case "/":
return angka / evaluate(str.slice(1));
case "*":
return angka * evaluate(str.slice(1));
default:
return parseInt(str[0]) + evaluate(str.slice(1));
}
}
}
function evaluate(str) {
if (str.length === 0) {
return ""
}
let numbers = "";
let operator = "";
let lastIndex = 0;
for (let i = 0; i <= str.length; i++) {
if (!isNaN(parseInt(str[i]))) {
numbers += parseInt(str[i]);
}else{
operator = str[i];
lastIndex = i;
break;
}
}
// console.log(numbers, " " , operator , " " , lastIndex);
lastIndex = lastIndex < 1 ? 1 : lastIndex;
if (operator === "+") {
return numbers + evaluate(str.slice(lastIndex));
}
}
function evaluate(str) {
if (str.length === 0) {
return 1;
}else{
let numbers = "";
for (let i = 0; i <= str.length; i++) {
if(parseInt(str[i]) >= 0){
numbers = numbers + "+" + str[i];
}else{
let lengthNumbers = numbers.length > 1 ? numbers.length : 1;
let tempNumbers = numbers;
numbers = "";
return tempNumbers + evaluate(str.slice(lengthNumbers))
}
}
}
}
===========
How nub i am :), for now this is my answer (according to the solutions below), Thanks for all
function evaluate(str) {
if(str.match(/[*/+-]/)){
let numbers = "";
for (let i = 0; i < str.length; i++) {
switch (str[i]) {
case "+":
return parseInt(numbers) + evaluate(str.slice(numbers.length+1))
case "*":
return parseInt(numbers) * evaluate(str.slice(numbers.length+1))
case "/":
return parseInt(numbers) / evaluate(str.slice(numbers.length+1))
case "-":
return parseInt(numbers) - evaluate(str.slice(numbers.length+1))
default:
numbers += str[i];
break;
}
}
}else{
return parseInt(str[0]);
}
}
console.log(evaluate('1+2+3+4+5')) // 15
console.log(evaluate('1*2*3*4*5')) // 120
console.log(evaluate('20/4')) // 5
console.log(evaluate('20-6')) // 14
And no one work! I know eval will save my day, but in this case i need to solve this case, thanks before.
Try this code
function evaluate(str) {
var reg = /[*/+-]/
if(str.match(reg)){
var temp = ''
for(let i = 0; i < str.length; i++){
if(str[i] === '+') {
return parseInt(temp) + evaluate(str.substring(i+1))
}
else if(str[i] === '-') {
return parseInt(temp) - evaluate(str.substring(i+1))
}
else if(str[i] === '*') {
return parseInt(temp) * evaluate(str.substring(i+1))
}
else if(str[i] === '/') {
return parseInt(temp) / evaluate(str.substring(i+1))
}
else {
temp += str[i]
}
}
}
else {
return parseInt(str)
}
}
console.log(evaluate('1+2+3+4+5')) // 15
console.log(evaluate('1*2*3*4*5')) // 120
console.log(evaluate('20/4')) // 5
console.log(evaluate('20-6')) // 14