Numbers JavaScript

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a function that takes a number, doubles it and returns the value

function double(num) { return 2*num; }

write a function that takes a number and returns that many numbers from the fibonacci sequence

function fib(n){ var arr = []; if (n == 0) { return arr; }else if (n==1){ arr.push(1); return arr; }else { arr.push(1, 1); for (var i=2; i<n; i++){ var lastNum = arr[arr.length-1] var secLasNum = arr[arr.length-2] arr.push(lastNum+secLasNum); } return arr; } }

Write a function that takes in a number and returns true if even and false If odd (way two)

function isEven(num) { return !(num/2).toString().includes('.'); }

Write a function that takes in a number and returns true if even and false If odd (way one)

function isEven(num) { return num % 2 == 0; }

Write a function that takes in two numbers, multiplies them and returns the result

function multiplyThem(num1, num2){ return num1*num2; }

Write a function that takes a number, triples it, adds 2 to it and returns the value (way two)

function tripleItAndAddTwo(num){ return num*3+2; }

Write a function that takes a number, triples it, adds 2 to it and returns the value (way one)

function tripleItAndAddTwo(num){ var a = num*3+2; return a; }

Using JavaScript make a variable named "a" set to 5

var a = 5;

Using JavaScript write a loop that console logs 1 to 100

//one way for (var i=1; i<=100; i++){ console.log(i); } //another way for (var i=1; i<101; i++){ console.log(i); } //another way for (var i=0; i<100; i++){ console.log(i+1); }

return the product up to a number

//one way function productUpTo(n){ var product = 1; for (var i=2; i<=n; i++){ product = product * i; } return product; }

Using JavaScript write a function that takes a number and returns the sum up to that number

/one way function sumUpTo(n){ var total = 0; for (var i=1; i<=n; i++){ total += i; } return total; } //another way function sumUpTo(n){ return n(n+1)/2; }


Ensembles d'études connexes

Relationships between f, f prime and f double prime

View Set

ALL APGO UWISE - SHANE, Uwise Comprehensive, OBGYN uwise, UWise, APGO Part 1, Unit 1: Approach to the patient, Obsetrics&Gynecology, U-wise Maternal-Fetal Physiology, uWISE Unit 2: Obstetrics A - Normal Obstetrics

View Set

OB chapter 15, Neonatal Period: Discharge Planning and Teaching

View Set

Chapter 9: Policy in Public Health

View Set

Solving Polynomial Equations using Technology

View Set

Med Surg Ch 45 Oral and Esophageal Disorders

View Set

ITIL V4 Review Session 4 (Objectives 7)

View Set