Related
What i need to do - simple task - I am looping the array. When array[i] === 0;
i want to +1 to start, it will be the same for array[i] === 1.
But when array[i] will go back (be back where it was a moment ago) i don't want to +1 to start, and i did that code :
var start = 0;
var array = [0,1,0,0,1,0,0,1];
for (i=0; i < array.length; i ++) {
if (array[i] === 0) {
start += 1;
if (array[i + 1] === 1) {
start -= 1;
}
}
else if (array[i] === 1) {
start += 1;
if (array[i + 1] === 0) {
start -= 1;
}
}
}
console.log(start)
But what if array = [0,1,1,1,0,0,0,1,0,0,0,1]; , if array[i] will go back where it was a long time ago? I dont want then +1 to start.
Imagine 0 and 1 like -1 and +1 on chart, so console.log(start) from array = [0,1,1,1,0,0,0,1,0,0,0,1] should be 5.
My code is some kind of hard coding, have you any ideas how to fix that?
Thanks for answers in advance!
Example from comments
[0,1,1,1,0,0,0,1,0,0,0,1] should produce a result of 5; you need to imagine 0 == -1 and 1 == +1.
Then the array looks like [-1,+1,+1,+1,-1,-1,-1,+1,-1,-1,-1,+1] and begins this from 0 we have 5 positions where i was only 1 time.
As it looks like, you need to count the changes of the values in the array. You need to store the last value for checking the next value. If the last value and the actual value is different, you got a change and need to count.
function getChanges(array) {
var count = 0,
last = array[0],
i;
for (i = 1; i < array.length; i++) {
if (last !== array[i]) {
count++;
last = array[i];
}
}
return count;
}
console.log(getChanges([0, 1, 0, 0, 1, 0, 0, 1]));
console.log(getChanges([0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]));
As #nurdyguy has pointed out, your call for array[i+1] will be out of bounds for the last element. I have flipped the comparison to the previous element and started the loop on the second element to counter this.
You seem to be calculating +=1 regardless of the binary result then only -=1 when the next element in the array differs so essentially i think you have the opposite of what you are looking for :
var start = 0;
var array = [0,1,0,0,1,0,0,1];
for (i=1; i < array.length; i++) {
if (array[i] === array[i-1]) {
start++;
}
}
Now if you want to calculate the times the element changes from each step then you can just flip the logical:
var start = 0;
var array = [0,1,0,0,1,0,0,1];
for (i=1; i < array.length; i++) {
if (array[i] !== array[i-1]) {
start++;
}
}
console.log("The result is: " + start) // The result is: 5
I think this is what you are after, please elaborate if not.
Edit:
Apologies, I did test your other array and the result was also 5.
What you are looking for is this then:
var start = 0;
var array = [0,1,1,1,0,0,0,1,0,0,0,1];
for (i=0; i < array.length; i++) {
start += array[i];
}
console.log("The result is: " + start) // The result is: 5
If the array of 0 and 1 is only as an example then use a logical:
var start = 0;
var array = [0,1,1,1,0,0,0,1,0,0,0,1];
for (i=0; i < array.length; i++) {
array[i] == 1 ? ++start : +-start;
}
console.log("The result is: " + start) // The result is: 5
or:
var start = 0;
var array = [0,1,1,1,0,0,0,1,0,0,0,1];
for (i=0; i < array.length; i++) {
if (array[i] == 1) {
++start
}
}
console.log("The result is: " + start) // The result is: 5
If the array becomes [-1,1,1,1,-1,-1,-1,1,-1,-1,-1,1] like you said then the result is not 5, it's -3?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array of numbers and a value. I'd like to find the numbers around the value. The most important is the highest value but I need both.
var array = [2,4,5,8,11];
var value = 9;
The result I need should be : 8 and 11.
How can I do that?
Thanks.
Just write a simple for-loop searching for each of the values.
So for example:
function findNumbers(arr, num){
var low = num + 1; //largest number lower than num, set initial value above num
var high = num - 1; //smallest number higher than num, set initial value below num
for(var i = 0; i < arr.length; i ++){
if(arr[i] > low && arr[i] < num){
low = arr[i];
}
if(arr[i] < high && arr[i] > num){
high = arr[i];
}
}
//optional check to see if low/high exists
if(low > num){
low = -1; //not found
}
if(high < num){
high = -1; //not found
}
return [low,high];
}
and there you go. This works whether or not arr is sorted.
This help you :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var array = [2,4,5,8,11];
var value = 9;
var min,max;
fun();
function fun() {
var sortArray = array.sort(function(a,b){return a-b});
for(var i=0; i<sortArray.length;i++) {
if (sortArray[i] < value)
min = sortArray[i];
if (sortArray[i] > value)
max = sortArray[i];
}
alert(min + " , " + max);
}
</script>
</body>
</html>
Looking at your question looks like you are still learning jquery.However I am sure you will get good suggestions here.
Here is an example with your test case,I am leaving the explanation on you so that you can study each line and learn.
var myArray = [2, 4, 5, 8, 11];
var myValue = 9;
function BiggerThan(inArray) {
return inArray > myValue;
}
function LesserThan(inArray) {
return inArray < myValue;
}
var arrBiggerElements = myArray.filter(BiggerThan);
var nextElement = Math.min.apply(null, arrBiggerElements);
alert(nextElement);
var arrLesserElements = myArray.filter(LesserThan);
var prevElement = Math.max.apply(null, arrLesserElements);
alert(prevElement);
I am new to Javascript, I am doing a coding challenge to learn more about the language. This is not school related or anything like that, totally for my own personal growth. Here is the challenge:
Return the sum of all odd Fibonacci numbers up to and including the
passed number if it is a Fibonacci number.
I have spent the past 2 evenings working on solving this challenge. When I run my code using underscore.js it works. When I use Ramda.js it says NaN. I would think both would return NaN. I'm very surprised that I can get the correct answer from one and not the other. Any insights would be greatly appreciated!
var R = require('ramda');
function sumFibs(num) {
var fib_Arr = [];
var new_Arr = [];
var total = 0;
// I use this to tell if the fib num is greater than 2
var the_Bit = "false";
// This is used to keep track of when to stop the loop
var fib_Num = 0;
// THIS WORKS FROM HERE
// This loop generates a list of fibonacci numbers then pushes them to the fib_Arr
for(var i = 0; total < num; i++){
if (i < 1){
fib_Arr.push(0);
}
else if (i === 1){
fib_Arr.push(i);
fib_Arr.push(1);
}
else if (i === 2){
fib_Arr.push(2);
the_Bit = "true";
}
else if (the_Bit === "true"){
temp_Arr = R.last(fib_Arr,2);
temp_Arr = temp_Arr[0] + temp_Arr[1];
fib_Arr.push(temp_Arr);
total = R.last(fib_Arr);
}
// Generating the fib Array works TO HERE!!!!
}
// console.log(fib_Arr); // Print out the generated fibonacci array
// if last Array element is greater than the original in
var last_Element = R.last(fib_Arr);
if (last_Element > num){
console.log("The last element of the array is bigger!");
fib_Arr.splice(-1,1); // This removes the last item from the array if it is larger than the original num input
}
// This loop removes all of the EVEN fibonacci numbers and leaves all of the ODD numbers
for (var j = 0; j < fib_Arr.length; j++){
if (fib_Arr[j] % 2 !== 0){
new_Arr.push((fib_Arr[j]));
}
}
// This checks if the original input num was a
if (num % 2 !== 0){
new_Arr.push(num);
}
else{
console.log("The original num was not a Fibonacci number!");
}
// if last Array element is the same as the original input num
var last = R.last(fib_Arr);
if (last === num){
console.log("Removing the last element of the array!");
new_Arr.splice(-1,1); // This removes the last item from the array if it is the same as the original num input
}
// Now to add all of the numbers up :-)
for (var k = 0; k < new_Arr.length; k++){
console.log("This is fib_Num: " + fib_Num);
// console.log(fib_N`);
fib_Num = fib_Num += new_Arr[k];
}
return fib_Num;
}
// TEST CASES:
// console.log(sumFibs(75025)); //.to.equal(135721);
console.log(sumFibs(75024)); //.to.equal(60696);
You have a problem on these lines :
temp_Arr = R.last(fib_Arr,2);
temp_Arr = temp_Arr[0] + temp_Arr[1];
Besides the fact that R.last does not take a second argument (that will not fail though), you are using temp_arr as an array, when it is a number. Therefore, temp_arr gets a NaN value.
You are probably looking for R.take (combined with R.reverse) or R.slice.
By changing :
temp_Arr = R.last(fib_Arr,2);
with :
temp_Arr = R.take(2, R.reverse(fib_Arr));
or with :
temp_Arr = R.slice(fib_Arr.length - 2, fib_Arr.length)(fib_Arr);
or with (bonus play with a reduce from the right) :
temp_Arr = R.reduceRight(function(arr, elem) {
return arr.length < 2 ? [elem].concat(arr) : arr;
}, [])(fib_Arr);
We get :
sumFibs(75024) === 60696
For the record, here's how you do this problem:
function fibSumTo(n) {
var f1 = 1, f2 = 1, sum = 1, t;
while (f2 <= n) {
if (f2 & 1) sum += f2;
t = f1 + f2;
f1 = f2;
f2 = t;
}
return sum;
}
There's really no need for any sort of library because there's really no need for any sort of data structure.
var _ = require('underscore');function sumUpFibs (number){
arr_of_fibs = [1,1];
current = 1; //cursor for previous location
while (true){
var num = arr_of_fibs[current] + arr_of_fibs[current - 1];
if (num <= number) {
arr_of_fibs.push(num);
current++;
} else {
break;
}
}
console.log(arr_of_fibs);
var total = 0;
_.each(arr_of_fibs, function(fib){
total += fib;
})
return total;}console.log(sumUpFibs(75025));
This may be a better implementation... Though I know you're just starting so I don't want to come off as mean : D.... Also, maybe check your test cases too.
I need to go through an array from the middle outwords.
var array = [a,b,c,d,e];
I would need to print in this order:
c,d,b,e,a
I have already split the array in half, first going forward and then going backwards which is already an improvement, but I really need to go one on each side till the end of the array on each side.
Say I want to start in the middle. I have the following before the loop statement, condition, and I can't seem to figure out the third part to switch one on each side incrementally.
for (var i = Math.floor(array.length/2); i >= 0 || i < array.length; i?){
//Do Something here.
}
Does anyone know how to do this?
Obviously I can't seem to test this in this condition.
Thanks
I modified the answer below (Thanks so much) to come up with this function. It allows to start from anywhere in the array and choose the direction to go. I am sure it could be written more elegantly. There is also a safety for wrong index numbers.
var array = ["a", "b", "c", "d", "e"];
function processArrayMiddleOut(array, startIndex, direction){
if (startIndex < 0){
startIndex = 0;
}
else if ( startIndex > array.length){
startIndex = array.lenght-1;
};
var newArray = [];
var i = startIndex;
if (direction === 'right'){
var j = i +1;
while (j < array.length || i >= 0 ){
if (i >= 0) newArray.push(array[i]);
if (j < array.length) newArray.push(array[j]);
i--;
j++;
};
}
else if(direction === 'left'){
var j = i - 1;
while (j >= 0 || i < array.length ){
if (i < array.length) newArray.push(array[i]);
if (j >= 0) newArray.push(array[j]);
i++;
j--;
};
};
return newArray;
}
var result = processArrayMiddleOut(array, 2, 'left');
alert(result.toString());
http://jsfiddle.net/amigoni/cqCuZ/
Two counters, one going up, other going down:
var array = ["a", "b", "c", "d", "e"];
var newArray = [];
var i = Math.ceil(array.length/2);
var j = i - 1;
while (j >= 0)
{
newArray.push(array[j--]);
if (i < array.length) newArray.push(array[i++]);
}
http://jsfiddle.net/X9cQL/
So I decided to revisit this, not feeling very satisfied with the first answer I gave. I was positive there would be some relationship between the index numbers when the data is successfully reordered; I found that pattern in the addition of the iteration number to the last item position.
For our initial array, we'll use the following: ['a', 'b', 'c', 'd', 'e'].
Our starting point is Math.floor( arr.length / 2 ), which gives us 2, corresponding to c in the array values. This is on iteration 0. The following instructions detail how we walk through an array with an odd number of values:
Position | Direction | Iteration | New Position | Value at Position
----------+-----------+-----------+--------------+-------------------
2 | - | 0 | 2 | c
2 | + | 1 | 3 | d
3 | - | 2 | 1 | b
1 | + | 3 | 4 | e
4 | - | 4 | 0 | a
You'll see a pattern developing, when our iteration is odd we add it to our location to find our new position. When the iteration is negative, we subtract it from our position to find the new location.
When dealing with an array that has an even number of values, the rules are flipped. When you have an even number of values, we subtract odd iterations from location to get the new position, and add even iterations to our location to find the next value.
To demonstrate how little code is needed to perform this sorting logic, below is a minified version of the above logic (the aforementioned link is far more readable):
// DON'T USE THIS IN PRODUCTION, OR YOUR TEAM MAY KILL YOU
function gut(a){
var o=[],s=a.length,l=Math.floor(s/2),c;
for(c=0;c<s;c++)o.push(a[l+=(s%2?c%2?+c:-c:c%2?-c:+c)]);
return o
}
Implementing the above logic in a more readable manner:
// Sort array from inside-out [a,b,c,d,e] -> [c,d,b,e,a]
function gut( arr ) {
// Resulting array, Counting variable, Number of items, initial Location
var out = [], cnt,
num = arr.length,
loc = Math.floor( num / 2 );
// Cycle through as many times as the array is long
for ( cnt = 0; cnt < num; cnt++ )
// Protecting our cnt variable
(function(){
// If our array has an odd number of entries
if ( num % 2 ) {
// If on an odd iteration
if ( cnt % 2 ) {
// Move location forward
loc = loc + (+cnt);
} else {
// Move location backwards
loc = loc + (-cnt);
}
// Our array has an even number of entries
} else {
// If on an odd iteration
if ( cnt % 2 ) {
// Move location backwards
loc = loc + (-cnt);
} else {
// Move location forwards
loc = loc + (+cnt);
}
}
// Push val at location to new array
out.push( arr[ loc ] );
})()
// Return new array
return out;
}
Okay, let's solve this problem step by step:
An array may either have an odd or an even number of elements:
If the array has an odd number of elements:
The middle element is at index (array.length - 1) / 2. Let this index be called mid.
There are mid number of elements to the left of the middle element. Obviously.
There are mid number of elements to the right of the middle element.
If the array has an even number of elements:
The middle element is at index array.length / 2. Let this index be called mid.
There are mid number of elements to the left of the middle element. Obviously.
There are mid - 1 number of elements to the right of the middle element.
Now let's create a function to tackle this problem using the above known data:
function processMidOut(array, callback) {
var length = array.length;
var odd = length % 2; // odd is 0 for an even number, 1 for odd
var mid = (length - odd) / 2; // succinct, isn't it?
callback(array[mid]); // process the middle element first
for (var i = 1; i <= mid; i++) { // process mid number of elements
if (odd || i < mid) // process one less element if even
callback(array[mid + i]); // process the right side element first
callback(array[mid - i]); // process the left side element next
}
}
That's all that there is to it. Now let's create some arrays and process them mid out:
var odd = ["a", "b", "c", "d", "e"];
var even = ["a", "b", "c", "d", "e", "f"];
var oddOrder = "";
var evenOrder = "";
processMidOut(odd, function (element) {
oddOrder += element;
});
processMidOut(even, function (element) {
evenOrder += element;
});
alert(oddOrder);
alert(evenOrder);
You can find a working demo here: http://jsfiddle.net/xy267/1/
Very interesting algorithm. Here is what I came with:
walkMidleOut = function(arr, callback) {
var mid = (arr.length - arr.length % 2) / 2;
for (var i = 0; i < arr.length; i++) {
var s = -1,
j = (i % 2 ? (s = 1, i + 1) : i) / 2,
index = mid + s * j == arr.length ? 0 : mid + s * j;
callback.call(arr, arr[index], index);
}
}
Usage:
walkMidleOut([1,2,3,4,5], function(el, index) {
console.log(el, index);
});
Will give you:
3 2
4 3
2 1
5 4
1 0
Function can be used with any number of elements, odd or even.
How about using concat() and slice()? You can just pass this the index of the middle element.
Array.prototype.eachFrom = function(index){
var index = index > this.length ? 0 : index;
return [].concat(this.slice(index), this.slice(0, index));
}
so for example:
var arr = ['a', 'b', 'c', 'd', 'e'], arr = arr.eachFrom(2);
for( var i = 0; i < arr.length; i++ ) { doFunThings(); }
Using underscore and _( Object ).Sort_Inside_Out():
_.mixin( {
Sort_Inside_Out: function ( Object ) {
Counter = 0
return (
_( Object ).sortBy( function ( Element ) {
Counter =
-Counter + (
( Math.sign( Counter ) == 1 ) ?
0 :
1 )
return ( Counter )
} ) )
},
} )
Here is a simple way to start at any index in an array and loop both forward and backward at the same time (i.e., iterate through all the items starting with items closest to the index and moving farther away).
let passing = 0;
function bothSides(arr, idx) {
newArr = [];
const shortLen = Math.min(idx, arr.length - idx);
for (let i = 0; i < shortLen; i++) {
newArr.push(arr[idx + i]); // add next
newArr.push(arr[idx - i - 1]); // add previous
}
for (let i = idx + shortLen; i < arr.length; i++) {
newArr.push(arr[i]); // add any remaining on right
}
for (let i = idx - shortLen - 1; i > -1; i--) {
newArr.push(arr[i]); // add any remaining on left
}
return newArr;
}
var arr = [...Array(10).keys()]; // 0,1,2,3,4,5,6,7,8,9
passing += bothSides(arr, 0) == '0,1,2,3,4,5,6,7,8,9' ? 1 : 0;
passing += bothSides(arr, 2) == '2,1,3,0,4,5,6,7,8,9' ? 1 : 0;
passing += bothSides(arr, 4) == '4,3,5,2,6,1,7,0,8,9' ? 1 : 0;
passing += bothSides(arr, 5) == '5,4,6,3,7,2,8,1,9,0' ? 1 : 0;
passing += bothSides(arr, 7) == '7,6,8,5,9,4,3,2,1,0' ? 1 : 0;
passing += bothSides(arr, 9) == '9,8,7,6,5,4,3,2,1,0' ? 1 : 0;
// same algorigthm but as generator
function* bothSidesG(arr, idx) {
const shortLen = Math.min(idx, arr.length - idx);
for (let i = 0; i < shortLen; i++) {
yield arr[idx + i]; // add next
yield arr[idx - i - 1]; // add previous
}
for (let i = idx + shortLen; i < arr.length; i++) {
yield arr[i]; // add any remaining on right
}
for (let i = idx - shortLen - 1; i > -1; i--) {
yield arr[i]; // add any remaining on left
}
}
var arr2 = [...Array(7).keys()]; // 0,1,2,3,4,5,6
passing += [...bothSidesG(arr2, 0)] == '0,1,2,3,4,5,6' ? 1 : 0;
passing += [...bothSidesG(arr2, 1)] == '1,0,2,3,4,5,6' ? 1 : 0;
passing += [...bothSidesG(arr2, 3)] == '3,2,4,1,5,0,6' ? 1 : 0;
passing += [...bothSidesG(arr2, 5)] == '5,4,6,3,2,1,0' ? 1 : 0;
passing += [...bothSidesG(arr2, 6)] == '6,5,4,3,2,1,0' ? 1 : 0;
console.log(`Passing ${passing} of 11 tests`);
I've a simple FOR statement like this:
var num = 10,
reverse = false;
for(i=0;i<num;i++){
console.log(i);
}
when reverse is false I want it to return something like [0,1,2,3,4,5,6,7,8,9]
but, when reverse is true, it should return [9,8,7,6,5,4,3,2,1,0]
Which is the most efficient way to get this result, without checking every time if reverse is true or false inside the loop?
I don't want to do this:
var num = 10,
reverse = false;
for(i=0;i<num;i++){
if(reverse) console.log(num-i)
else console.log(i)
}
I would like to check reverse only one time outside the loop.
var num = 10,
reverse = false;
if(!reverse) for( var i=0;i<num;i++) log(i);
else while(num-- ) log(num);
// to avoid duplication if the code gets long
function log( num ) { console.log( num ); }
EDIT:
As noted in the comments below, if i is not declared elsewhere and you do not intend for it to be global, then declare it with the other variables you declared.
And if you don't want to modify the value of num, then assign it to i first.
var num = 10,
reverse = false,
i;
if(!reverse) for(var i=0;i<num;i++) log(i); // Count up
else {var i=num; while(i--) log(i);} // Count down
function log( num ) { console.log( num ); }
Try use 2 loops:
if (reverse) {
for(i=num-1;i>=0;i--){
console.log(i)
}
}
else {
for(i=0;i<num;i++){
console.log(i)
}
}
var num = 10,
reverse = false;
for (var i = 0, n = reverse?num-1:0, d = reverse?-1:1; i < num; i++, n+=d) {
console.log(n);
}
This is equivalent to the following, which is more readable, but less compact:
var num = 10,
reverse = false;
var start = reverse ? num-1 : 0,
end = reverse ? -1 : num,
step = reverse ? -1 : 1;
for (var i = start; i != end; i += step) {
console.log(i);
}
Edit:
Actually, these two solutions are not identical, because the first one has an additional increment operation. Still, it is negligible from performance point of view. If you really want to get a compact solution that has the best performance, you can do the following (not for the faint of heart):
var num = 10,
reverse = false;
for (var r=reverse, i=r?num-1:0, n=r?-1:num, d=r?-1:1; i!=n; i+=d) {
console.log(i);
}
This has the advantage of having a single control structure, a single test in each loop, and a single iterator addition. It is not as fast as having an iterator increment/decrement, but only marginally so.
var start; var end; var inc;
if (reverse) {
start = num-1; end = 0; inc = -1;
}
else {
start = 0; end = num-1; inc = 1;
}
for(i=start;i!=end;i+=inc){
console.log(i)
}
I just came across the need for this the other day. Here's how I did it:
var num = 10,
i = 0,
direction = 1,
reverse = false;
if(reverse)
i = num + (direction = num = -1);
for(; i !== num; i += direction) {
console.log(i);
}
No need for separate loops, and no need to do math to calculate the proper i in the loop.
So if reverse is true...
i (which represents our first item) becomes num - 1, so we're now starting on what would have been the last item
num (which represents out of bounds) becomes -1, so we're now stopping on what would have been the first item
direction is -1, which means it will decrement when we do i += direction
So by swapping our starting point with our ending point and changing the alteration of i from 1 to -1, we'll be going up or down based on those modifications.
I think this meets your requirements:
var num = 10;
var reverse = false;
var diff = 0;
if (reverse) {
diff = num - 1;
}
for (i = 0; i < num; i++) {
console.log(Math.abs(diff - i));
}
Here's how I've always done reverse loops:
for (i = num; --i >= 0; ) ...
And what's your problem with:
if (reverse)
{
for(i=num-1; i>=0;i--){
console.log(i);
}
}
else
{
for(i=0;i<num;i++){
console.log(i)
}
}
}
Roy's is similar to mine, but here's what I was thinking. I'll give you what I wrote in C# and how I think it translates to Javascript.
C#
int num = 10;
bool reverse = true;
for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
{
Console.Write((reverse ? i - 1 : i).ToString());
}
Console.ReadKey();
Javascript
var num = 10,
reverse = true;
for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
{
console.log(reverse ? i - 1 : i);
}
And here's another way
Javascript
var num = 10,
reverse = false;
for (int i = 0; i < num; i++)
{
console.log((reverse ? abs(-num + (i + 1)) : i));
}
It seems to work:
var num = 10;
var z = 1;
var k = -10;
if (reverse ){
k = -1;
z = -1;
}
for(int i = 0; i < 10; i++){
console.log(num+i*z+k);
}
Surely in a language like Javascript there must be a way to define a local function and use that in the loop?
function SubtractFrom(val, subtractor) {
return val - subtractor;
}
function PassThrough(val) {
return val;
}
var num = 10;
var processor = reverse ? SubtractFrom(num-1) : PassThrough;
for (i = 0; i < num; i++) {
console.log(processor(i));
}
Not knowing Javascript though, I don't know what actual form the function definitions would take.
//reverse a number
let c = [2,3,5,67,4]
let sum = '';
let d = c.toString().split('')
console.log(d)
for(let i = d.length-1; i >= 0; i--) {
sum += d[i]
}
console.log(sum)