In: Computer Science
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1) Declare a variable named myName equal to your first name //Firstname is Susan // Construct a basic IF statement that prints the variable to the // console IF the length of myName is greater than 1 // 2) Copy your IF statement from above and paste it below // Change the IF statement to check if the length of myName // is greater than 10. // Add an ELSE statement to it that prints your last name // to the console // 3) Copy the IF / ELSE statement from #2 and paste it below // Add an ELSE IF statement to it that checks if the length of // myName is greater than 3. If true, print the word "javascript" // to the console. // 4) Think of your 5 favorite foods. // 5 fav foods: sushi, spaghetti, tacos, ramen, wings // Set a variable named myFaveFood equal to a random number from 1 to 5. // Construct a SWITCH statement that will print one favorite food to // the console for each number 1 through 5. // 5) Declare a variable named myNum and set it equal to // a random number from 1 to 100 (using the Math object). // Construct a TERNARY statement with the ternary operator // that checks if the number is greater than or equal to 75. // If true, print "Great weather!" to the console // If false, print "Still cold!" to the console
Ans 1.
<!Doctype html>
<html>
<body>
<h2>first part</h2>
<p>First name is : </p>
<p id="part1"></p>
<script> //start of javascript
var myname = "Susan"
var strnglen = myname.length;
if (strnglen > 1) //checking if length of firstname greater than
1
{
document.getElementById("part1").innerHTML = myname; //printing the
first name if condition satisfies
}
</script> //end of javascript
</body>
</html>
Ans 2.
<!Doctype html>
<html>
<body>
<h2>first part</h2>
<p>First name is : </p>
<p id="part2"></p>
<script>
var myname = "Susan"
var lastname = "mathhew"
var strnglen = myname.length;
if (strnglen > 10)
{
document.getElementById("part2").innerHTML = myname;
}
else
{
document.getElementById("part2").innerHTML = lastname; //printing
the else part if the condition doesn't satisfies
}
</script>
</body>
</html>
Ans 3.
<!Doctype html>
<html>
<body>
<h2>first part</h2>
<p>First name is : </p>
<p id="part3"></p>
<script>
var myname = "Susan"
var lastname = "mathhew"
var strnglen = myname.length;
if (strnglen > 10)
{
document.getElementById("part3").innerHTML = myname;
}
else if(strnglen >3) //use of else if
{
document.getElementById("part3").innerHTML = "javasript";
}
else
{
document.getElementById("part3").innerHTML = lastname;
}
</script>
</body>
</html>
Ans 4.
<!Doctype html>
<html>
<body>
<h2>first part</h2>
<p>First name is : </p>
<p id="part4"></p>
<script>
var myFaveFood = Math.floor(Math.random() *5) + 1; //function to
generate random number
var favfood; //variable to store the favorite food name
switch(myFaveFood)
{
case 1 : favfood = "sushi";
break;
case 2 : favfood = "spaghetti";
break;
case 3 : favfood = "tacos";
break;
case 4 : favfood = "ramen";
break;
case 5 : favfood = "wings";
break;
}
document.getElementById("part4").innerHTML = favfood;
</script>
</body>
</html>
Ans 5.
<!Doctype html>
<html>
<body>
<h2>first part</h2>
<p>First name is : </p>
<p id="part5"></p>
<script>
var myNum = Math.floor(Math.random() *100) + 1;
var weather;
weather = (myNum >= 75) ? "Great Weather!" : "Still Cold!";
//use of ternary operator
document.getElementById("part5").innerHTML = weather;
</script>
</body>
</html>