user10090764
user10090764

Reputation: 25

Function to verify if first OR last item in array are equal to a certain number

I am trying to write a function to check if an array contains a specific number at the first or last position of the array.

All of the console.log examples there should print true. The way its printing I don't think I should be writing to console.log in my function. I can't get them all to show true.

function checkForNumber(arr, num) {
  if (arr[0] === num) {
    //console.log(true) removed
    return true
  } else if (arr.slice(-1)[0] === num) {
    //console.log(true) removed
    return true
  } else
    return false;
}
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

Upvotes: 0

Views: 1302

Answers (5)

Bharata
Bharata

Reputation: 14175

In your first version of your question you had console.log(true); instead of return true; and this was wrong.

function checkForNumber(arr, num)
{
	if(arr[0] === num)
	{
		//console.log(true);
		return true;
	}
	else if (arr[arr.length-1] === num)
	{
		//console.log(true);
		return true;
	}
	else return false;
}
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

You could write too:

function checkForNumber(arr, num)
{
	return  arr[0] === num || arr[arr.length - 1] === num;
}

console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

But your second code version is correct and if you write it on this way:

function checkForNumber(arr, num)
{
    return  arr[0] === num || arr.slice(-1)[0] === num;
}

then it is one symbol even shorter than:

function checkForNumber(arr, num)
{
    return  arr[0] === num || arr[arr.length - 1] === num;
}

Upvotes: 1

Aurel Bílý
Aurel Bílý

Reputation: 7983

function checkForNumber(arr, num) {
  return arr[0] === num || arr[arr.length - 1] === num;
}
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

if (condition) return true and similar are anti-patterns. If the function is a predicate (i.e. returning a boolean value), just return the result of the logical operation.

Upvotes: 5

CrazyMenConnected
CrazyMenConnected

Reputation: 702

I think that this is enough, right? Cheers!

function checkForNumber(arr, num)
{
    // for OR
    return ((arr[0] === num) || (arr[arr.length-1] === num));
    // for AND
    // return ((arr[0] === num) && (arr[arr.length-1] === num));
}
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([3, 2, 5, 4], 3) === true);
console.log(checkForNumber([0, 4, 4, 8], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

Upvotes: 1

Adam H
Adam H

Reputation: 1828

Here is the solution fully implemented that I suggested in the comments.

function checkForNumber(arr, num) {
  if (arr[0] === num || arr[arr.length - 1] === num) {
    return true
  } else
    return false;
}

console.log(checkForNumber([4, 2, 5, 3], 4));
console.log(checkForNumber([4, 2, 5, 3], 3));
console.log(checkForNumber([4, 2, 5, 3], 2));
console.log(checkForNumber([4, 2, 5, 3], 13));
console.log(checkForNumber([4, 2, 5, 4], 4));

Upvotes: 1

user10090764
user10090764

Reputation: 25

I tried doing this while editing, but i replaced the console.log(true) inside the function with return true. This solved my question.

Correct Code here again:

function checkForNumber(arr, num) {
  if (arr[0] === num) {
     //console.log(true) removed
     return true
   } else if (arr.slice(-1)[0] === num) {
     //console.log(true) removed
     return true
   } else
  return false;
 }
console.log(checkForNumber([4, 2, 5, 3], 4) === true);
console.log(checkForNumber([4, 2, 5, 3], 3) === true);
console.log(checkForNumber([4, 2, 5, 3], 2) === false);
console.log(checkForNumber([4, 2, 5, 3], 13) === false);

Upvotes: 0

Related Questions