2

i get some trouble when i try to filtering time now in array time, i have code like this ;

  var timeNow = "07";
    var timeShift = ["08","10","12","14","16","18","20","22","00","02","04","06"];
    var newData =[];
    for(var data of timeShift){
    	 if(data >= timeNow){
     	  	newData.push(data);
        }
    }
    console.log(newData[0]); // output 08

the problem is output not same with my expectation.
i want :

if timeNow is 08, timeShift output(selected) is 08,
if timeNow is 07, timeShift output(selected) is 06,
if timeNow is 09, timeShift output(selected) is 08,
if timeNow is 23, timeShift output(selected) is 22,
. . . . . . . continued as the structural of data timeNow and timeShift like my expectation in there.

How can i fix the problem ?
Please Help me. Thank you :)

6
  • do you want only a single value? Commented Oct 24, 2018 at 17:48
  • yes, its helpful for me :) @NinaScholz Commented Oct 24, 2018 at 17:49
  • what do you mean by 'output not same with my exception'? Just want to understand your question better! Commented Oct 24, 2018 at 17:52
  • @alokstar okay, im sorry , the mean is expectation not exception, my english is bad. :) hehe Commented Oct 24, 2018 at 17:53
  • the main problem is, you have string, which represents numbers, but you do not have this relation in it. even the 24 h system is not mentioned here. Commented Oct 24, 2018 at 17:53

4 Answers 4

1

If you need to find the closest single value which is less or equal to than the given one, you need just a small update to your code:

function getTime(timeNow) {
var timeShift = ["08", "10", "12", "14", "16", "18", "20", "22", "00", "02", "04", "06"];
var newTime = '00';
for (var data of timeShift)
  if (data <= timeNow && data >= newTime)
    newTime = data;

return newTime;
}

console.log(getTime('08'));
console.log(getTime('07'));
console.log(getTime('09'));
console.log(getTime('23'));

Sign up to request clarification or add additional context in comments.

2 Comments

yes alright. Thats same with my expectation. Thanks you sir @KoshVery. its helpful for me :)
@Raffikbojjes, happy to help!
1

You could filter the values by taking the numericak value for comparing and a value decremented by one for checking.

const getN = s => parseInt(s, 10),
      check = (strong, weak) => strong === weak -1 || strong === weak;
  
var timeNow = "07",
    timeShift = ["08","10","12","14","16","18","20","22","00","02","04","06"],
    result = timeShift.filter(s => check(getN(s), getN(timeNow)));

console.log(result);

2 Comments

if timeNow i change to '09' the return index of array its not '08', but the return is '10 @NinaSchols
okay its alright. it work clearly, thanks before it :) @NinaSchols
1

Looks like you want the highest value less than or equal to now. You can accomplish this with a combination of filter (to first filter values from timeShift that are less than or equal to your target) and reduce (to find the maximum of those filtered values):

var timeShift = ["08", "10", "12", "14", "16", "18", "20", "22", "00", "02", "04", "06"];

function highestUnder(now) {
  now = parseInt(now);
  return timeShift.filter(t => parseInt(t) <= now)
                  .reduce((a, c) => parseInt(c) > parseInt(a) ? c : a);
}

console.log(highestUnder("08"));
console.log(highestUnder("07"));
console.log(highestUnder("09"));
console.log(highestUnder("23"));

3 Comments

i want result one values which is in the data array of 'timeShift'.
@Raffikbojjes yes I believe that function is returning one value (you can run the code snippet to check).
okay sir. its work clearly. Iam sorry, im not check your code carefully. thank you before it :)
1

One way of doing it is, once you have newData array you can just find closest number to 'timeNow' variable in this array. This is probably not efficient but should give you the desired output. Adding following should do the trick:

var closest = newData.reduce(function(prev, curr) {
  return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});

console.log(closest);

3 Comments

Yes alright, thats the point. but should i create a new variable to store the closest number to 'timeNow' variable in the array ? or just like filtering the data and add some logic if ?
Its your choice how you want to process the data further. One of the example can be the added code!
okay sir. Its work so fine. Same with my expectation. Thank you before it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.