Home > OS >  I want to ignore the string in all cases and return numbers
I want to ignore the string in all cases and return numbers

Time:09-11

I need to multiply any arguments I add, I just need to ignore the string in all cases and then multiply just numbers. and if all strings without numbers > the output should return // "All Is String"

function specialMix(...data) {
  let result = 0;
  for (let i = 0; i < data.length; i  ) {
    let check = parseInt(data[i]);
    if (Number.isInteger(check)) {
      result  = check;
    }
    if (Number.isNaN(result)) {
      result  = check;
    }
  }
  console.log(result);
}
specialMix(10, 20, 30); // 60
specialMix("10Test", "Testing", "20Cool"); // 30
specialMix("Testing", "10Testing", "40Cool"); // 50
// the problem here >>>
specialMix("Test", "Cool", "Test"); // All Is Strings

CodePudding user response:

Convert every value to a number, then filter out those that couldn't be parsed by Number.parseInt(). If the result is an empty array, then there were no numbers passed to the function.

function specialMix(...data) {
  const convertedData = data
    .map(entry => Number.parseInt(entry))
    .filter(entry => Number.isInteger(entry));

  if (!convertedData.length) {
    return 'All Is Strings';
  }
  
  return convertedData.reduce((acc, cur) => {
    acc  = cur;
    return acc;
  }, 0);
}

console.log(specialMix(10, 20, 30)); // 60
console.log(specialMix("10Test", "Testing", "20Cool")); // 30
console.log(specialMix("Testing", "10Testing", "40Cool")); // 50
console.log(specialMix("Test", "Cool", "Test")); // All Is Strings

CodePudding user response:

You could join that array and test for numbers at the start...

let hasNumbers = data.join().replaceAll(/\D/g, '').trim() != ''

function specialMix(...data) {
  let hasNumbers = data.join().replaceAll(/\D/g, '').trim() != ''
  if (!hasNumbers) return console.log("all is numbers");
  let result = 0;
  for (let i = 0; i < data.length; i  ) {
    let check = parseInt(data[i]);
    if (Number.isInteger(check)) {
      result  = check;
    }
    if (Number.isNaN(result)) {
      result  = check;
    }
  }
  console.log(result);
}
specialMix(10, 20, 30); // 60
specialMix("10Test", "Testing", "20Cool"); // 30
specialMix("Testing", "10Testing", "40Cool"); // 50
// the problem here >>>
specialMix("Test", "Cool", "Test"); // All Is Strings

CodePudding user response:

This solution, where you set a boolean to false and then mark true if any of the looped items are parse-able, is the most efficient way to your requirements, as it utilizes the existing loop through the arguments, keeping the number of loops through arguments array to 1.

function specialMix(...data) {
  let result = 0;
  let foundAParsableValue = false;
  for (let i = 0; i < data.length; i  ) {
    let check = parseInt(data[i]);
    if (Number.isInteger(check)) {
      result  = check;
      foundAParsableValue = true;
    }
    if (Number.isNaN(result)) {
      result  = check;
    }

  }

  if (foundAParsableValue) {
    console.log(result);
  } else {
    console.log("All Is Strings");
  }
}
specialMix(10, 20, 30); // 60
specialMix("10Test", "Testing", "20Cool"); // 30
specialMix("Testing", "10Testing", "40Cool"); // 50
specialMix(0, 0, 0); // 0

specialMix("Test", "Cool", "Test"); // All Is Strings

CodePudding user response:

The quick way is to simply remove the isNaN condition. Since you are going to add only the numbers, and at the end, after the for loop, ask for the value of the result, which if it is 0 is because, most likely, there are no numbers. And you return that they are all strings. The solution is:

function specialMix(...data) {
  let result = 0;
  for (let i = 0; i < data.length; i  ) {
    let check = parseInt(data[i]);
    
    if (Number.isInteger(check))
      result  = check;
    
  }
  
  if(result === 0) 
    result = "All Is String";
  
  console.log(result);
}
specialMix(10, 20, 30); // 60
specialMix("10Test", "Testing", "20Cool"); // 30
specialMix("Testing", "10Testing", "40Cool"); // 50
// the problem here >>>
specialMix("Test", "Cool", "Test"); // All Is Strings

Now, this has its limitations and that is that:

  1. There can be 0's in the string and the total sum is 0.
  2. Since you take the number from the string, it won't work if the number is in another part of the string, for example in the middle or at the end. So it will ignore it and it's like that argument doesn't have numbers, a failure.

The main solution The solution that can fix what you want and the aforementioned limitations is:

function specialMix() {
  let result = 0;
  let areThereNumbers = false;
  
  for (let i = 0; i < arguments.length; i  ) {
        const data = arguments[i];
    
    // If it's a number just added
    if(Number.isInteger(data)) {
      result  = data;
      areThereNumbers = true;
      continue;
    }
      
    // Get all numbers in the data
    const numbers = data.match(/\d/g);
    
    // If there is no any number just continue to the other data 
    if(!numbers) continue;
    
    // If there is a number, added it
    areThereNumbers = true;
    const number = parseInt(numbers.join(""));
    result  = number;
  }
  
  if(result === 0 && !areThereNumbers)
    result = "All Is String";
  
  console.log(result);
}

specialMix(10, 20, 30); // 60
specialMix("10Test", "Testing", "20Cool"); // 30
specialMix("Testing", "10Testing", "40Cool"); // 50
specialMix("Test", "Cool", "Test"); // All Is Strings

specialMix("x", "asdasd0", "a10", "xxa1"); // 11
specialMix("d", "q", "x", "asd"); // All Is Strings
specialMix("0", "a", "a", "asdas0"); // 0

Explanation

  1. Use is made of the arguments that give us in an array all the arguments that are passed to a function, which gives a solution to working with an indefinite number of arguments.
  2. We define the result variable that will have the sum of the numbers in the arguments.
  3. We define a variable that will act as a flag and will tell us if there are numbers or not among all the arguments.
  4. We loop through the arguments.
  5. We ask if the argument is a number per se, and if it is, we add it and skip doing more operations on this argument and jump to the next iteration of the loop.
  6. If the argument is not a number, we get all the numbers that exist in the string. The match method returns an array with all the matches of the regex, which in this case is a simple one that matches if there are digits.
  7. If the match method fails to match anything, it returns null so we know there is no number in the argument so we jump to the next iteration of the loop.
  8. If match matches something, it is because there is a number, so we pass the array to a string and convert that string to an integer to do the sum. This can be modified if the numbers do not necessarily form a unit, but are independent, this gives the basis for obtaining the numbers independently and adding them. But in this case, we simply assume that the numbers in the argument make up one.
  9. We add the number in the argument.

Outside of the for loop, we ask if the result is 0, and if so, we want to know if it's because the arguments don't have numbers, in which case, the result will be that everything is a string. But in case there are numbers but they are all 0, then we will get the integer 0.

Hope this can help you.

  • Related