Click here to Skip to main content
15,881,757 members
Home / Discussions / JavaScript
   

JavaScript

 
QuestionRe: e.target.value, switch-case operators Pin
Richard MacCutchan6-Dec-18 23:26
mveRichard MacCutchan6-Dec-18 23:26 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz7-Dec-18 3:47
ghazabaz7-Dec-18 3:47 
AnswerRe: e.target.value, switch-case operators Pin
Richard MacCutchan7-Dec-18 4:29
mveRichard MacCutchan7-Dec-18 4:29 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 5:37
mveRichard Deeming7-Dec-18 5:37 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz7-Dec-18 6:44
ghazabaz7-Dec-18 6:44 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 6:51
mveRichard Deeming7-Dec-18 6:51 
AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming7-Dec-18 6:54
mveRichard Deeming7-Dec-18 6:54 
QuestionRe: e.target.value, switch-case operators Pin
ghazabaz8-Dec-18 5:19
ghazabaz8-Dec-18 5:19 
Richard, thank you very much.

After implementing the suggested corrections (added "append" operator to line 95 in mt_calc.js file) the buttons are functional except the "previous" calculator buttons. Also, enter, delete and up arrow (supposed to copy & yield previous equation) are not working when used on keyboard.
Also getting a "NaN" along with the answer?

These are the instructions for the enter, delete and up up arrow buttons:

Quote:
. Click within the calculator window and, using the keyboard, directly type a mathematical expression. Verify that pressing the Enter key evaluates the current expression and moves the cursor to a new line. Show that you can copy the previous expression by pressing the Enter key followed by the up-arrow key on your keyboard. Finally verify that pressing the Delete key clears the contents of the calculator window. Note: Keyboard commands might not work in the Safari browser since Safari does not support the key property at the time of this writing.


I added the append operator to calcValue = calcValue + buttonValue; as the instructions require:

Quote:
Otherwise, append the calculator button character to the calculator window by letting, calcValue equal calcValue plus buttonValu


But when a calculator digit is entered and entered again it multiplies itself infinitely.

I was hinted at these sections as the answer:

switch(buttonValue) {
		case "del":
		calcValue = "";
		break;
		case "bksp":
		calcValue = eraseChar(calcValue);
		break;
		case "enter":
		calcValue = + " = " + evalEq(calcValue, calcDecimal); // Here
		break;
		case "prev":
		calcValue = lastEq(calcValue);  // This one
		default:
		calcValue = calcValue + buttonValue;
		break;
	}



// Begins a switch-case structure, assigning values to each of the buttons below (delete, enter, arrow up) when pressed
	switch(e) {
		case "Delete": 
		calcValue = "";
		break;
		case "Enter":
		calcValue = + " = " + evalEq(calcValue, calcDecimal);  // This one
		break;
		case "ArrowUp":
		calcValue +=  lastEq(calcWindow.value); // This one but it is exacly as the instructions ask for:

// "iii.	For "ArrowUp", add the following expression to calcValue
// lastEq(calcWindow.value)"


		break;
		case "ArrowUp":
		e.preventDefault();
		break;
	}


Here is the entire code for reference:

"use strict";

/*	
   Functions List:

   init()
      Initializes the contents of the web page and sets up the
      event handlers
      
   buttonClick(e)
      Adds functions to the buttons clicked within the calcutlor
      
   calcKeys(e)
      Adds functions to key pressed within the calculator window 
      
   eraseChar(textStr)
      Erases the last character from the text string, textStr
      
   evalEq(textStr, decimals) 
      Evaluates the equation in textStr, returning a value to the number of decimals specified by the decimals parameter

   lastEq(textStr) 
      Returns the previous expression from the list of expressions in the textStr parameter

*/

// Loads the init function when the broser window is opened
window.addEventListener("load", init);

// Declares the init() function
function init() {
	
	// Declares the calcButtons variable and sets it equal to the element calcButton
	var calcButtons = document.getElementsByClassName("calcButton");
	
	// Loops through calcButtons and runs the buttonClick() function when a button is clicked
	for (var i = 0; i < calcButtons.length; i++) {
		calcButtons[i].addEventListener("click", buttonClick);
	}
	
	// Runs the calcKeys function when a key is pressed within the calcWindow element
	document.getElementById("calcWindow").addEventListener("keydown", calcKeys);
}

