Question

In: Computer Science

does this java script code seam incorrect to anyone of you ? var inch=document.getElementById("i"); var cm=document.getElementById("c");...

does this java script code seam incorrect to anyone of you ?

var inch=document.getElementById("i");
var cm=document.getElementById("c");
function convert() {
cm.value=(inch*2.54);
}

here is the html code also

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">

<title>Length Converter</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<script src="length.js"></script>
<!-- Custom styles for this template -->
<link href="css/the-big-picture.css" rel="stylesheet">
  
</head>

<body>

<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom">
<div class="container">
<a class="navbar-brand" href="#">Home</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>

<h1>Length Converter</h1>
  
<p><input type="number" id="i" placeholder="Enter Inches" name="inch"/> In.</p>

<p> <input type="number" id="c" placeholder="Enter Centimeters" name="cm" disabled > Cm.</p>
<input type="submit" onclick= "convert()" value="Convert"/>


<!-- Page Content -->
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>

</html>

i cant convert to the right amount of cm if i putt in 99 inches i get 39 centimeters which is incorrect  

Solutions

Expert Solution

There are two errors in this code:

1. In the function convert the code should be:

function convert() {

cm.value=(inch.value*2.54);

}

It should be inch.value*2.54 not inch*2.54

2. <script src = "length.js"></script>

It should be declared after declaring input type with ID's "i" and "c", because code will first include length.js file and bind it to the webpage and in that web page until now there will be no input types with ID's "i" and "c". So it will log a null var exception in browser console.

You should declare this script tag before closing </body> tag

Correct Code:

length.js

var inch=document.getElementById("i");

var cm=document.getElementById("c");

function convert() {

    cm.value=(inch.value*2.54);

}

HTML Code for webPage

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<meta name="description" content="">

<meta name="author" content="">

<title>Length Converter</title>

<!-- Bootstrap core CSS -->

<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom styles for this template -->

<link href="css/the-big-picture.css" rel="stylesheet">

</head>

<body>

<!-- Navigation -->

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom">

<div class="container">

<a class="navbar-brand" href="#">Home</a>

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">

<span class="navbar-toggler-icon"></span>

</button>

</div>

</nav>

<h1>Length Converter</h1>

<p><input type="number" id="i" placeholder="Enter Inches" name="inch"/> In.</p>

<p> <input type="number" id="c" placeholder="Enter Centimeters" name="cm" disabled > Cm.</p>

<input type="submit" onclick= "convert()" value="Convert"/>

<!-- Page Content -->

<!-- Bootstrap core JavaScript -->

<script src="vendor/jquery/jquery.min.js"></script>

<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- Including length.js file after creating input fields -->

<script src="length.js"></script>

</body>

</html>

Sample Output:


Related Solutions

I need this Java code translated into C Code. Thank you. //Logical is the main public...
I need this Java code translated into C Code. Thank you. //Logical is the main public class public class Logical { public static void main(String args[]) { char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}}; // table contains total combinations of p,q,& r int totalTrue, totalFalse, proposition;    //proposition 1: proposition=1; start(proposition); totalTrue=0; totalFalse=0; for(int i=0;i<8;i++) {    { char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0])); System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o); if(o=='T') totalTrue++; else totalFalse++;    } } finalOutput(totalTrue,totalFalse,proposition); System.out.println(" "); System.out.println(" ");    //proposition 2: proposition=2; start(proposition);...
In Java Script how do you code an array with the values and suits of cards...
In Java Script how do you code an array with the values and suits of cards (all 52 cards in a deck) 2 different array one that just has the face values of the card, and the other with the suit value. Then have it randomly give you 1-2 cards from the deck without repeating? (Example: Dealer gives player two cards. Player asks for a hit and the dealer gives a different card, no repeats). Game is Blackjack.
Why does my code print nothing in cout? I think my class functions are incorrect but...
Why does my code print nothing in cout? I think my class functions are incorrect but I don't see why. bigint.h: #include <string> #include <vector> class BigInt { private:    int m_Input, m_Temp;    std::string m_String = "";    std::vector<int> m_BigInt; public:    BigInt(std::string s)   // convert string to BigInt    {        m_Input = std::stoi(s);        while (m_Input != 0)        {            m_Temp = m_Input % 10;            m_BigInt.push_back(m_Temp);       ...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
Swift Code 4 only please var arr: [String] = ["a","b","s","c"] var dictionary : [String : Int]...
Swift Code 4 only please var arr: [String] = ["a","b","s","c"] var dictionary : [String : Int] = ["a":1, "b":2, "c":3, "d":4] for i in arr if(dict[i] = even) print ("even number" if else (dict[i] =odd) print("odd number") else print("Does not exist") Is there a way to compare the string array with the dictionary keys. Use the array to see if string exists as a key in the dictionary. Depending on the value of the key, it will print a specific...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT