8 kyu

Training JS #7: if..else and ternary operator

25,584 of 42,858myjinxin2015

Description:

In JavaScript, if..else is the most basic conditional statement, it consists of three parts:condition, statement1, statement2, like this:

if (condition) statementa
else           statementb
if (condition) statementa
else           statementb
if (condition) statementa
else           statementb
if (condition) statementa
else           statementb
if condition: statementa
else:         statementb
if condition then statementa
else         statementb end
if (condition) { doThis(); } 
else           { doThat(); } // Note: This code is valid with or without brackets, but it is strongly recommended to use brackets.

It means that if the condition is true, then execute the statementa, otherwise execute the statementb. If the statementa or statementb are more than one line, you need to add { and } at the head and tail of statements in JS, to keep the same indentation on Python and to put an end in Ruby where it indeed ends.

For example, if we want to judge whether a number is odd or even, we can write code like this:

function oddEven(n){
  if (n % 2 == 1) return "odd number";
  else            return "even number";
}
function oddEven(n: number): string {
  if (n % 2 == 1) return "odd number";
  else            return "even number";
}
oddEven=(n)->
  if (n % 2 == 1) return "odd number";
  else            return "even number";
public static string OddEven(final int n){
  if (n % 2 == 1) return "odd number";
  else            return "even number";
}
def odd_even(n):
    if n % 2: return "odd number"
    else:     return "even number"
def odd_even(n)
  if n % 2 then return "odd number"
  else          return "even number" end
end
public static string OddEven(int n)
{
  if (n % 2 == 0) { return "even number"; }
  else            { return "odd number"; }
}

If there is more than one condition to judge, we can use the compound if...else statement. For example:

function oldYoung(age){
  if (age < 16)      return "children"
  else if (age < 50) return "young man"   //use "else if" if needed
  else               return "old man"
}
function oldYoung(age: number): string{
  if (age < 16)      return "children"
  else if (age < 50) return "young man"   //use "else if" if needed
  else               return "old man"
}
oldYoung=(age)->
  if (age < 16)      return "children"
  else if (age < 50) return "young man"   //use "else if" if needed
  else               return "old man"
public static string OldYoung(final int age){
  if (age < 16)      return "children";
  else if (age < 50) return "young man";   //use "else if" if needed
  else               return "old man";
}
def old_young(age):
    if age < 16:        return "children"
    elif age < 50:      return "young man" #use "else if" if needed
    else:               return "old man"
def old_young(age):
  if age < 16 then   return "children"
  elsif age < 50     return "young man" #use "else if" if needed
  else               return "old man" end
end
public static string OldYoung(int age)
{
  if (age < 16)      { return "children"; }
  else if (age < 50) { return "young man"; } // use "else if" if needed
  else               { return "old man"; }
}

This function returns a different value depending on the parameter age.

Looks very complicated? Well, JS and Ruby also support the ternary operator and Python has something similar too:

condition ? statementa : statementb
condition ? statementa : statementb
condition ? statementa : statementb
condition ? statementa : statementb
statementa if condition else statementb
condition ? statementa : statementb
condition ? DoThis() : DoThat();

Condition and statement separated by "?", different statement separated by ":" in both Ruby and JS; in Python you put the condition in the middle of two alternatives. The two examples above can be simplified with ternary operator:

function oddEven(n){
  return n%2 == 1 ? "odd number" : "even number";
}
function oldYoung(age){
  return age < 16 ? "children" : age < 50 ? "young man" : "old man";
}
function oddEven(n: number): string {
  return n % 2 == 1 ? "odd number" : "even number";
}
function oldYoung(age: number): string {
  return age < 16 ? "children" : age < 50 ? "young man" : "old man";
}
oddEven=(n)->
  return n % 2 == 1 ? "odd number" : "even number";

oldYoung=(age)->
  return age < 16 ? "children" : age < 50 ? "young man" : "old man";
public static string OddEven(final int n){
  return n % 2 == 1 ? "odd number" : "even number";
}
public static string OldYoung(final int age){
  return age < 16 ? "children" : age < 50 ? "young man" : "old man";
}
def odd_even(n):
    return "odd number" if n % 2 else "even number"
def old_young(age):
    return "children" if age < 16 else "young man" if age < 50 else "old man"
def odd_even(n):
  return n % 2 == 1 ? "odd number" : "even number"
end
def old_young(age):
  return age < 16 ? "children" : age < 50 ? "young man" : "old man"
end
public static int OldYoung(int age)
{
  return age < 16 ? "children" : age < 50 ? "young man" : "old man";
}

Task:

Complete function saleHotdogs/SaleHotDogs/sale_hotdogs, function accepts 1 parameter:n, n is the number of hotdogs a customer will buy, different numbers have different prices (refer to the following table), return how much money will the customer spend to buy that number of hotdogs.

number of hotdogs price per unit (cents)
n < 5 100
n >= 5 and n < 10 95
n >= 10 90

You can use if..else or ternary operator to complete it.

When you have finished the work, click "Run Tests" to see if your code is working properly.

In the end, click "Submit" to submit your code and pass this kata.

Series:

( ↑↑↑ Click the link above can get my newest kata list, Please add it to your favorites)

Fundamentals
Tutorials

Stats:

CreatedApr 27, 2016
PublishedApr 27, 2016
Warriors Trained60578
Total Skips5977
Total Code Submissions80772
Total Times Completed42858
JavaScript Completions25584
CoffeeScript Completions54
Python Completions10206
Ruby Completions899
Java Completions4251
C# Completions1574
TypeScript Completions924
C Completions739
Total Stars536
% of votes with a positive feedback rating92% of 4524
Total "Very Satisfied" Votes3926
Total "Somewhat Satisfied" Votes502
Total "Not Satisfied" Votes96
Ad
Contributors
  • myjinxin2015 Avatar
  • Javatlacati Avatar
  • GiacomoSorbi Avatar
  • anter69 Avatar
  • donaldsebleung Avatar
  • Chrono79 Avatar
  • kazk Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • rge123 Avatar
Ad