## Not sure why this is entering the final if statement in JavaScript

javascript if statement multiple conditions
javascript if else shorthand
if else condition in onclick javascript
if else statement
if statement with array javascript
if statement java
if not in javascript
var if statement javascript

So I am doing a HackerRank JavaScript Challenge(Array Section), basically I need to find the second largest number in an array. I was getting close and looked in the discussion to help get my answer but I'm confused on how it works. The nums array is an array of numbers of n length, which allows duplicates. n can be 1<=n<10, nums can be 1<=nums<=100. The code I have is here :

```function getSecondLargest(nums)
{
var largestNum = nums;
var secondLargest = nums;

for (let i = 0; i < nums.length; i++)
{
if (nums[i] > largestNum)
{

secondLargest = largestNum;
largestNum = nums[i];

continue;
}

if ((nums[i] > secondLargest) && (nums[i] < largestNum))
{
secondLargest = nums[i];
}
}

return secondLargest;
```

So I know by the final loop iteration that secondLargest = 9.

My issue is I believe LargestNum = 10 by the end of the final loop, first if block. So if LargestNum = 10, How does that fulfill this if statement requirement.

num = 10 (I think)

largestNum = 10 (I think)

secondlargest = 9 ( I think)

``` if ((nums[i] > secondLargest) && (nums[i] < largestNum))
```

10 is not less then 10

The answer I get is correct, `secondLargest = 9;` I'm just not certain how the code arrived there.

We use set to remove duplicates `data = [...new Set(data)];`, quick and easy.

```function secondHighest(data) {
data = [...new Set(data)]; // Remove dupicates
// New data after removing duplicates
console.log('Data to test', data)
// Max value placeholder
let max = data;
// Second largest placeholder
let second_biggest = data;
// For loop for our data length
for (let i = 0; i < data.length; i++){
// If current item in data is > max
if(data[i] > max){
// Second largest now equals max
second_biggest = max;
// And max = current data item
max = data[i];
}
// Else if data item > second largest and data item not equal to max
else if (data[i] > second_biggest && data[i]!== max) {
second_biggest = data[i];

}
}
return second_biggest;
}

console.log(secondHighest([1, 2, 3, 4, 50, 60, 60, 7, 8, 9, 99]));```

Making decisions in your code — conditionals, In this article, we'll explore how so-called conditional statements work in JavaScript. As a final point, you may sometimes see ifelse statements written without the This is perfectly valid code, but using it is not recommended — it is much If you want to go outside, make sure to put some sunscreen on. An IF statement is a type of control structure. Control structures represents the concept of modifying the flow of execution of the code in your program. So, typically speaking, the code you write will be “executed” by the JavaScript engine from top to bottom and left to right.

With duplicates:

```const secondLargest = nums => {
const desc = nums.sort((a, b) => a - b).reverse();
let i = 0;
let n = desc[i];
while (n >= desc) {
i++;
n = desc[i];
}
return n;
}

console.log(secondLargest([1, 77, 27, 54, 63, 77, 19, 72, 100, 200, 200, 79]));```

Conditional branching: if, To do that, we can use the if statement and the conditional operator ? , that's also called a “question If that is also falsy, it shows the last alert . It's not recommended to use the question mark operator in this way. If the visitor enters “ECMAScript”, then output “Right!”, otherwise – output: “Didn't know? So, if you had an if statement that was following by and another if statement, the first if statement would be separate from the second if statement just like the example above. However, the second if statement in the example above, has an else built onto it.

If you are using ES6, a nice solution is:

```const nums = [7, 4, 9, 8, 4, 4, 2, 1, 9, 5, 2, 5, 3, 1, 7];
const numsUnique = [...new Set(nums)] // Remove duplicates
const sorted = numsUnique.sort((a, b) => a - b); // Order by ascending
const secondLargest = sorted[sorted.length - 2]; // Get the second last element
```

Tips and Tricks on JavaScript if else Statement Usage, Take this tutorial & start using JavaScript if else statement now. Still, memorizing it might not be the same as understanding how to use it in practice. If a user inputs c , the text states they entered the correct value. How to nest if statements in JavaScript There are many reasons why you might nest if statements — that is, place one if statement within another. In this case, the user selects an option onscreen, CheckChoice () performs a check of that option, and then CheckChoice () displays the correct result onscreen.

If I was you, I would make a max function to get the maximum number. Then I would filter out ever number that is not equal to the max, then run max again on the remaining array.

```function max(array){ // JavaScript already has Math.max, but it takes arguments so we apply
return Math.max.apply(Math, array);
}
function secondMax(array){
var a = array.slice(), m = max(array); // .slice() so original array is not affected
a = a.filter(function(v){
return v !== m;
});
return max(a);
}
console.log(secondMax([1, 77, 25, 72, 77, 23, 72, 21]));```

Javascript if statements not working, You're assigning with = . Use == or === . if( 0 == number ){ text.value = "You didn't enter a number!"; }. Also, be wary of your brace placement. There can be any number of if-else…if statement in the if-else…if block. In if-else, if statement only the statements which satisfy the condition inside that block are executed and the rest of the blocks are ignored. If none of the conditions are met then the statements in else block get executed. Examples of Jquery IF Statement

if-else Statement in JavaScript, It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of� How to use Conditional Statements. Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action. Different Types of Conditional Statements. There are mainly three types of conditional statements in

JavaScript: Conditional Statements, JavaScript supports two conditional statements: ifelse and switch. else {alert(" You did not enter a number!")} } </script>. Click the button below to see is found , the program continues execution at the statement following the end of switch. The problem with making this field available for manual entry as it is now, is that it's fighting the calculation. Since the value of this field is set from a single source, it is a simple matter to move the script to that source, which means there is no conflict for manual entry. This is your best solution.

If, Else, and User Input, A JavaScript tutorial about 'If, Else, and User Input' Now we know how to store values in variables, it's probably worth learning input from the user (which isn't really used very much in the web these days, but it's good for while we're learning ). present the user with the string we entered and a textbox to enter a value in. An empty statement is used to provide no statement, although the JavaScript syntax would expect one.

• why not do soething like `return (nums.length > 2) ? nums.sort().reverse() : nums ` this would sort the values and return the 2nd one...you should look into usort/other more efficient sort algorithms if you expect a large array
• What's in the `nums` array?
• If the case is `num[i] = 10` and `largestNum = 10`, then the update should not happen anyway. Hence the `&&` condition ensures that the duplicates are not considered as second largest number.
• @DaCat - touché. Going to have the get the first entry in the array that is smaller than the one at index 0 (after the descending sort). `const desc = nums.sort().reverse(); let i = 0; n = desc[i]; while(n >= desc) { i++; n = desc[i]; } return n;`
• Thank you for explaining what `set` does. Your answer is very helpful, I find your else if statement easier to read then what I had. Thank you.