parseInt("23412"); //<-- Function, which is standalone. Math.random( ) //<-- Method, called from the Math class. s.toUpperCase( ) //<-- Method, called from the String object s.
var a = [4, 2, 6, 9, 2, 4]; console.log(a.toString( )); // Output: 4,2,6,9,2,4
// a. function inToCm(inches) { return inches * 2.54; } // Answer: var inToCm = inches => inches * 2.54; console.log(inToCm(2.5); // Output: 6.35 // b. function printGreeting( ) { console.log("Hello!"); } // Answer: var printGreeting = ( ) => { console.log("Hello!"); console.log(printGreeting( )); // Output: Hello! // c. function makeGreeting(firstName) { return "Hello, " + firstName + "."); } // Answer: var makeGreeting = firstName => "Hello, " + firstName + "."; console.log(makeGreeting("Alice")); // Output: Hello, Alice. // d. function ftInToIn(feet, inches) { return feet * 12 + inches; } // Answer: var ftInToIn = (feet, inches) => feet * 12 + inches; console.log(5, 3); // Output: 63
function showTime( ) { var d = new Date( ); console.log(d); } setInterval(showTime, 1000);Rewrite this script using anonymous function instead of the showTime function. Answer:
// Using anonymous function setInterval(function( ) { var d = new Date( ); console.log(d); }, 1000);Rewrite it again using an anonymous function in arrow notation.
// Anonymous function using arrow notation: setInterval(( ) => { var d = new Date( ); console.log(d); }, 1000);
10:10:00 1/17/2023Date Methods: getDate getFullYear getHours getMinutes getMonth getSeconds
setInterval(( ) => { var date = new Date( ); var y = date.getFullYear( ); var m = date.getMonth( ); // Add one to date because dates are zero-based. var d = date.getDate( ) + 1; var h = date.getHours( ). toString( ).padStart(2, "0"); var min = date.getMinutes( ). toString( ).padStart(2, "0"); var sec = date.getSeconds( ). toString( ).padStart(2, "0"); var output = `${h}:${min}:${sec} ${m}/${d}/${y}`; console.log(output); }, 1000);
var arr = ["ibm", "microsoft", "google", "oracle"];Output the array items one per line like this:
ibm microsoft google netscapeHere are three for loops that accomplish this:
for(let i = 0; i < arr.length; i++) { console.log(arr[i]); }A traditional for loop has three parts (1) initialization, (2) condition, (3) iteration, (4) body:
for (initialization; condition; iteration) { body; }The initialization is performed once before executing the body of the for loop. The condition is checked at thebeginning of the loop, if the condition is true the body is executed again, if the condition is false, the control flow passes to the next statement after the end of the loop. The iteration is performed at the end of the loop, then control passes back to the beginning of the loop. Compare the preceding for loop with this while loop that does the same thing.
for(let n of arr) { console.log(arr[i]); }
function print(item) { console.log(item); } arr.forEach(print)
> cd test-http-status-codes
> npm initPress Enter to accept the default values for package-name, and version:
package-name: (test-http-status-codes) version: (1.0.0)For the entry point, use main.js:
entry point: (index.js) main.jsAccept the default values (null string) for test command, git repository, and keywords:
test command: git repository: keywords:For author, enter your name:
author: Stan SmithClick Enter to accept the default value for license:
license: (ISC)Click Enter to answer yes for this final question:
Is this OK: (yes)The JSON file package.json is created:
{ "name": "http-status-codes", "version": "1.0.0", "description": "Test HTTP Status Codes Module", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Stan Smith", "license": "ISC" }
> npm i http-status-codes
const HttpStatusCodes = require("http-status-codes"); console.log(HttpStatusCodes.StatusCodes.OK); console.log(HttpStatusCodes.StatusCodes);
node main.js