1

I am attempting to make a conditional statement where the code will only run if it is a real number 90 vs a string number "90". I don't know what to type into the number part in my conditional statement. I have tried using Number and I have also tried variations of typeof() method. My code is listed below. Thank you

        function sumAll(start, end) {
            let x = 0;
            array = [];

            if (start === Number && end === Number) {
                if (start < end && start >= 0) {

                    for (let i = start; i <= end; i++) {
                        array.push(i);
                        console.log(array);
                        x = i + x;
                    }
                    return x;

                } else if (start > end) {
                    for (let i = start; i >= end; i--) {
                        array.push(i);
                        // console.log(array);
                        x = i + x;
                    }
                    return x;
                } else {
                    return 'ERROR';
                }
            } else {
                return 'ERROR not a number';
            }
        }


        console.log(sumAll(10, 90));
        sumAll(10, "90")
        // expect(sumAll(10, "90")).toEqual('ERROR');
    ```

3 Answers 3

2

let n = 5;

console.log(n === Number);
console.log(typeof n === "number");

The first one logs false, the second one true. You should try using typeof to check if start and end are numbers in your code.

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

Comments

1

You need to use the typeof operator

Using it, you can check to see if start and end are 'number'

function sumAll(start, end) {
  let x = 0;
  array = [];

  if (typeof start === 'number' && typeof end === 'number') {
    if (start < end && start >= 0) {

      for (let i = start; i <= end; i++) {
        array.push(i);
        console.log(array);
        x = i + x;
      }
      return x;

    } else if (start > end) {
      for (let i = start; i >= end; i--) {
        array.push(i);
        // console.log(array);
        x = i + x;
      }
      return x;
    } else {
      return 'ERROR';
    }
  } else {
    return 'ERROR not a number';
  }
}


console.log(sumAll(10, 90));
console.log(sumAll(10, "90"));
// expect(sumAll(10, "90")).toEqual('ERROR');}

2 Comments

I typed in the typeof wrong originally when I tried it. Any tips for learning the syntax of how to type this stuff in correctly?
I've used Mozilla's MDN a lot for reference as well as stackoverflow. Both of those often help me find how to do what I'm trying to do, especially when it comes to new syntax.
1

I think you used typeof wrongly, because that is the answer you need here.

const a = 5;
const b = '5';

if (typeof a === 'number') { } //true
if (typeof a === 'string') { } //false

if (typeof b === 'number') { } //false
if (typeof b === 'string') { } //true


console.log('type of a: ', typeof a);
console.log('type of b: ', typeof b);

But it seems like you can use string '90' as well converting it into number.

function sumAll(start, end) {
  let x = 0;
  array = [];

  if (typeof start === 'string') { start = parseInt(start, 10); } // case '90'
  if (typeof end === 'string') { end = parseInt(end, 10); } // case '90'

  if (Number.isNaN(start) || Number.isNaN(end)) {
    // Error case
    return 'Error not a number'; // Throw could be better here though 
  }

  // any operation you need to do is here
  // if you need to sum number between start and end its called
  // For example summing operation
  return end > start 
           ? (end * (end + 1) / 2) - ((start - 1) * start / 2)
           : (start* (start + 1) / 2) - ((end- 1) * end / 2)
}

console.log(sumAll(10, 12));

Comments

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.