Giter Club home page Giter Club logo

conditional_assignment's Introduction

Conditional Exercises

  1. Write a block of code that checks and logs whether variables are equal or not.
let num = 3
let bar = 10
if ( num === bar){
console.log('Yes')
}else
{
console.log('No')
}
  1. Write a block of code that logs which of two number variables is larger.
  • Your output should look like this (in this example for the numbers x = 5 and y = 10): 'The greater number of 5 and 10 is 10'
  • If the two numbers are equal, log: 'The numbers are equal.'
let num = 10
let bar = 1
if (num > bar){
  console.log('The greater number of ' + num + ' and ' + bar + ' is ' + num  )
} else if (num < bar){
  console.log('The greater number of ' + num + ' and ' + bar + ' is ' + bar  )
} else {
  console.log('They are equal')
}
  1. Create a string variable with a language code (e.g. "english", "spanish", "german"). Use an if - else if - else statement to log "Hello, World" in the given language, for at least 3 languages. It should default to logging English.
let language = 'English'

if (language === 'German'){
  console.log('Hallo Welt')
} else if (language === 'Spanish'){
  console.log('Hola Mundo')
} else if (language === 'English'){
  console.log('Hello World')
} else {
  console.log('Done checking')
}
  1. Do question 3 again but this time use a switch statement.
switch (language = 'English'){

case 'German':
  console.log('Hallo Welt')
  break;
case 'Spanish':
  console.log('Hola Mundo')
  break;
case 'English':
  console.log('Hello World')
  break;
default:
  console.log('Done checking')

}
  1. Create a variable numGrade that will contain a numeric grade (out of 100). Use an if - else if ... - else statement to log the corresponding letter grade, either "A", "B", "C", "D", or "F".
let grade = 100

if (grade > 90) {
  console.log('A')
} else if (grade > 80 ) {
  console.log('B')
} else if (grade > 70){
  console.log('C')
} else if (grade > 60){
  console.log('D')
} else {
  console.log('F')
}
  1. Create two variables: animalName ('cat', 'dog', etc.) , and numberOfAnimals.

Use an if - else statement to log:

  • the number

  • either in single or plural form. Exp: "5 cats" or "1 dog".

Bonus: Make it handle a few collective nouns like "sheep" and "geese".

let typeOfAnimal = ' cat'
let numberOfAnimals =12

if (numberOfAnimals === 1){
  console.log(numberOfAnimals + typeOfAnimal)
} else {
  console.log(numberOfAnimals + typeOfAnimal + 's')
}
  1. Write a block of code that logs whether a given number is even or odd.
let weather = 99

if (weather % 2 === 0) {
  console.log('Even')
} else {
  console.log('Odd')
}
  1. Do question 7 again but use a ternary.
let weather =100

weather % 2 === 0 ? console.log('Even'): console.log('Odd')
  1. Write a block of code that checks and logs whether a given number is positive, negative, or equal to 0.
let num = 10

if (num > 0){
  console.log('Positive')
} else if (num < 0){
  console.log('Negative')
} else {
  console.log("Equal")
}

conditional_assignment's People

Contributors

coreyladovsky avatar jevit01 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.