bite some code

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

Have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000. Use the Parameter Testing feature in the box below to test your code with different arguments.

function SimpleAdding(num) { var sum = num; for (var i = 1; i < num; i++) { sum += i; } // code goes here return sum; } // keep this function call here // to see how to enter arguments in JavaScript scroll down print(SimpleAdding(readline()));

Have the function TimeConvert(num) take the num parameter being passed and return the number of hours and minutes the parameter converts to (ie. if num = 63 then the output should be 1:3). Separate the number of hours and minutes with a colon. Use the Parameter Testing feature in the box below to test your code with different arguments.

function TimeConvert(num) { var minutes = num % 60; var hours = parseInt(num/60); return "" + hours + ":" + minutes; } // keep this function call here // to see how to enter arguments in JavaScript scroll down print(TimeConvert(readline()));

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it (ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18. Use the Parameter Testing feature in the box below to test your code with different arguments.

//welcome to recursion! //recursion is simply calling a function from within itself to rerun the function //it's like a complex for loop a lot of the time, but is also super useful for binary searchs function FirstFactorial(num) { if( num === 1 ) { //recursion needs a base case, otherwise it will go on forever return 1; //if num is ever 1 we break out of the recursion } return num * FirstFactorial(num-1); //recursion magic! for factorials (num!) the answer is num * num-1 ... * 1 } //so we need to call FirstFactorial on all the numbers below num and collect the results

Have the function CheckNums(num1,num2) take both parameters being passed and return the string true if num2 is greater than num1, otherwise return the string false. If the parameter values are equal to each other then return the string -1. Use the Parameter Testing feature in the box below to test your code with different arguments.

function CheckNums(num1,num2) { if (num1 == num2) { return "-1"; } else { return num2 > num1; } } // keep this function call here // to see how to enter arguments in JavaScript scroll down print(CheckNums(readline()));

Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. Use the Parameter Testing feature in the box below to test your code with different arguments.

function FirstReverse(str) { return str.split("") //splits the string into an array of characters .reverse() //reverses the array .join(""); //joins the array back into a string, with no space between the characters }

different way to solve this problem:

function Palindrome(str) { str = str.split(" ").join("").toLowerCase(); var rev = str.split("").reverse().join(""); return str === rev; } // keep this function call here Palindrome(readline());

Have the function Palindrome(str) take the str parameter being passed and return the string true if the parameter is a palindrome, (the string is the same forward as it is backward) otherwise return the string false. For example: "racecar" is also "racecar" backwards. Punctuation and numbers will not be part of the string. Use the Parameter Testing feature in the box below to test your code with different arguments.

function Palindrome(str) { str = str.split(" ").join(""); // code goes here for(var i = 0; i < str.length/2; i++) { if (str[i] != str[str.length - 1 - i]) { return false; } } return true; } // keep this function call here // to see how to enter arguments in JavaScript scroll down print(Palindrome(readline()));


Ensembles d'études connexes

Abnormal Psychology Final Exam Part 1

View Set

ISY 251 - Chapter 2: Security Policies and Standards

View Set

TopHat Circulation & Short-Term Blood Pressure Regulation Questions

View Set

CHAPTER 6 DEP 3305 MCGRAW HILL CONNECT

View Set

The Writing Process: Revising, Editing and Proofreading and Essay types

View Set

Chem 103- Prefixes, Conversion and sig figs

View Set

RNSG1126 Module 2 Pre-Module Quiz

View Set

CompTIA A+ Core 1 Practice Test part 2

View Set