Question

In: Computer Science

Use Math.random function to generate a number between 20 and 100 and then use the ngSwitch...

  1. Use Math.random function to generate a number between 20 and 100 and then use the ngSwitch directive to display a letter grade based on this class grading policy.
    1. add implements OnInit to the AppComponent class
    2. add variable x in the AppComponent class
    3. add ngOnInit(){ this.x = Math.floor(Math.random()*10);}
    4. add {{x}} in the app.components.html file
    5. You should see numbers from 1-9 when you refresh the page
    6. Change formula in #c to generate numbers from 20 to 100.
    7. Change {{x}} in #d to ngSwitch directive

  1. Use attribute directives to display credit card logo based on the credit card number

4 visa

5 mastercard

34 and 37 amex

30, 36, 38, 39 diners

60, 64, 65 discover

Solutions

Expert Solution

import { Component, OnInit } from 'angular/core';

@Component({

selector: 'app-root',

templateUrl: '.app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements onInit {

title = 'title';

x: number;

ngOnInit(){

this.x = Math.floor(Math.random()*10);

}

}

Here is the app.component.html file:

<html lang="en">

<head>
<meta charset="utf-8">
<title>Angular</title>
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<app-root></app-root>
<div style="text-align:center">
<h1>
Welcome to {{x}}.
</h1>
</div>

<div [ngswitch]="switch_expression">
<p *ngSwitchCase="1">A</p>
<p *ngSwitchCase="2">B</p>
<p *ngSwitchCase="3">C</p>
<p *ngSwitchCase="4">D</p>
<p *ngSwitchDefault>F</p>
</div>

</body>
</html>

f.As C does not have an inbuilt function for generating a number in the range, but it does have rand function which generate a random number from 0 to RAND_MAX. With the help of rand () a number in range can be generated as num = (rand() % (upper – lower + 1)) + lower

// C program for generating a

// random number in a given range.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

  

// Generates and prints 'count' random

// numbers in range [lower, upper].

void printRandoms(int lower, int upper,  

int count)

{

int i;

for (i = 0; i < count; i++) {

int num = (rand() %

(upper - lower + 1)) + lower;

printf("%d ", num);

}

}

  

// Driver code

int main()

{

int lower = 5, upper = 7, count = 1;

  

// Use current time as  

// seed for random generator

srand(time(0));

  

printRandoms(lower, upper, count);

  

return 0;

}

Output:

7

11

Three things to keep in mind ngSwitch, ngSwitchCase and ngSwitchDefault.

ngSwitch - set the property value of model. For example - viewMode, which is a property in your component.

ngSwitchCase - Defines what to render when the value of the property defined in ngSwitchChanges. For ex. when viewMode = 'map'

ngSwitchDefault - Defines what to render if the value doesn't match. For ex. when viewMode=undefined. The default will be rendered.

Another important point is that we need to set the [ngSwitchCase] within a <template></template> HTML element otherwise it will not work. Angularwill automatically convert it into a <div> tag.

<div [ngSwitch]="'viewMode'">

<template [ngSwitchCase]="'map'" ngSwitchDefault>

Map View Content...

</template>   

<template [ngSwitchCase]="'list'">

List View Content...

</template>

</div>


Related Solutions

JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Write a program that does the following: Generate an array of 20 random integers between -100...
Write a program that does the following: Generate an array of 20 random integers between -100 and 100. Compute the average of the elements of the array and find the number of elements which are above the average. For example, if the elements of the array were 5 2 4 1 3 then your program should output The average is 3.0 There are two elements above the average Find the smallest element of the array as well as its index...
  Question no 3: USE PYTHON a. Generate a discrete uniform distribution of population size 100 between...
  Question no 3: USE PYTHON a. Generate a discrete uniform distribution of population size 100 between interval (1,10).) b Consider the sample size of N=10, Simulate the sampling distribution of the sample mean. (repeat 100 times) c Consider the sample size of N=30, what is the sample mean and sample standard deviation? (repeat 100 times)
Collect the Data: Use a random number generator to generate 50 values between 0 and 1...
Collect the Data: Use a random number generator to generate 50 values between 0 and 1 (inclusive). Theoretical Distribution In words, X = The theoretical distribution of X is X ~ U(0, 1). In theory, based upon the distribution X ~ U(0, 1), find μ = __________ σ = __________ 1st quartile = __________ 3rd quartile = __________ median = __________ Construct a box plot of the data. Be sure to use a ruler to scale accurately and draw straight...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements of the array having values between 50-80 and print the residual array Sort the data in the final array(residual)...
Use Excel to generate 100 N (10,5) distributed random values (that is 100 values of a...
Use Excel to generate 100 N (10,5) distributed random values (that is 100 values of a Normally distributed random variable with mean 10 and standard deviation 5). Plot histogram for your data. See Appendix on how to generate these values.
9. Generate a random number between 1 and 8, which is the computer’s number. Ask the...
9. Generate a random number between 1 and 8, which is the computer’s number. Ask the user to guess a number between 1 and 8. Write a sentinel-controlled loop to continue to executed until the user’s guess matches the computer’s number. If the user's guess matches the random number, print a message in green on the SenseHat congratulating the user and then break out of the loop. Otherwise, print a message in red on the SenseHat to ask the user...
Write a program that uses a custom function to generate a specified number of random integers...
Write a program that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also report how many...
Find the number of integers between 100 and 1000 that are
Find the number of integers between 100 and 1000 that are (i) divisible by 7  (ii) not divisible by 7      
Using MS Excel and the random number generator function, generate values for 30 observations for the...
Using MS Excel and the random number generator function, generate values for 30 observations for the following columns with average daily: Body weight with random values between 100 and 250lbs Calories intake with random values between 1000 and 3000 calories Workout duration with random values between 0 and 60 minutes Sleep duration with random values between 2 and 12 hours Work duration with random values between 0 and 12 hours Assuming that the values are averages over 1 year, conduct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT