we can’t help debug a screenshot, above all if the code is not all visible, please post your whole code.
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
Again, post your code, not a screenshot
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
,/**/
var calPoints = function(ops) {
var result = null;
var Sum = 0;
var i;
const arr = [0];
if(Number.isInteger(ops)==true)
{
arr.push(ops);
}
else if((ops== "+") && (arr.length>=2))
{
var y = arr.length;
Sum = parseInt(arr[y])+parseInt(arr[y-1]);
arr.push(Sum);
return arr
}
else if((ops ="D") && (arr.length>=1))
{
var y = arr.length;
Sum = 2*parseInt(arr[y]);
arr.push(Sum);
return arr;
}
else if((ops="C") && (arr.length>=1))
{
arr.pop();
return arr;
}
for (i=0; i=arr. length; i++)
{
Sum = 0;
Sum = Sum + arr[i];
}
result= Sum;
return result;
};
var ops= readline().split(" ");
console.log(calPoints(ops));
input: 5 2 C d +
output : 30
5 : add 5 to the array = [5]
2 : add 2 to the array =[5,2]
C : remove last ; [5]
D: double the last element => 5 *2 => [5,10]
+ : add the last two and push sum in array => 5+10=15 , => [5,10,15]
result = 5+10+15 = 30
please debug this and share me everywhere i went wrong
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
This index is out of bounds.
isn’t ops
an array?
all your checks are against ops
, but ops
is an array, you need to check the elements inside it
Thanks I’m really new to it.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.