- gain familiarity with using JavaScript in a browser’s console
- practice using basic sequential JavaScript programming including:
- numbers and simple arithmetic operations
- strings, string concatenation and string length
- variables and assignment
`prompt`

and`alert`

for crude input and output

- prepare for the first programming assignment.

- Work on this lab with your partner (or on your own if you have not been assigned one).
- Unless otherwise indicated, use only the JavaScript operations and commands we have described thus far in class.
- Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.
- You might not complete the exercises during our scheduled lab. If so, it is then up to you to complete the lab on your own time.

**A warm up**. Using the browser’s JavaScript console, try these examples in order, one at a time:`> 59 > 33 + 47 > 33 - 47 > 3 * 7 > 23 * 34 > 782 / 34 > 7 / 3 > 782 % 34 > 7 % 3 > x > let x = 5; > x > x = x + 1; > x > alert('x'); > alert(x); > alert('the value of x is ' + x);`

**String exercises**. Try these in order (again, using the browser’s JavaScript console), one at a time:`> let word = 'the'; > word.length > alert(word); > let phrase = word + ' time'; > alert(phrase); > phrase = 'now is ' + phrase; > alert(phrase); > phrase.length`

**Inputting numbers**. Again, using the browser’s JavaScript console, try the three examples below, one at a time:`> prompt('type something: ') [type: 34] > parseFloat(prompt('type something: ')) [type: 34] > parseFloat(prompt('type something: ')) [type: thirty-four]`

You should be able to explain why the results produced are different in each case.

**Experiment**more with the JavaScript console. Identify cases where the console’s response is not what you would expect.

To work on the remaining problems:

- Download and unzip the starter archive.
- Use Visual Studio code to open the
`lab1`

folder and then the file`lab1.js`

. - Write all your code in the file
`lab1.js`

. - Replace
`(YOUR NAMES)`

with*your names*(without the parentheses) in the comment near the top of`lab1.js`

. - Open
`lab1-demo.html`

in your browser to experiment with my solution. - Open your
`lab1.html`

in your browser to test your solution. Remember to save your file in VS Code and reload your`lab1.html`

in your browser as you continue to test your work. - Fill in all the areas of the supplied
`lab1.js`

file marked by`//`

comments with`...`

. Remove those comments once you are finished with that part of the assignment. - Use the browser JavaScript console to assist you, in particular to identify
*syntax errors*.

**The problems themselves**.

Complete the function

`doubleWord`

so that it asks the user (using`prompt`

) to enter a word (really any piece of text) and then displays (using`alert`

) the word repeated twice consecutively. For example, if the user enters`chi`

it should display`chichi`

.Complete the function

`threeN1`

so that it asks the user to enter a number and then computes and displays one more than three times that number. For example, if the user enters 7 it should display:`Result: 22`

Modify the function

`phraseLength`

so that after it asks the user to enter a phrase (really any string of text) it reports not only the phrase itself, but the length of the phrase. For example, if the user enters`Hello world!`

the function should display:`The phrase "Hello world!" is 12 characters long.`

Only modify the line marked with

`// ...`

and remove that comment when done.Consider the function

`add3`

. It has a bug! Spot the bug and fix it so that the function correctly asks the user for a number and displays three more than that number. Modify the way it displays the result so that instead of saying something of the form: “wrong answer …” it says “Three more than … is …” as in:`Three more than 17 is 20.`

Only modify the lines that have

`// ...`

comments on them. (Remove those comments as you make your modifications.)Complete the function

`milesToInches`

so that it asks the user to enter a number of miles and then computes and displays the equivalent number of inches. For example, If the user enters 6, it should report:`There are 380160 inches in 6 miles.`

(There are 12 inches in a foot and 5280 feet in a mile.)

**An algorithm’s inverse**. Below is a function that converts Celsius to Fahrenheit:`function c2f() { let c = parseFloat(prompt('What is the Celsius temperature?')); let f = (9/5 * c) + 32; alert(c + ' degrees Celsius is ' + f + ' degrees Fahrenheit.'); }`

(You can try it here.)

Complete the function

`f2c`

so that it is structured similarly to`c2f`

and so that it achieves the inverse: it converts Fahrenheit to Celsius. For example, if the user enters`212`

it should report:`212 degrees Fahrenheit is 100 degrees Celsius.`

Complete the function

`proseProblem`

so that it corresponds to the following prose description:*Take a number called x. Output the difference between the square of two more than x and the square of one more than x.*Use variables and assignments.

**Keep your arithmetic expressions simple: no more than a single arithmetic operation per assignment statement.**The goal is to be literal - follow the steps implied by the prose description. (It is true that if you apply some algebra then this computation can be simplified. But no need - let the computer do the work!)**Hermione goes apple picking**. Complete the function`applesForTeachers`

so that it asks how many apples Hermione brings to school and then how many teachers she has. It should then report how many apples she gives to each teacher and how many apples she has left over to share with the Gryffindor common room. Assume that she gives the same number of apples to each teacher and she gives as many apples to her teachers as she can. (In other words, use division, remainder and subtraction.) For example, if Hermione brings 50 apples total to give to her 11 teachers the function should first report:`Hermione gives 4 apples to each teacher.`

and then report:

`She has 6 apples left to share with her pals.`

**Hours, minutes, seconds**[courtesy of Jim Marshall]. Complete the function`seconds`

so that it asks the user for a total number of seconds, and converts this to the corresponding number of hours, minutes, and seconds. (*Hint: the remainder operator*.) Examples: If the user enters 250 seconds, the`%`

might come in handy here`That is 0 hours 4 minutes and 10 seconds.`

If the user enters 10000 seconds, the function should output:

`That is 2 hours 46 minutes and 40 seconds.`

Complete the function

`repeats`

so that it asks the user to enter a word (really any piece of text) and then displays the word 25 times, once per line. For example if the user enters`cool`

the function should display a result like this:Use varable assignment and string concatenation (and the newline character

`\n`

) to make your code compact. (For my solution, the body of the function is 6 lines and 124 characters, not including comments and indentation.) The idea is to use*symbolic abstraction*- where a variable stands in for a computation, in this case to avoid some redundancy in the code.**Challenge**. Write a function from scratch (call it whatever you think appropriate) such that it asks the user for a number and then rounds the number down to the nearest whole number. If that rounded number is even, it displays one half the rounded number; if the rounded number is odd it displays one more than three times the rounded number. For example, if the user enters`7.2`

, it reports`22`

since 7.2 rounds down to 7 which is odd. If the user enters`24.3`

it reports`12`

since 24 is even. You should only use variable assignment and the five arithmetic operations we have covered (`+`

,`-`

,`*`

,`/`

, and`%`

).*Hints*: use remainder and subtraction to round down; use remainder to help determine if a number is even or odd; take advantage of two convenient properties of multiplication: anything times one is itself and anything times zero is zero; and subtracting 0 from 1 is 1 and subtracting 1 from 1 is 0. In order to test your function you will likely want to modify`lab1.html`

so it contains an additional button that invokes your function when clicked. (Follow the model of the other buttons in`lab1.html`

.)