Coder Social home page Coder Social logo

js-basics-1's Introduction

Exercise 1: Variables and values

  • Open the index.html file using a Live Server
  • Open the DevTools Console
  • Perform the following exercises by making changes to script.js
  1. Using const, define a variable called x with the value 5.25

    • Log it to the console using console.log('The value of x is', x)
  2. Using let, define a variable called y with the value 'Hello!'

    • Log it to the console
  3. Using var, define a variable called z with the value true

    • Log it to the console
  4. Define a variable called sum with the value of x plus 15

    • Log it to the console
  5. Define a variable called bodyClass with the value of document.body.className

    • Log it to the console
  6. Change document.body.className to "loading"

  7. Log the value of bodyClass to the console again

  8. Define a variable called randomNumber with the value of Math.random()

    • Log it to the console
  9. Define a variable called mainClass to store the className of the main element on the page

    • Log it to the console

Exercise 2: Functions

  1. Define a function hello that displays an alert with the message "Hello, world!"

    • Invoke it to verify that it works
  2. Update the hello function above to console.log a message ("I am the hello function") after displaying the alert.

  3. Define a function hi that takes a single argument name and returns the string "Hi, <name>!".

    • when invoked like hi('John') it should return "Hi, John!"
    • when invoked like hi('Ben') it should return "Hi, Ben!"
  4. Log the output of the hi function to the console

  5. Define a function add that takes two arguments: num1 and num2 and returns the sum of num1 and num2

  6. Define a function random5 that does not take any arguments and returns a random number between 1 and 5 (no decimals)

    • Hint: Multiply Math.random() by 5 and then use Math.round()

js-basics-1's People

Contributors

spinningarrow avatar kod2nd 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.