From 1fd902d2745ff23ad453c4c7a101e5b95efcb783 Mon Sep 17 00:00:00 2001 From: Chetan Nada Date: Wed, 9 Aug 2023 23:10:17 +0530 Subject: [PATCH] Create 2710-remove-trailing-zeros-from-a-string.js --- .../2710-remove-trailing-zeros-from-a-string.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 javascript/2710-remove-trailing-zeros-from-a-string.js diff --git a/javascript/2710-remove-trailing-zeros-from-a-string.js b/javascript/2710-remove-trailing-zeros-from-a-string.js new file mode 100644 index 000000000..3a212df02 --- /dev/null +++ b/javascript/2710-remove-trailing-zeros-from-a-string.js @@ -0,0 +1,12 @@ +/** + * @param {string} num + * @return {string} + */ +var removeTrailingZeros = function (num) { + + for (let i = num.length - 1; i >= 0; i--) { // loop through the every element of string num from end + if (num[i] != 0) { // if every character of num is not equal to zero + return num.slice(0, i + 1); // return the character of num upto ith character + } + } +}; \ No newline at end of file