// Declares the buttonClick(e) function
function buttonClick(e) {
	
	
	// Declares variables calcValue, calcDecimal and buttonValues
	var calcValue = document.getElementById("calcWindow").value;
	var calcDecimal = document.getElementById("decimals").value;
	var buttonValue = e.target.value;
	
	// Begins a switch-case structure to assign events to each button pressed
	switch(buttonValue) {
		case "del":
		calcValue = "";
		break;
		case "bksp":
		calcValue = eraseChar(calcValue);
		break;
		case "enter":
		calcValue = + " = " + evalEq(calcValue, calcDecimal) + "\n";
		break;
		case "prev":
		calcValue = lastEq(calcValue);
		default:
		calcValue = calcValue + buttonValue;
		break;
	}

document.getElementById("calcWindow").value = calcValue;
document.getElementById("calcWindow").focus();

}


// Declares the calcKeys(e) function
function calcKeys(e) {
	
	var calcValue = document.getElementById("calcWindow").value;
	var calcDecimal = document.getElementById("decimals").value;
	
	
	// Begins a switch-case structure, assigning values to each of the buttons below (delete, enter, arrow up) when pressed
	switch(e) {
		case "Delete": 
		calcValue = "";
		break;
		case "Enter":
		calcValue = + " = " + evalEq(calcValue, calcDecimal);
		break;
		case "ArrowUp":
		calcValue +=  lastEq(calcWindow.value) ;
		break;
		case "ArrowUp":
		e.preventDefault();
		break;
	}
	

	document.getElementById("calcWindow").value = calcValue;
	
}


/* ===================================================================== */

function eraseChar(textStr) { 
   return textStr.substr(0, textStr.length - 1);
}

function evalEq(textStr, decimals) {
   var lines = textStr.split(/\r?\n/);
   var lastLine = lines[lines.length-1];
   var eqValue = eval(lastLine);
   return eqValue.toFixed(decimals);
}  

function lastEq(textStr) {
   var lines = textStr.split(/\r?\n/);
   var lastExp = lines[lines.length-2];
   return lastExp.substr(0, lastExp.indexOf("=")).trim();
}

AnswerRe: e.target.value, switch-case operators Pin
Richard Deeming11-Dec-18 1:16
mveRichard Deeming11-Dec-18 1:16 
PraiseRe: e.target.value, switch-case operators Pin
ghazabaz13-Dec-18 6:05
ghazabaz13-Dec-18 6:05 
QuestionEasy HTML Editor Pin
Bram van Kampen3-Dec-18 15:23
Bram van Kampen3-Dec-18 15:23 
AnswerRe: Easy HTML Editor Pin
Nathan Minier6-Dec-18 1:24
professionalNathan Minier6-Dec-18 1:24 
AnswerRe: Easy HTML Editor Pin
Nitin S7-Dec-18 1:57
professionalNitin S7-Dec-18 1:57 
AnswerRe: Easy HTML Editor Pin
RedDk28-Dec-18 11:44
RedDk28-Dec-18 11:44 
GeneralRe: Easy HTML Editor Pin
Bram van Kampen28-Dec-18 12:55
Bram van Kampen28-Dec-18 12:55 
QuestionProblems to obtain value "0" or "1" from a div Pin
serenimus1-Dec-18 0:37
serenimus1-Dec-18 0:37 
AnswerRe: Problems to obtain value "0" or "1" from a div Pin
ZurdoDev4-Dec-18 2:27
professionalZurdoDev4-Dec-18 2:27 
QuestionUnable to switch events in jquery Pin
vmann30-Nov-18 18:45
vmann30-Nov-18 18:45 
SuggestionRe: Unable to switch events in jquery Pin
ZurdoDev4-Dec-18 2:28
professionalZurdoDev4-Dec-18 2:28 
QuestionError on line 45 Pin
ghazabaz30-Nov-18 13:38
ghazabaz30-Nov-18 13:38 
AnswerRe: Error on line 45 Pin
ZurdoDev4-Dec-18 2:29
professionalZurdoDev4-Dec-18 2:29 
AnswerRe: Error on line 45 Pin
maryam.saboor4-Dec-18 23:54
professionalmaryam.saboor4-Dec-18 23:54 
Questiondata transfer in two dimensional array format Pin
Member 1406088822-Nov-18 20:31
Member 1406088822-Nov-18 20:31 
QuestionRe: data transfer in two dimensional array format Pin
ZurdoDev4-Dec-18 2:29
professionalZurdoDev4-Dec-18 2:29 
QuestionJavascript syntax Pin
ghazabaz22-Nov-18 15:48
ghazabaz22-Nov-18 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.