Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me write in javascript to test for prime numbers. I CANNOT edit the html code or the css code, I can only EDIT

Please help me write in javascript to test for prime numbers. I CANNOT edit the html code or the css code, I can only EDIT THE JAVASCRIPT:

These are the instruction:

Write code in script.js to implement the following functions.

  1. Write the isPrime function where indicated. The isPrime function should have one parameter and return true if it is a prime number and false otherwise. No integer less than 2 should be considered prime, and no non-number should be considered prime. Hints: You should use Number.isFinite to make sure that the given value is a finite number before testing it for primality. Use a loop to try all divisors between 2 and one less than the number you're testing. If none of them evenly divides the number you're testing, then it is a prime. Remember that n1 % n2 === 0 is true if and only if n2 evenly divides n1. Your isPrime function should run quickly and accurately even for large numbers like 19999999.
  2. Write the report function where indicated. The report function should have one parameter and output a string to the element with id equal to prime-or-not:
    • If the parameter's value is a finite number and is prime, the function should output the string 'prime'.
    • If the parameter's value is a finite number and is not prime, the function should output the string 'not prime'.
    • If the parameter's value is not a finite number, the function should output the string 'not a number'.
    Your report function should call your isPrime function to test primality.

//This is the .css that I cannot use

@charset "UTF-8";

/* Fixing the box-sizing attribute will make sizing boxes easier. */ html { box-sizing: border-box; }

/* Normally the * selector is smart to avoid, but for this purpose it's fine. */ *, *:before, *:after { box-sizing: inherit; }

body { background-color: rgb(255, 250, 245); font-family: Georgia, serif; }

button { margin: 5px; }

h1 { color: rgb(34, 68, 102); text-align: center; }

fieldset { border-color: rgb(68, 136, 204); border-radius: 10px; border-style: solid; border-width: 2px; float: left; }

legend { border-color: rgb(68, 136, 204); border-style: solid; border-width: 2px; padding: 0px 5px; }

input { text-align: center; }

label, textarea { display: block; }

.number-output { color: rgb(17, 68, 34); font-size: 200%; }

.square { border-color: rgb(0, 0, 0); border-style: solid; border-width: 1px; height: 256px; width: 256px; }

// This is the HTML code that I CANNOT edit

Studio 5

Make it functional

Test for prime numbers

is .

Calculate Fibonacci values

The th Fibonacci number is .

I'll reverse your input

Make this square colorful

Accumulator

Your total so far:

// This is the script I must change in .js

// All the code below will be run once the page content finishes loading. document.addEventListener('DOMContentLoaded', function () { 'use strict';

// Each little web app is hidden from the others using an IIFE. (function () { // Do not declare any other variables here, but you may declare variables inside your function.

// WRITE YOUR isPrime FUNCTION HERE

// The report function is hidden from the isPrime function using an IIFE. (function () { // Do not declare any other variables here, but you may declare variables inside your function.

// WRITE YOUR report FUNCTION HERE

// Call the report function even before there's a value to use. report(); // When the number is changed at all, immediately . . . document.querySelector('#primality-input').addEventListener('input', function () { // . . . call the report function and pass it the user's value. report(parseInt(document.querySelector('#primality-input').value, 10)); }); }()); }());

(function () { // Do not declare any other variables here, but you may declare variables inside your function.

// WRITE YOUR fibonacci FUNCTION HERE

// Do things when the "Calculate it" button is clicked. document.querySelector('#calculate-fibonacci').addEventListener('click', function () { // Get the user's number. const whichFibonacciNumber = parseInt(document.querySelector('#fibonacci-input').value, 10); // Use the fibonacci function to calculate the output. document.querySelector('#which-fibonacci-number').textContent = whichFibonacciNumber.toString(); document.querySelector('#fibonacci-number').textContent = fibonacci(whichFibonacciNumber).toString(); }); }());

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

Students also viewed these Databases questions