0

I have some problem with this function that has firstValue(number), lastValue(number) and dataArray(array).The function has validation of firstvalue < lastValue and the number of records in the DataArray must be greater than 5. The function will search for data in the dataArray that has a value between the firstvalue and lastValue, sort the search results and display them on the console.

This is What I got so far. I have not used Javascript before, so any help would be appreciated.

let result ="";
function valueSelection(firstValue, lastValue, ...dataArray){
 if(firstValue > lastValue) {
 console.log("The last value must be greater than first value");
 if(dataArray.length < 5) {
 console.log("Numbers in dataArray must be more than 5");
 } else {
 console.log(result);
 }
 } else {
 console.log(result);
 }
 }
valueSelection(5, 20 , [2, 25, 4, 14, 17, 30, 8]) 

the output I want is: [8, 14, 17]

HandlingError :

valueSelection(15, 3 , [2, 25, 4, 14, 17, 30, 8])

output:"The last value must be greater than first value"

valueSelection(4, 17 , [2, 25, 4])

output: Numbers in dataArray must be more than 5

Yilmaz
51k19 gold badges226 silver badges278 bronze badges
asked Nov 14, 2020 at 4:52
5
  • Is the browser you are using listed here: caniuse.com/?search=spread%20operator Commented Nov 14, 2020 at 4:56
  • 2
    Also, you say the output you want is [8, 14, 17]. How are you coming up with that? Your input for firstValue is 5 and lastValue is 20. The if statement is false, so it jumps to the console.log(result). The value for result is just an empty string as shown in the very first line of your code. Were you expecting to change the value of result somewhere? Commented Nov 14, 2020 at 5:00
  • 1
    What is the process of getting [8, 14, 17]? Commented Nov 14, 2020 at 5:02
  • @kojow7 I think he is asking help to figure out the rest of the algorithm. I think he got [8, 14, 17] since its between 5 and 20. Commented Nov 14, 2020 at 5:04
  • thank guys for figuring out, it is done Commented Nov 14, 2020 at 5:36

4 Answers 4

1

Have a look at the MDN documentation for Spread syntax , I do not think you require to use it for this question:

function valueSelection(firstValue, lastValue, dataArray) {
 if (firstValue > lastValue) {
 console.error("lastValue must be greater than firstValue");
 }
 else if (dataArray.length < 5) {
 console.error("dataArray must contain 5 or more numbers");
 } else {
 const result = dataArray.filter(x => x >= firstValue && x <= lastValue);
 console.log(result.sort((a, b) => a - b));
 }
}
valueSelection(5, 20, [2, 25, 4, 14, 17, 30, 8]);
valueSelection(15, 3, [2, 25, 4, 14, 17, 30, 8]);
valueSelection(4, 17, [2, 25, 4]);

Other relevant links:

answered Nov 14, 2020 at 5:06
Sign up to request clarification or add additional context in comments.

Comments

0

You do not require the spread operation at all.

I have also improved the logic so that it works more efficently :)

function valueSelection(firstValue, lastValue, dataArray){
 let result = []
 console.log(dataArray.length)
 if(firstValue < lastValue && dataArray.length > 5 ) {
 result = dataArray.filter(data => data > firstValue && data < lastValue )
 console.log(result.reverse())
 return result.reverse()
 } else {
 console.log("The last value must be greater than first value and Numbers in dataArray must be more than 5" );
 }
 }
valueSelection(5, 20 , [2, 25, 4, 14, 17, 30, 8]) 
answered Nov 14, 2020 at 5:08

Comments

0

You don't need the spread operator if you will pass an array as the parameter.

What happends when you use "...dataArray" is that all the parameters starting at pos 2 would be added to the array as a element.

So, in your example, your third parameter is an array [2, 25, 4, 14, 17, 30, 8] and you don't have any other after that one.

resulting into

let result ="";
function valueSelection(firstValue, lastValue, ...dataArray){
 //firstValue = 4, lastValue = 17, dataArray [ [2, 25, 4, 14, 17, 30, 8]] which is, an array where the first element is the array you sent as param
 if(firstValue > lastValue) {
 console.log("The last value must be greater than first value");
 if(dataArray.length < 5) {
 console.log("Numbers in dataArray must be more than 5");
 } else {
 console.log(result);
 }
 } else {
 console.log(result);
 }
 }
valueSelection(5, 20 , [2, 25, 4, 14, 17, 30, 8]) 

if you remove the spread operator, the code would work as you want.

The use of the ...dataArray would make sense if instead of sending an array as the third parameter you send a bunch of single numers like in this example

let result ="";
function valueSelection(firstValue, lastValue, ...dataArray){
 //firstValue = 4, lastValue = 17, dataArray [2, 25, 4, 14, 17, 30, 8] an array made of all the parameters sent starting for the third one
 if(firstValue > lastValue) {
 console.log("The last value must be greater than first value");
 if(dataArray.length < 5) {
 console.log("Numbers in dataArray must be more than 5");
 } else {
 console.log(result);
 }
 } else {
 console.log(result);
 }
 }
valueSelection(5, 20 , 2, 25, 4, 14, 17, 30, 8) 
answered Nov 14, 2020 at 5:08

Comments

0

Couple mistakes in your code. Also, you don't explain how you come up with the result so I'm assuming the values must be between firstValue and lastValue. You should also throw the Errors and catch them in a try-catch.

function valueSelection(firstValue, lastValue, dataArray) {
 if (firstValue > lastValue) return "The last value must be greater than first value";
 if (dataArray.length < 5) return "Numbers in dataArray must be more than 5";
 let result = [];
 for (let i = 0; i < dataArray.length; i++) {
 const v = dataArray[i];
 if (v > firstValue && v < lastValue) result.push(v);
 }
 return result.sort((a, b) => a - b);
}
console.log(valueSelection(5, 20, [2, 25, 4, 14, 17, 30, 8]));
//the output I want is: [8, 14, 17] 
//HandlingError :
console.log(valueSelection(15, 3, [2, 25, 4, 14, 17, 30, 8]));
//output:"The last value must be greater than first value"
console.log(valueSelection(4, 17, [2, 25, 4]));
//output: Numbers in dataArray must be more than 5

answered Nov 14, 2020 at 5:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.