简体   繁体   中英

spread operator function in javascript

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

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:

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]) 

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) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM